非亲缘进程间的共享内存与信号通信

来源:互联网 发布:什么是优化教育 编辑:程序博客网 时间:2024/05/16 10:52
view plainprint?
  1. /***************************** 
  2.  
  3. 写入数据到共享内存,后再得到另一个进程pid 
  4. 向其发送SIGUSR1信号,另一个进程 
  5. 收到后退出 
  6.  
  7.   ***************************/  
  8.   
  9. #include<sys/shm.h>  
  10. #include<sys/ipc.h>  
  11. #include<stdio.h>  
  12. #include<stdlib.h>  
  13. #include<unistd.h>  
  14. #include<signal.h>  
  15. #include<sys/types.h>  
  16. #include<sys/stat.h>  
  17. #include<string.h>  
  18. #include<errno.h>  
  19. #define SIZE 1024  
  20.   
  21.   
  22. int main(void)  
  23. {  
  24.     sleep(2);  
  25.     char *addr;  
  26.     char buf[50];   
  27.     int shmid;  
  28.   
  29.     printf("please input string to share memory\n");  
  30.     fgets(buf,100,stdin);  
  31.       
  32.     key_t key;  
  33.     key = ftok(".",'a');  
  34.     printf("key = %d\n",(int)key);  
  35.     if((shmid = shmget(key,SIZE,0666|IPC_CREAT)) < 0)  
  36.         perror("shmget");  
  37.     else{  
  38.         printf("get share memory success\n");  
  39.         printf("shmid = %d\n",shmid);  
  40.     }  
  41.   
  42.     addr = shmat(shmid,NULL,0);  
  43.     if( addr == (void *)(-1))  
  44.         perror("shmat");  
  45.     else{  
  46.         printf("get addr success\n");  
  47.         printf("addr = %p\n",addr);  
  48.     }  
  49.   
  50.     strcpy(addr+4,buf);  
  51.       
  52.   
  53.     pid_t pid;  
  54.     strncpy(&pid,addr,4);     
  55.   
  56.   
  57.     kill(pid,SIGUSR1);  
  58.   
  59.     if(shmdt(addr) < 0)  
  60.         perror("shmdt");  
  61.     else  
  62.         printf("detached share memory\n");  
  63.     return 0;  
  64.   
  65. }  

view plainprint?
  1. /************************ 
  2.  
  3.   先写入自己的pid 对方写完数据后 
  4.   向 自己 发送一SIGUSR1信号,收到 
  5.   信号后,读数据后退出. 
  6.  
  7.   **********************/  
  8. #include<sys/shm.h>  
  9. #include<sys/ipc.h>  
  10. #include<stdio.h>  
  11. #include<stdlib.h>  
  12. #include<unistd.h>  
  13. #include<signal.h>  
  14. #include<sys/types.h>  
  15. #include<sys/stat.h>  
  16. #include<string.h>  
  17. #include<errno.h>  
  18. #define SIZE 1024  
  19.   
  20. void get(int sig);  
  21. int main(void)  
  22. {  
  23.     char *addr;  
  24.     char buf[50];  
  25.     int shmid;  
  26.     signal(SIGUSR1,get);  
  27.   
  28.     key_t key;  
  29.   
  30.     key = ftok(".",'a');  
  31.     printf("key = %d\n",key);  
  32.       
  33.     shmid = shmget(key,SIZE,0666|IPC_CREAT);  
  34.     if(shmid < 0)  
  35.         perror("shmget");  
  36.     else{  
  37.         printf("get share memory success\n");  
  38.         printf("shmid = %d\n",shmid);  
  39.     }  
  40.   
  41.   
  42.     addr = shmat(shmid,NULL,0);  
  43.     if(addr == (void *)(-1))  
  44.         perror("shmat");  
  45.     else{  
  46.         printf("get addr success\n");  
  47.         printf("addr = %p\n",addr);  
  48.     }  
  49.   
  50.     pid_t pid = getpid();  
  51.     strncpy(addr,&pid,4);  
  52.   
  53.     pause();  
  54.     strcpy(buf,addr+4);  
  55.     fprintf(stdout,"get message from shm:\n%s\n",buf);  
  56.   
  57.     if(shmdt(addr)<0)  
  58.         perror("shmdt");  
  59.     else  
  60.         printf("detached share memory success\n");  
  61.   
  62.   
  63.      if (shmctl(shmid, IPC_RMID, NULL) == -1)  
  64.           perror("Child: shmctl(IPC_RMID)\n");  
  65.      else  
  66.            printf("Delete shared-memory\n");  
  67.   
  68.   
  69.     return 0;  
  70.   
  71. }  
  72.   
  73. void get(int sig)  
  74. {  


原创粉丝点击