进程间通信______共享内存

来源:互联网 发布:感情淡了 知乎 编辑:程序博客网 时间:2024/04/29 01:05
#include <sys/types.h>#include <sys/ipc.h>#include <sys/shm.h>#include <stdio.h>#include <stdlib.h>#define BUFSZ 2048int main(){int shmid;char *shmadd;if((shmid=shmget(IPC_PRIVATE,BUFSZ,0666))<0)//创建共享内存{perror("shmget");exit(1);}elseprintf("created shared-memory: %d\n",shmid);system("ipcs -m");if((shmadd=shmat(shmid,0,0))<(char *)0){ //映射共享内存perror("shmat");exit(1);}elseprintf("attached shared-memory\n");system("ipcs -m");if((shmdt(shmadd))<0){perror("shmdt");exit(1);}elseprintf("deleted shared-memory\n");system("ipcs -m");exit(0);}

0 0
原创粉丝点击