共享内存实现进程间通信

通过共享内存实现进程间通信,一个进程从终端输入数据,另一个进程打印数据,循环执行,当输入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;
}
 
 
相关推荐
写代码的小球1 小时前
求模运算符c
算法
weixin_472339462 小时前
高效处理大体积Excel文件的Java技术方案解析
java·开发语言·excel
枯萎穿心攻击2 小时前
响应式编程入门教程第二节:构建 ObservableProperty<T> — 封装 ReactiveProperty 的高级用法
开发语言·unity·c#·游戏引擎
Eiceblue4 小时前
【免费.NET方案】CSV到PDF与DataTable的快速转换
开发语言·pdf·c#·.net
m0_555762904 小时前
Matlab 频谱分析 (Spectral Analysis)
开发语言·matlab
学不动CV了4 小时前
ARM单片机启动流程(二)(详细解析)
c语言·arm开发·stm32·单片机·51单片机
大千AI助手5 小时前
DTW模版匹配:弹性对齐的时间序列相似度度量算法
人工智能·算法·机器学习·数据挖掘·模版匹配·dtw模版匹配
浪裡遊5 小时前
React Hooks全面解析:从基础到高级的实用指南
开发语言·前端·javascript·react.js·node.js·ecmascript·php
lzb_kkk6 小时前
【C++】C++四种类型转换操作符详解
开发语言·c++·windows·1024程序员节