找回密码
 立即注册
首页 资源区 代码 共享内存和信号结合练习

共享内存和信号结合练习

吉娅寿 2025-5-29 10:54:48
设计三个程序,三个程序申请一块共享内存,并分别映射到各自进程的地址空间,进程A和进程B对共享内存段中的数据进行修改,然后进程B不断输出共享内存段中的数据,实现进程间的互斥,避免竞争。
  1. /********************************************************************************
  2. *
  3. *
  4. * 使用信号对共享内存进行互斥访问
  5. * author:jindouliu2024@163.com
  6. * date:2025.5.8
  7. *
  8. *
  9. * Copyright (c)  2024-2025   jindouliu2024@163.com   All right Reserved
  10. * ******************************************************************************/
复制代码
  1. //进程A
  2. #include <unistd.h>
  3. #include <sys/types.h>
  4. #include <sys/stat.h>
  5. #include <fcntl.h>
  6. #include <sys/wait.h>
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <string.h>
  10. #include <errno.h>
  11. #include <time.h>
  12. #include <sys/ipc.h>
  13. #include <sys/stat.h>
  14. #include <unistd.h>
  15. #include <sys/ipc.h>
  16. #include <sys/shm.h>
  17. char *shm_map;
  18. int id,i=0;
  19. void sig_handler(int signal)
  20. {
  21.         //如果是接受到的信号是SIGUSR1,则执行
  22.         if(signal == SIGUSR1){
  23.                 i++;
  24.                 //向内存写入数据
  25.                 sprintf(shm_map,"shm_a.c data = %d\n",i);
  26.                 printf("请输入进程id:");
  27.                 scanf("%d",&id);
  28.                 //给进程B发送信号
  29.                 kill(id,SIGUSR2);
  30.         }
  31. }
  32. int main()
  33. {
  34.        
  35.         //设置键值
  36.         key_t key = ftok(".",2);
  37.         //创建一块共享内存空间
  38.         int shm_id = shmget(key,256,IPC_CREAT|IPC_EXCL|0644);
  39.         if(shm_id == -1){
  40.                 printf("shmget error\n");
  41.                 shm_id = shmget(key,256,0644);
  42.                
  43.         }
  44.        
  45.         //连接映射空间,并写入数据
  46.         shm_map = (char *)shmat(shm_id,NULL,0);
  47.         printf("running a\n");
  48.        
  49.        
  50.         signal(SIGUSR1,sig_handler);
  51.         while(1);
  52.        
  53.        
  54.         return 0;
  55. }
复制代码
  1. //进程B
  2. #include <unistd.h>
  3. #include <sys/types.h>
  4. #include <sys/stat.h>
  5. #include <fcntl.h>
  6. #include <sys/wait.h>
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <string.h>
  10. #include <errno.h>
  11. #include <time.h>
  12. #include <sys/ipc.h>
  13. #include <sys/stat.h>
  14. #include <unistd.h>
  15. #include <sys/ipc.h>
  16. #include <sys/shm.h>
  17. char *shm_map;
  18. int id;
  19. //信号处理函数
  20. void sig_handler(int signal)
  21. {
  22.         //如果是接受到的信号是SIGUSR2,则执行
  23.         if(signal == SIGUSR2){
  24.                 //从内存中读取数据
  25.                 printf("%s\n",shm_map);
  26.         }
  27.         printf("请输入进程id:");
  28.         scanf("%d",&id);
  29.         //给进程A或者进程B发送信号
  30.         kill(id,SIGUSR1);
  31. }
  32. int main()
  33. {
  34.         //设置键值
  35.         key_t key = ftok(".",2);
  36.         //创建一块共享内存空间
  37.         int shm_id = shmget(key,256,IPC_CREAT|IPC_EXCL|0644);
  38.         if(shm_id == -1){
  39.                 printf("shmget error\n");
  40.                 shm_id = shmget(key,256,0644);
  41.                
  42.         }
  43.         //连接映射空间,并读取数据
  44.         shm_map = (char *)shmat(shm_id,NULL,0);
  45.         printf("请输入进程id:");
  46.         scanf("%d",&id);
  47.         kill(id,SIGUSR1);
  48.         signal(SIGUSR2,sig_handler);
  49.         while(1){
  50.                
  51.         }
  52.        
  53.         shmdt(shm_map);
  54.         return 0;
  55. }
复制代码
  1. //进程C
  2. #include <unistd.h>
  3. #include <sys/types.h>
  4. #include <sys/stat.h>
  5. #include <fcntl.h>
  6. #include <sys/wait.h>
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <string.h>
  10. #include <errno.h>
  11. #include <time.h>
  12. #include <sys/ipc.h>
  13. #include <sys/stat.h>
  14. #include <unistd.h>
  15. #include <sys/ipc.h>
  16. #include <sys/shm.h>
  17. char *shm_map;
  18. int id,i=100;
  19. void sig_handler(int signal)
  20. {
  21.         //如果是接受到的信号是SIGUSR1,则执行
  22.         if(signal == SIGUSR1){
  23.                 i--;
  24.                 //向内存写入数据
  25.                 sprintf(shm_map,"shm_a.c data = %d\n",i);
  26.                 printf("请输入进程id:");
  27.                 scanf("%d",&id);
  28.                 //给进程B发送信号
  29.                 kill(id,SIGUSR2);
  30.         }
  31. }
  32. int main()
  33. {
  34.        
  35.         //设置键值
  36.         key_t key = ftok(".",2);
  37.         //创建一块共享内存空间
  38.         int shm_id = shmget(key,256,IPC_CREAT|IPC_EXCL|0644);
  39.         if(shm_id == -1){
  40.                 printf("shmget error\n");
  41.                 shm_id = shmget(key,256,0644);
  42.                
  43.         }
  44.        
  45.         //连接映射空间,并写入数据
  46.         shm_map = (char *)shmat(shm_id,NULL,0);
  47.         printf("running c\n");
  48.        
  49.        
  50.         signal(SIGUSR1,sig_handler);
  51.         while(1);
  52.        
  53.        
  54.         return 0;
  55. }
复制代码
来源:程序园用户自行投稿发布,如果侵权,请联系站长删除
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
您需要登录后才可以回帖 登录 | 立即注册