共享内存实现进程间通信

通过共享内存实现进程间通信,一个进程从终端输入数据,另一个进程打印数据,循环执行,当输入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;
}
 
 
相关推荐
2401_8956108211 分钟前
Java 后端基础 Maven
java·开发语言·maven
LAM LAB13 分钟前
【VBA/word】批量替换字体大小
开发语言·c#·word
zzc92115 分钟前
怎么用Origin画出MATLAB效果的3D时频图
开发语言·matlab
小峰编程17 分钟前
Python函数——万字详解
linux·运维·服务器·开发语言·前端·网络·python
芯眼30 分钟前
正点原子STM32新建工程
开发语言·c++·stm32·单片机·软件工程
海盐泡泡龟33 分钟前
Javascript本地存储的方式有哪些?区别及应用场景?(含Deep Seek讲解)
开发语言·javascript·ecmascript
alphaTao42 分钟前
LeetCode 每日一题 2025/5/12-2025/5/18
算法·leetcode
helloworld工程师1 小时前
Java实现PDF加水印功能:技术解析与实践指南
java·开发语言·pdf
五花肉村长1 小时前
Linux-进程信号
linux·运维·服务器·开发语言·网络·c++
半青年2 小时前
Qt读取Excel文件的技术实现与最佳实践
c语言·c++·python·qt·c#·excel