共享内存实现进程间通信

通过共享内存实现进程间通信,一个进程从终端输入数据,另一个进程打印数据,循环执行,当输入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;
}
 
 
相关推荐
曼巴UE53 分钟前
UE5.3 C++ TArray系列(一)
开发语言·c++·ue5
熬夜苦读学习14 分钟前
Linux文件系统
linux·运维·服务器·开发语言·后端
菜鸟一枚在这22 分钟前
深度解析建造者模式:复杂对象构建的优雅之道
java·开发语言·算法
gyeolhada40 分钟前
2025蓝桥杯JAVA编程题练习Day5
java·数据结构·算法·蓝桥杯
阿巴~阿巴~41 分钟前
多源 BFS 算法详解:从原理到实现,高效解决多源最短路问题
开发语言·数据结构·c++·算法·宽度优先
给bug两拳1 小时前
Day9 25/2/22 SAT
算法
_Itachi__2 小时前
LeetCode 热题 100 73. 矩阵置零
算法·leetcode·矩阵
waicsdn_haha2 小时前
Visual Studio Code 2025 安装与高效配置教程
c语言·ide·windows·vscode·微软·编辑器·win7
夏末秋也凉2 小时前
力扣-贪心-376 摆动序列
算法·leetcode