linux 进程通信之共享内存
来源:互联网 发布:js插件开发教程 编辑:程序博客网 时间:2024/05/21 21:34
一,创建共享内存
void *shmat(int shmid, void *shmaddr, int shmflg);该系统调用将shmid 对应的共享内存区映射到进程的虚拟地址空间中,shmaddr 为指定的映射起始地址,其值为NULL 时,映射空间由系统确定;shmflg 为标志字,其值一般指定为0。
/* * mkshm.c - Create and initialize shared memory segment */#include <sys/types.h>#include <sys/ipc.h>#include <sys/shm.h>#include <stdio.h>#include <stdlib.h>#define BUFSZ 4096 /* Size of the segment */int main(void){ int shmid; if((shmid = shmget(IPC_PRIVATE, BUFSZ, IPC_CREAT | 0666)) < 0) { perror("shmget"); exit(EXIT_FAILURE); } printf("segment created: %d\n", shmid); system("ipcs -m"); exit(EXIT_SUCCESS);}
查看目前已有的共享内存命令:ipcs -m
二,向共享内存中写入数据
/* * wrshm.c - Write data to a shared memory segment */#include <sys/types.h>#include <sys/ipc.h>#include <sys/shm.h>#include <stdio.h>#include <stdlib.h>#include <fcntl.h>#include <ctype.h>#include <unistd.h>#define BUFSZ 4096int main(int argc, char *argv[]){ int shmid;/* Segment ID */ char *shmbuf;/* Address in process */ key_t key; char *msg; int len; key = ftok("/tmp", 0); if((shmid = shmget(key, BUFSZ, IPC_CREAT|0666)) < 0) { perror("shmget"); exit(EXIT_FAILURE); } printf("segment created: %d\n", shmid); system("ipcs -m"); /* Attach the segment */ if((shmbuf = (char *)shmat(shmid, 0, 0)) < 0) { perror("shmat"); exit(EXIT_FAILURE); } /* Write message to the segment */ msg = "this is the message written by wrshm program."; len = strlen(msg); strcpy(shmbuf, msg); printf("%s\nTotal %d characters written to shared memory.\n", msg, len); /* De-attach the segment */ if(shmdt(shmbuf) < 0) { perror("shmdt"); exit(EXIT_FAILURE); } exit(EXIT_SUCCESS);}
三,从共享内存中读入数据
/* * wrshm.c - Write data to a shared memory segment */#include <sys/types.h>#include <sys/ipc.h>#include <sys/shm.h>#include <stdio.h>#include <stdlib.h>#include <fcntl.h>#include <ctype.h>#include <unistd.h>#define BUFSZ 4096int main(int argc, char *argv[]){ int shmid;/* Segment ID */ char *shmbuf;/* Address in process */ key_t key; int len; key = ftok("/tmp", 0); /* get the same share memory block */ if((shmid = shmget(key, 0, 0)) < 0) { perror("shmget"); exit(EXIT_FAILURE); } /* Attach the segment */ if((shmbuf = (char *)shmat(shmid, 0, 0)) < 0) { perror("shmat"); exit(EXIT_FAILURE); } /* read from share memory */ printf("Info read form shared memory is:\n%s\n", shmbuf); /* De-attach the segment */ if(shmdt(shmbuf) < 0) { perror("shmdt"); exit(EXIT_FAILURE); } exit(EXIT_SUCCESS);}
- linux进程间通信之共享内存
- linux进程间通信之共享内存
- linux 进程通信之共享内存
- Linux进程间通信之共享内存
- Linux进程间通信之共享内存
- Linux下进程通信之共享内存
- Linux进程通信之POSIX共享内存
- linux进程间通信之共享内存
- linux进程间通信之共享内存
- Linux进程间通信之共享内存
- linux进程通信之共享内存
- Linux进程通信之POSIX共享内存
- Linux进程通信之POSIX共享内存
- linux进程间通信之共享内存
- Linux进程通信之共享内存
- Linux进程通信之共享内存
- linux 进程通信之 共享内存
- 【Linux】进程间通信之共享内存
- firefox disabled readonly 可用控件
- Android退出时退出所有Activity的通用类,ActivityManager
- 黑马程序员_JQuery选择器小结
- [社会百态] 黑快餐大厨良心发现:大家真别吃了
- 【算法导论】第八章之计数排序
- linux 进程通信之共享内存
- Android手机创建无线热点,共享网络供电脑或者平板,手机等设备上网
- Xcode 4.4.1 + iOS 5.1.1 免证书真机调试
- IIS32位,64位模式下切换
- 给C++初学者的50条忠告
- Android invalidate 与postInvalidate用法(更新视图)
- 【python写批处理】仿dir命令
- Struts2_2.1.6版本的中文问题
- linux定时器Timer的用法