Linux高级编程基础——进程间通信之共享内存

进程间通信之共享内存

  1. 创建共享内存,写进程每隔2秒向内存写入一次“hello world”;
    如果结束写操作,则写进程写入“end”;

  2. 读进程从共享内存读取数据,并打印。直到读到“end”为止。

                     这是写进程
    


#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>

#define SIZE 2048

typedef struct{
   char xinxi[20];
 } xx;

int main()
{
 xx *point;
 key_t key;
 key = ftok(".",6);
 int shmid;

 if ((shmid = shmget (key,SIZE,IPC_CREAT)) <0 )   { perror("shmget"); exit (1);  }
 else 
     printf ("creat ok,shmid is :%d \n",shmid);
 
 point = (xx*)shmat(shmid,0,0);

 char sc[3]="a";
 char yes[3] = "y";
 int n=1,i=0;
 for (n = 1; n >= 1;)
   {
      printf ("是否结束写操作? \n");
      sc
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值