进程间通信之2----共享内存
生活随笔
收集整理的這篇文章主要介紹了
进程间通信之2----共享内存
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
進程間通信之2----共享內存
函數(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 用于將共享內存段與進程空間分離。
? ? ?函數(shù) shmctl 是共享內存的控制函數(shù), 可以用來刪除共享內存段。
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 共享內存機制: shmget shmat shmdt shmctl
- 共享內存本質是一段特殊的內存區(qū)域,所有需要訪問該共享區(qū)域的進程都要把該共享區(qū)域映射到本進程的地址空間中去,不同的進程可以通過對內存簡單的讀寫,發(fā)生信息交換,從容實現(xiàn)通信。而這塊虛擬內存的頁面被每個共享進程的頁表條目所引用, 同時并不需要在所有進程的虛擬內存都有相同的地址。 進程對象對于共享內存的訪問通過 key(鍵) 來控制, 同時通過 key 進行訪問權限的檢查。
函數(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)建一個關鍵字, 可以用該關鍵字關聯(lián)一個共享內存段。
- 函數(shù) shmget 用于創(chuàng)建或打開一共享內存段, 該內存段由函數(shù)的第一個參數(shù)唯一創(chuàng)建。函數(shù)成功, 則返回一個唯一的共享內存標識號(相當于進程號, 唯一的標識著共享內存),失敗返回-1。
- 參數(shù) key 是一個與共享內存段相關聯(lián)關鍵字, 如果事先已經存在一個與指定關鍵字關聯(lián)的共享內存段, 則直接返回該內存段的標識, 表示打開, 如果不存在, 則創(chuàng)建一個新的共享內存段。------可用于連接。
- key 的值既可以用 ftok 函數(shù)產生,也可以是IPC_PRIVATE(用于創(chuàng)建一個只屬于創(chuàng)建進程的共享內存, 主要用于父子通信) ,表示總是創(chuàng)建新的共享內存段;
- 參數(shù) size 指定共享內存段的大小, 以字節(jié)為單位;一般為頁面的整數(shù)倍--一般一個頁面為4k---4096個字節(jié)
- 參數(shù) shmflg 是一掩碼合成值, 可以是訪問權限值與(IPC_CREAT 或 IPC_EXCL)的
- 合成。 IPC_CREAT 表示如果不存在該內存段, 則創(chuàng)建它。 IPC_EXCL 表示如果該內存
- 段存在, 則函數(shù)返回失敗結果(-1)。 如果調用成功, 返回內存段標識, 否則返回-1
? ????shmat接口:
- 函數(shù) shmat 將共享內存段映射到進程空間的某一地址。
- 參數(shù) shmid 是共享內存段的標識 通常應該是 shmget 的成功返回值
- 參數(shù) shmaddr 指定的是共享內存連接到當前進程中的地址位置。通常是 NULL, 表示讓系統(tǒng)來選擇共享內存出現(xiàn)的地址。
- 參數(shù) shmflg 是一組位標識, 通常為 0 即可。
- 如果調用成功, 返回映射后的進程空間的首地址, 否則返回(char *)-1。
? ? ?函數(shù) shmdt 用于將共享內存段與進程空間分離。
- 參數(shù) shmaddr 通常為 shmat 的成功返回值
- 它共享內存分離并沒刪除它, 只是使得該共享內存對當前進程不在可用。
? ? ?函數(shù) shmctl 是共享內存的控制函數(shù), 可以用來刪除共享內存段。
- 參數(shù) shmid同上。
- 參數(shù) cmd 是對共享內存段的操作方式, 可選為 IPC_STAT,IPC_SET,IPC_RMID。 通常為 IPC_RMID, 表示刪除共享內存段。
- 參數(shù) buf 是表示共享內存段的信息結構體數(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)共享內存ipcs
- 刪除共享內存ipcrm -m shmid
總結
以上是生活随笔為你收集整理的进程间通信之2----共享内存的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 题解 P1091 【合唱队形】
- 下一篇: 6内置数据结构_set