Linux System V IPC 共享內存
#include<sys/types.h>
#include<sys/ipc.h>
#include<sys/shm.h>
//獲取key值, key值是System V IPC的標識符,成功返回key,失敗返回-1設errno
//同pathname+同 proj_id==>同key_t;
key_t ftok(const char *pathname, int proj_id);
pathname :文件名
proj_id: 1~255的一個數,表示project_id
key_t key=ftok(".",100); //“.”就是一個存在且可訪問的路徑, 100是假設的proj_id
if(-1==key)
perror("ftok"),exit(-1);
//創建/獲取共享內存,成功返回共享內存的標識符shmid,失敗返回-1設errno
int shmget(key_t key, size_t size, int shmflg); //多設為int shmid=... 和shmat()一起用比較好看
key :ftok()的返回值
size:共享內存的大小,實際會按照頁的大小(PAGE_SIZE)來分配。0表示獲取已經分配好的共享內存
shmflg:具體的操作標志
//創建shared memory
shmid=shmget(key,4,IPC_CREAT|IPC_EXCL|0664);
if(-1==shmid)
perror("shmget"),exit(-1);
Q:既然shmget()可以創建, 那要ftok()有啥用
A:shmget才是創建共享內存, ftok()只是用來產生一個key
//掛接共享內存,成功返回映射內存的地址,失敗返回(void*)-1設errno
void *shmat(int shmid, const void *shmaddr, int shmflg);
shmid: shmget()的返回值
shmaddr
//掛接共享內存
void* pv=shmat(shmid,NULL,0);
if((void*)-1==pv)
perror("shmat"),exit(-1);
//脫接共享內存,成功返回0,失敗返回-1設errno
int shmdt(const void *shmaddr);
//脫接shm
int res=shmdt(pv);
if(-1==res)
perror("shmdt"),exit(-1);
//共享內存管理,成功返回0,失敗返回-1設errno
int shmctl(int shmid, int cmd, struct shmid_ds *buf);
shmid:共享內存的id,由shmget()返回
buf : shmid_ds類型的指針
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 */
...
};
//<sys/ipc.h>
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 */
};
cmd
IPC_INFO(Linux-specific)返回系統對共享內存的限制寫入到buf指向的時shminfo結構體中
//_GNU_SOURCE
struct shminfo {
unsigned long shmmax; /* Maximum segment size */
unsigned long shmmin; /* Minimum segment size; always 1 */
unsigned long shmmni; /* Maximum number of segments */
unsigned long shmseg; /* Maximum number of segments that a process can attach; unused within kernel */
unsigned long shmall; /* Maximum number of pages of shared memory, system-wide */
};
//shmmni, shmmax, and shmall 可以童工/proc裡的同名文件進行修改
SHM_INFO(Linux-specific) 返回一個shm_info結構體來表示該共享內存消耗的系統資源
//_GNU_SOURCE
struct shm_info {
int used_ids; /* # of currently existing segments */
unsigned long shm_tot; /* Total number of shared memory pages */
unsigned long shm_rss; /* # of resident shared memory pages */
unsigned long shm_swp; /* # of swapped shared memory pages */
unsigned long swap_attempts; /* Unused since Linux 2.4 */
unsigned long swap_successes;/* Unused since Linux 2.4 */
};
SHM_UNLOCK 解除鎖定,即允許共享內存被系統放到swap區
//使用IPC_RMID刪除共享內存
int res=shmctl(shmid,IPC_RMID,NULL);
if(-1==res)
perror("shmctl"),exit(-1);
//Sys V IPC shm
int shmid; //定義全局變量記錄id
void fa(int signo){
printf("deleting shared memories...\n");
sleep(3);//其實沒用
int res=shmctl(shmid,IPC_RMID,NULL);
if(-1==res)
perror("shmctl"),exit(-1);
printf("delete success\n");
exit(0); //ctrl+C已經不能結束while(1),用exit(0)來終結
}
int main(){
//獲取key
key_t key=ftok(".",100); //.就是一個存在且可訪問的路徑, 100是隨便給的
if(-1==key)
perror("ftok"),exit(-1);
printf("key=%#x\n",key); //打印出進制的標示,即0x
//創建shared memory
shmid=shmget(key,4,IPC_CREAT|IPC_EXCL|0664);
if(-1==shmid)
perror("shmget"),exit(-1);
printf("shmid=%d\n",shmid);
//掛接shm
void* pv=shmat(shmid,NULL,0);
if((void*)-1==pv)
perror("shmat"),exit(-1);
printf("link shared memory success\n");
//訪問shm
int* pi=(int*)pv;
*pi=100;
//脫接shm
int res=shmdt(pv);
if(-1==res)
perror("shmdt"),exit(-1);
printf("unlink success\n");
//如果不再使用,刪除shm
printf("刪除共享內存請按Ctrl C...\n");
if(SIG_ERR==signal(SIGINT,fa))
perror("signal"),exit(-1);
while(1);
return 0;
}