Linux共享内存

本文介绍了一个使用C语言实现的生产者-消费者模型示例,通过共享内存进行进程间通信。两个程序分别扮演生产者和消费者角色,展示了如何利用共享内存交换数据。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. #include <stdio.h>  
  2. #include <string.h>  
  3. #include <semaphore.h>  
  4. #include <sys/shm.h>  
  5. #include <unistd.h>  
  6. #include <fcntl.h>  
  7.   
  8. #define BUFFER_SIZE 4  
  9.   
  10. int main()  
  11. {  
  12.     int shmid = shmget((key_t)1234, sizeof(int) * BUFFER_SIZE, 0644 | IPC_CREAT);  
  13.       
  14.     if ( -1 == shmid )  
  15.     {  
  16.         printf("shmget failed!\n");  
  17.         return 0;  
  18.     }  
  19.       
  20.     void *shm = shmat(shmid, NULL, 0);  
  21.     if ((void *)-1 == shm)  
  22.     {  
  23.         printf("shmat failed!\n");  
  24.         return 0;  
  25.     }  
  26.       
  27.     int *sharedbuffer = (int *)shm;  
  28.     memset(sharedbuffer, -1, sizeof(int) * BUFFER_SIZE);  
  29.       
  30.     int index = 0;  
  31.     while (index <= 20)  
  32.     {  
  33.         for (int i = 0; i < 4; i++)  
  34.         {  
  35.             if (-1 == sharedbuffer[i])  
  36.             {  
  37.                 sharedbuffer[i] = ++index;  
  38.                 printf("product: %d\n", sharedbuffer[i]);  
  39.                 break;  
  40.             }  
  41.         }  
  42.     }  
  43.     if (-1 == shmdt((void *)sharedbuffer))  
  44.     {  
  45.         printf("shmdt failed!\n");  
  46.     }  
  47. }  


[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. #include <stdio.h>  
  2. #include <string.h>  
  3. #include <semaphore.h>  
  4. #include <sys/shm.h>  
  5. #include <unistd.h>  
  6. #include <fcntl.h>  
  7.   
  8. #define BUFFER_SIZE 4  
  9.   
  10. int main()  
  11. {  
  12.     int shmid = shmget((key_t)1234, sizeof(int) * BUFFER_SIZE, 0644 | IPC_CREAT);  
  13.       
  14.     if ( -1 == shmid )  
  15.     {  
  16.         printf("shmget failed!\n");  
  17.         return 0;  
  18.     }  
  19.       
  20.     void *shm = shmat(shmid, NULL, 0);  
  21.     if ((void *)-1 == shm)  
  22.     {  
  23.         printf("shmat failed!\n");  
  24.         return 0;  
  25.     }  
  26.       
  27.     int *sharedbuffer = (int *)shm;  
  28.        
  29.     int run = 1;  
  30.     while (run)  
  31.     {  
  32.         for (int i = 0; i < 4; i++)  
  33.         {  
  34.             if (-1 != sharedbuffer[i])  
  35.             {  
  36.                 printf("consumer: %d\n", sharedbuffer[i] );  
  37.                 if ( 20 == sharedbuffer[i] )  
  38.                 {  
  39.                     run = 0;  
  40.                 }  
  41.                 sharedbuffer[i] = -1;  
  42.                 break;  
  43.             }  
  44.         }  
  45.     }  
  46.     if (-1 == shmdt((void *)sharedbuffer))  
  47.     {  
  48.         printf("shmdt failed!\n");  
  49.     }  
  50. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值