共享内存实现进程间通信

通过共享内存实现进程间通信,一个进程从终端输入数据,另一个进程打印数据,循环执行,当输入quit时循环结束
思路:构建建构体,通过两个进程实现,input.c和output.c
          联合编译:汇编:gcc 生成

结构体创建:

struct data
{
    int flag;
    char buf[32];
};

 二:input.c

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

struct data
{
    int flag;
    char buf[32];
};

int main(int argc, char const *argv[])
{
    // key_t ftok(const char *pathname, int proj_id); int proj_id:整数型的低八位二进制数
    key_t key;
    int shmid;
    if ((key = ftok("./yue", 'a')) < 0)//创建key值,文件名和整数型的低八位数值
    {
        perror("ftok err");
        return -1;
    }
    // printf("key的值:%#x\n", key);//key值打印
    //  int shmget(key_t key, size_t size, int shmflg);//创建或者打开共享文件
    shmid = shmget(key, 64, IPC_CREAT | IPC_EXCL | 0666);
    if (shmid <= 0)
    {
        if (errno == EEXIST)//文件存在时,重新打开共享内存
        {
            shmid = shmget(key, 64, 0666);
        }
        else
        {
            perror("shmget err");
            return -1;
        }
    }
    // printf("%d\n", shmid);
    //  void *shmat(int shmid, const void *shmaddr, int shmflg);//映射共享内存,即把指定的共享内存映射到进程的地址空间用于访问
    //  定义指针变量接收返回值
    struct data *p;
    p = (struct data *)shmat(shmid, NULL, 0);
    if (p == (struct data *)-1)
    {
        perror("shmat err\n");
        return -1;
    }
    p->flag = 0;
    while (1)
    {
        scanf("%s", p->buf);
        p->flag = 1;
        if (!strcmp(p->buf, "quit"))
            break;
    }
    //   int shmdt(const void *shmaddr);//取消映射
    shmdt(p);
    return 0;
}

三:output.c

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

struct data
{
    int flag;
    char buf[32];
};

int main(int argc, char const *argv[])
{
    // key_t ftok(const char *pathname, int proj_id); int proj_id:整数型的低八位二进制数
    key_t key;
    int shmid;
    if ((key = ftok("./yue", 'a')) < 0)
    {
        perror("ftok err");
        return -1;
    }
    // printf("key的值:%#x\n", key);
    // int shmget(key_t key, size_t size, int shmflg);
    shmid = shmget(key, 64, IPC_CREAT | IPC_EXCL | 0666);
    if (shmid <= 0)
    {
        if (errno == EEXIST)
        {
            shmid = shmget(key, 64, 0666);
        }
        else
        {
            perror("shmget err");
            return -1;
        }
    }
    //  printf("%d\n", shmid);
    // void *shmat(int shmid, const void *shmaddr, int shmflg);
    // 定义指针变量接收返回值
    struct data *p;
    p = (struct data *)shmat(shmid, NULL, 0);
    if (p == (struct data *)-1)
    {
        perror("shmat err\n");
        return -1;
    }
    while (1)
    {
        // sleep(2);
        if (p->flag == 1)
        {
            if (!strcmp(p->buf, "quit"))
                break;
            printf("%s\n", p->buf);
            p->flag = 0;
        }
    }
    //   int shmdt(const void *shmaddr);取消映射
    shmdt(p);
    // 阻塞,观察共享内存的状态
    //getchar();
    //  int shmctl(int shmid, int cmd, struct shmid_ds *buf);
    shmctl(shmid, IPC_RMID, NULL);
    return 0;
}
 
 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值