标签:
调试了下午,终于调通啦! 运行./c.out 输出共享内存中的内容,运行 ./c.out arg1 对共享内存区进行修改
下面先贴上main的代码:
#include <signal.h> //head file of define signal #include <pthread.h> #include <static_lib.h> #include <errno.h> #include <fcntl.h> #include <unistd.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/types.h> #include <sys/mman.h> extern int create_shm(char **shmptr); int main(int argc,char* argv[]) { //IPC share memory char *shmptr; if(create_shm(&shmptr) == -1) { printf("create_shm error \n"); return -1; } if(argc > 1) { //do the second thing for share memory while(1) { printf("input str to share memory:"); gets(shmptr); } } else { memcpy(shmptr,"hello",5); while(1) { sleep(2); printf("share memory is %s \n",shmptr); } } return 0; }
下面是共享内存创建的代码:
运行效果:
标签:
原文地址:http://www.cnblogs.com/mhl2018/p/4536547.html