进程间通信之2----共享内存
生活随笔
收集整理的這篇文章主要介紹了
进程间通信之2----共享内存
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
進(jìn)程間通信之2----共享內(nèi)存
函數(shù)定義如下: #include <sys/types.h> #include <sys/ipc.h> #include <sys/shm.h> key_t ftok(const char *pathname, int proj_id); ? ?不常用。 int shmget(key_t key, int size, int shmflg); void *shmat(int shmid, const void *shmaddr, int shmflg); int shmdt(const void *shmaddr); int shmctl(int shmid, int cmd, struct shmid_ds *buf);
? ? ?shmget接口:
? ????shmat接口:
? ? ?函數(shù) shmdt 用于將共享內(nèi)存段與進(jìn)程空間分離。
? ? ?函數(shù) shmctl 是共享內(nèi)存的控制函數(shù), 可以用來刪除共享內(nèi)存段。
struct shmid_ds { ? ? ? ? ? ? ? ?struct ipc_perm shm_perm;? ? /* Ownership and permissions */ ? ? ? ? ? ? ? ?size_t? ? ? ? ? shm_segsz;? ?/* Size of segment (bytes) */ ? ? ? ? ? ? ? ?time_t? ? ? ? ? shm_atime;? ?/* Last attach time */ ? ? ? ? ? ? ? ?time_t? ? ? ? ? shm_dtime;? ?/* Last detach time */ ? ? ? ? ? ? ? ?time_t? ? ? ? ? shm_ctime;? ?/* Last change time */ ? ? ? ? ? ? ? ?pid_t? ? ? ? ? ?shm_cpid;? ? /* PID of creator */ ? ? ? ? ? ? ? ?pid_t? ? ? ? ? ?shm_lpid;? ? /* PID of last shmat(2)/shmdt(2) */ ? ? ? ? ? ? ? ?shmatt_t? ? ? ? shm_nattch;? /* No. of current attaches */ ? ? ? ? ? ? ? ?... ? ? ? ? ? ?};
struct ipc_perm { ? ? ? ? ? ? ? ?key_t? ? ? ? ? __key;? ? /* Key supplied to shmget(2) */ ? ? ? ? ? ? ? ?uid_t? ? ? ? ? uid;? ? ? /* Effective UID of owner */ ? ? ? ? ? ? ? ?gid_t? ? ? ? ? gid;? ? ? /* Effective GID of owner */ ? ? ? ? ? ? ? ?uid_t? ? ? ? ? cuid;? ? ?/* Effective UID of creator */ ? ? ? ? ? ? ? ?gid_t? ? ? ? ? cgid;? ? ?/* Effective GID of creator */ ? ? ? ? ? ? ? ?unsigned short mode;? ? ?/* Permissions + SHM_DEST and ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?SHM_LOCKED flags */ ? ? ? ? ? ? ? ?unsigned short __seq;? ? /* Sequence number */ ? ? ? ? ? ?};
1、System V 共享內(nèi)存機(jī)制: shmget shmat shmdt shmctl
- 共享內(nèi)存本質(zhì)是一段特殊的內(nèi)存區(qū)域,所有需要訪問該共享區(qū)域的進(jìn)程都要把該共享區(qū)域映射到本進(jìn)程的地址空間中去,不同的進(jìn)程可以通過對內(nèi)存簡單的讀寫,發(fā)生信息交換,從容實現(xiàn)通信。而這塊虛擬內(nèi)存的頁面被每個共享進(jìn)程的頁表條目所引用, 同時并不需要在所有進(jìn)程的虛擬內(nèi)存都有相同的地址。 進(jìn)程對象對于共享內(nèi)存的訪問通過 key(鍵) 來控制, 同時通過 key 進(jìn)行訪問權(quán)限的檢查。
函數(shù)定義如下: #include <sys/types.h> #include <sys/ipc.h> #include <sys/shm.h> key_t ftok(const char *pathname, int proj_id); ? ?不常用。 int shmget(key_t key, int size, int shmflg); void *shmat(int shmid, const void *shmaddr, int shmflg); int shmdt(const void *shmaddr); int shmctl(int shmid, int cmd, struct shmid_ds *buf);
? ? ?shmget接口:
- 函數(shù) ftok 用于創(chuàng)建一個關(guān)鍵字, 可以用該關(guān)鍵字關(guān)聯(lián)一個共享內(nèi)存段。
- 函數(shù) shmget 用于創(chuàng)建或打開一共享內(nèi)存段, 該內(nèi)存段由函數(shù)的第一個參數(shù)唯一創(chuàng)建。函數(shù)成功, 則返回一個唯一的共享內(nèi)存標(biāo)識號(相當(dāng)于進(jìn)程號, 唯一的標(biāo)識著共享內(nèi)存),失敗返回-1。
- 參數(shù) key 是一個與共享內(nèi)存段相關(guān)聯(lián)關(guān)鍵字, 如果事先已經(jīng)存在一個與指定關(guān)鍵字關(guān)聯(lián)的共享內(nèi)存段, 則直接返回該內(nèi)存段的標(biāo)識, 表示打開, 如果不存在, 則創(chuàng)建一個新的共享內(nèi)存段。------可用于連接。
- key 的值既可以用 ftok 函數(shù)產(chǎn)生,也可以是IPC_PRIVATE(用于創(chuàng)建一個只屬于創(chuàng)建進(jìn)程的共享內(nèi)存, 主要用于父子通信) ,表示總是創(chuàng)建新的共享內(nèi)存段;
- 參數(shù) size 指定共享內(nèi)存段的大小, 以字節(jié)為單位;一般為頁面的整數(shù)倍--一般一個頁面為4k---4096個字節(jié)
- 參數(shù) shmflg 是一掩碼合成值, 可以是訪問權(quán)限值與(IPC_CREAT 或 IPC_EXCL)的
- 合成。 IPC_CREAT 表示如果不存在該內(nèi)存段, 則創(chuàng)建它。 IPC_EXCL 表示如果該內(nèi)存
- 段存在, 則函數(shù)返回失敗結(jié)果(-1)。 如果調(diào)用成功, 返回內(nèi)存段標(biāo)識, 否則返回-1
? ????shmat接口:
- 函數(shù) shmat 將共享內(nèi)存段映射到進(jìn)程空間的某一地址。
- 參數(shù) shmid 是共享內(nèi)存段的標(biāo)識 通常應(yīng)該是 shmget 的成功返回值
- 參數(shù) shmaddr 指定的是共享內(nèi)存連接到當(dāng)前進(jìn)程中的地址位置。通常是 NULL, 表示讓系統(tǒng)來選擇共享內(nèi)存出現(xiàn)的地址。
- 參數(shù) shmflg 是一組位標(biāo)識, 通常為 0 即可。
- 如果調(diào)用成功, 返回映射后的進(jìn)程空間的首地址, 否則返回(char *)-1。
? ? ?函數(shù) shmdt 用于將共享內(nèi)存段與進(jìn)程空間分離。
- 參數(shù) shmaddr 通常為 shmat 的成功返回值
- 它共享內(nèi)存分離并沒刪除它, 只是使得該共享內(nèi)存對當(dāng)前進(jìn)程不在可用。
? ? ?函數(shù) shmctl 是共享內(nèi)存的控制函數(shù), 可以用來刪除共享內(nèi)存段。
- 參數(shù) shmid同上。
- 參數(shù) cmd 是對共享內(nèi)存段的操作方式, 可選為 IPC_STAT,IPC_SET,IPC_RMID。 通常為 IPC_RMID, 表示刪除共享內(nèi)存段。
- 參數(shù) buf 是表示共享內(nèi)存段的信息結(jié)構(gòu)體數(shù)據(jù), 通常為 NULL。
struct shmid_ds { ? ? ? ? ? ? ? ?struct ipc_perm shm_perm;? ? /* Ownership and permissions */ ? ? ? ? ? ? ? ?size_t? ? ? ? ? shm_segsz;? ?/* Size of segment (bytes) */ ? ? ? ? ? ? ? ?time_t? ? ? ? ? shm_atime;? ?/* Last attach time */ ? ? ? ? ? ? ? ?time_t? ? ? ? ? shm_dtime;? ?/* Last detach time */ ? ? ? ? ? ? ? ?time_t? ? ? ? ? shm_ctime;? ?/* Last change time */ ? ? ? ? ? ? ? ?pid_t? ? ? ? ? ?shm_cpid;? ? /* PID of creator */ ? ? ? ? ? ? ? ?pid_t? ? ? ? ? ?shm_lpid;? ? /* PID of last shmat(2)/shmdt(2) */ ? ? ? ? ? ? ? ?shmatt_t? ? ? ? shm_nattch;? /* No. of current attaches */ ? ? ? ? ? ? ? ?... ? ? ? ? ? ?};
struct ipc_perm { ? ? ? ? ? ? ? ?key_t? ? ? ? ? __key;? ? /* Key supplied to shmget(2) */ ? ? ? ? ? ? ? ?uid_t? ? ? ? ? uid;? ? ? /* Effective UID of owner */ ? ? ? ? ? ? ? ?gid_t? ? ? ? ? gid;? ? ? /* Effective GID of owner */ ? ? ? ? ? ? ? ?uid_t? ? ? ? ? cuid;? ? ?/* Effective UID of creator */ ? ? ? ? ? ? ? ?gid_t? ? ? ? ? cgid;? ? ?/* Effective GID of creator */ ? ? ? ? ? ? ? ?unsigned short mode;? ? ?/* Permissions + SHM_DEST and ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?SHM_LOCKED flags */ ? ? ? ? ? ? ? ?unsigned short __seq;? ? /* Sequence number */ ? ? ? ? ? ?};
- 查看系統(tǒng)共享內(nèi)存ipcs
- 刪除共享內(nèi)存ipcrm -m shmid
總結(jié)
以上是生活随笔為你收集整理的进程间通信之2----共享内存的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 题解 P1091 【合唱队形】
- 下一篇: 6内置数据结构_set