共享内存实现进程间通信

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

结构体创建:

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

二:input.c

cs 复制代码
#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

cs 复制代码
#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;
}
 
 
相关推荐
酷爱码2 小时前
如何通过python连接hive,并对里面的表进行增删改查操作
开发语言·hive·python
画个大饼2 小时前
Go语言实战:快速搭建完整的用户认证系统
开发语言·后端·golang
喵先生!3 小时前
C++中的vector和list的区别与适用场景
开发语言·c++
Thomas_YXQ3 小时前
Unity3D Lua集成技术指南
java·开发语言·驱动开发·junit·全文检索·lua·unity3d
xMathematics4 小时前
计算机图形学实践:结合Qt和OpenGL实现绘制彩色三角形
开发语言·c++·qt·计算机图形学·cmake·opengl
ShiinaMashirol4 小时前
代码随想录打卡|Day27(合并区间、单调递增的数字、监控二叉树)
java·算法
yuanManGan6 小时前
C++入门小馆: 深入了解STLlist
开发语言·c++
北极的企鹅886 小时前
XML内容解析成实体类
xml·java·开发语言
梁下轻语的秋缘6 小时前
每日c/c++题 备战蓝桥杯(P1049 [NOIP 2001 普及组] 装箱问题)
c语言·c++·学习·蓝桥杯
BillKu6 小时前
Vue3后代组件多祖先通讯设计方案
开发语言·javascript·ecmascript