linux 进程通信zxy

/*1.创建共享内存,写进程通过键盘不断向内存写入“hello world”;
如果结束写操作,则通过键盘输入“end”
2.读进程从共享内存读取数据,并打印。直到读到“end”为止。

*/
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/shm.h>
#define MAXSIZE 1024
 
struct shm{
    int write;    //记录读进程是否已经将内容读取
    char buffer[MAXSIZE];
};
 
int main()
{
    int shmid;
    struct shm *share;
    void *shmptr = NULL;
    /*
    int shmget(key_t key, size_t size, int flag);
    key: 标识符的规则
    size:共享存储段的字节数
    flag:读写的权限
    返回值:成功返回共享存储的id,失败返回-1
    */
    if((shmid = shmget(0X44,MAXSIZE,0666|IPC_CREAT)) < 0)//得到一个共享内存标识符
        perror("shmget");
    /*
    void *shmat(int shmid, const void *addr, int flag);
    shmid:共享存储的id
    addr:一般为0,表示连接到由内核选择的第一个可用地址上,否则,如果flag没有指定SHM_RND,则连接到addr所指定的地址上,如果flag为SHM_RND,则地址取整
    flag:如前所述,一般为0
    返回值:如果成功,返回共享存储段地址,出错返回-1
    */
    if((shmptr = shmat(shmid,0,0)) == (void *)-1)//把共享内存区对象映射到调用进程的地址空间
        perror("shmat");
 
    printf("This is the read process!!!\n");
    share = (struct shm *)shmptr;//指向同一个地址空间
    while(1)
    {
        if(share->write != 0)
        {
            if(!strncmp(share->buffer,"end",3) == 0)//将share->buffer和"end"的前三个字节进行比较,如果==0就代表两个字符相等
            {
                printf("%s",share->buffer);//从共享内存中输出写的内容
                share->write = 0;//重置保证在循环里面
            }
            else
                break;
        }
    }
 
    if(shmdt(shmptr) < 0)//断开共享内存连接
        perror("shmdt");
 
    exit(0);
}

/*1.创建共享内存,写进程通过键盘不断向内存写入“hello world”;
如果结束写操作,则通过键盘输入“end”
2.读进程从共享内存读取数据,并打印。直到读到“end”为止。

*/
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/shm.h>
#define MAXSIZE 1024
struct shm{
    int write;        //记录读进程是否已经将内容读取
    char buffer[MAXSIZE];
};
 
int main()
{
    int shmid;
    void *shmptr = NULL;
    char str[MAXSIZE];    //存储输入的内容
    struct shm *share;
 
    if((shmid = shmget(0X44,MAXSIZE,0666|IPC_CREAT)) < 0)//得到一个共享内存标识符
        perror("shmget");
    if((shmptr = shmat(shmid,0,0)) == (void *)-1)//把共享内存区对象映射到调用进程的地址空间
        perror("shmat");
 
    printf("This is the write process!!!\n");
    share = (struct shm *)shmptr;//指向同一个地址空间
    while(1)
    {
        if(share->write == 1)//如果读进程未启动
        {
            sleep(1);
            printf("Waiting the read process!!!\n");
        }
 
        printf("please input hello world or input end\n");
        fgets(str,MAXSIZE,stdin);//获取从键盘输入的内容
        sprintf(share->buffer,"%s",str);//将输入的内容存到共享内存中
        share->write = 1;//重置保证能写入到共享内存里面        
 
        if(strncmp(str,"end",3) == 0)//如果输入end就跳出
            break;
        sleep(1);
    }
    if(shmdt(shmptr) < 0)//断开共享内存连接
        perror("shmdt");
    exit(0);
}

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值