有名管道---实现两个人之间相互通信

简介: 有名管道---实现两个人之间相互通信

方法一:使用两个管道实现两个.c文件,本文只提供了一个.c另一个.c文件copy过去父子进程的收发交换一下即可。

代码:

#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <sys/wait.h>
#include <fcntl.h>
#include <stdlib.h>
#define PATH1 "/tmp/fifo"
#define PATH2 "/tmp/fifo1"
int main()
{
    if (access(PATH1, F_OK))
    {
        umask(0000);
        //创建管道文件
        mkfifo(PATH1, 0777);
    }
    if (access(PATH2, F_OK))
    {
        umask(0000);
        //创建管道文件
        mkfifo(PATH2, 0777);
    }
    //打开管道文件
    int fd1 = open(PATH1, O_RDWR);
    if (fd1 == -1)
    {
        perror("open failed");
        exit(-1);
    }
    int fd2 = open(PATH2, O_RDWR);
    if (fd2 == -1)
    {
        perror("open failed");
        exit(-1);
    }
    pid_t ret = fork(); //创建进程
    if (ret == -1)
    {
        perror("fork");
        exit(-1);
    }
    else if (ret == 0) //子进程
    {
        char msg_Tom[32] = {0};
        while (1)
        {
            memset(msg_Tom, 0, 32);
            scanf("%s", msg_Tom);
            write(fd1, msg_Tom, strlen(msg_Tom));
        }
    }
    else //父进程
    {
        char msg_Tom[32] = {0};
        while (1)
        {
            memset(msg_Tom, 0, 32);
            printf("Jack:");
            read(fd2, msg_Tom, 32);
            printf("%s\n", msg_Tom);
        }
    }
    close(fd1);
    close(fd2);
    return 0;
}

方法二:使用一个管道实现:

#include <stdio.h>
#include <signal.h>
#include <stdlib.h>//exit
#include <string.h>//memset
#include <errno.h>
#include <sys/types.h>//fork
#include <sys/wait.h>//wait
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>//fork pipe

#define FIFO_PATH "/tmp/my_fifo"
#define MSG_LENGTH  20

int Fifo_Spreak_Init();//通信初始化  --- 创建管道
int Fifo_Spreakint(int fifo_fd);//进行通信
int Fifo_Spreak_Free();//通信释放

int Fifo_Spreak_Init()
{
    if(access(FIFO_PATH,F_OK))
    {
        umask(0000);
        mkfifo(FIFO_PATH,0777);
    }

    int fifo_fd = open(FIFO_PATH,O_RDWR);
    if(fifo_fd == -1)
    {
        perror("open");
        return -1;
    }
    return fifo_fd;
}

int Fifo_Spreakint(int fifo_fd)//进行通信
{
    int pid = fork();
    if(pid == -1)
    {
        perror("fork");
        return -1;
    }
    else if(pid == 0)
    {
        char msg_data[MSG_LENGTH];
        while(1)
        {
            memset(msg_data,0,MSG_LENGTH);
            read(fifo_fd,msg_data,MSG_LENGTH);
            printf("小红说:%s\n",msg_data);
        }
    }
    else
    {
        char msg_data[MSG_LENGTH];
        while(1)
        {
            memset(msg_data,0,MSG_LENGTH);
            printf("请输入要发送给小红的信息:");
            scanf("%s",msg_data);
           
            kill(pid,SIGSTOP);//system("kill -19 ");
            sleep(1);
            write(fifo_fd,msg_data,strlen(msg_data));
            sleep(1);
            kill(pid,SIGCONT);
            printf("唤醒子进程!\n");
        }  
    }
    return 0;
}

int Fifo_Spreak_Free(int fifo_fd)//通信释放
{
    close(fifo_fd);
    return 0;
}

int main()
{
    int fifo_fd = Fifo_Spreak_Init();
    if(fifo_fd == -1)
    {
        printf("管道通初始化失败\n");
        return -1;
    }
    Fifo_Spreakint(fifo_fd);//进行通信
    Fifo_Spreak_Free(fifo_fd);
    return 0;
}


相关文章
|
9天前
|
域名解析 人工智能 Linux
|
19天前
|
Linux Go 开发工具
手机上跑 DeepSeek 智能体:开源安卓客户端 DSH Go 安装与使用指南
DSH Go 是一款开源安卓客户端,将 DeepSeek Harness 智能体引擎装进手机:免 Root、免 Termux,一键安装即用;支持本机运行(内置 Ubuntu+Node.js)或远程接管电脑会话;具备插话打断、过程折叠、屏幕操控、端到端加密分享等特色功能,真正实现智能体“口袋化”。
17711 63
|
17天前
|
人工智能
Google 提示「所在地区不可用」?进不去 Antigravity 或 Gemini,先查账号这个设置
进不去 Antigravity 或 Gemini,提示所在地区不可用?三步查清 Google 账号关联地区、官方更正表单理由选法与改区注意事项。
Google 提示「所在地区不可用」?进不去 Antigravity 或 Gemini,先查账号这个设置
|
3天前
|
人工智能 安全 网络安全
|
7天前
|
云安全 数据采集 人工智能
|
8天前
|
Linux API iOS开发
【DSH】DeepSeek Harness新手保姆教程(下载+安装+配置+使用,2026最新实测)
DeepSeek Harness(dsh)是 DeepSeek AI 开源的智能体框架,秉持“一切皆插件”理念,支持读文件、改代码、执行命令、处理表格等自动化任务;免费开源,兼容 Win/macOS,需配 API 密钥使用。(239字)
1398 5
|
14天前
|
人工智能 开发工具 开发者
2026最新codex下载安装保姆级使用教程(下载+安装+登录+使用,含所有历史版本)
Codex 是 OpenAI 推出的开源 AI 编程智能体,跑在本地终端和桌面客户端里,能读懂整个代码仓库,一句自然语言就能让它改代码、执行命令、跑测试、修 Bug,支持 Windows、macOS、Linux 三大平台。本文把 Codex下载、安装、登录、汉化、基础使用一次讲全,并整理了国内用户最常踩的坑,跟着做 10 分钟就能跑起来。
2446 8
|
19天前
|
弹性计算 人工智能 API
Codex超详细入门指南(下载·安装·登录·上手,全版本覆盖,2026最新版)
建议收藏本教程,2026 全年各版本更新随时回来查阅。如果你在搭建过程中遇到任何问题,欢迎结合 阿里云工单系统 提交技术支持请求,或在 阿里云开发者社区 搜索 Codex 相关话题交流。 祝你编码愉快,效率翻倍!
3968 22
|
21天前
|
Linux API iOS开发
【DSH】DeepSeek Harness新手保姆教程(下载+安装+配置+使用,2026最新实测)
DeepSeek Harness(dsh)是DeepSeek AI开源的智能体框架,秉持“一切皆插件”理念,支持读文件、改代码、跑命令、处理表格、联网搜索等操作。免费开源,兼容Windows/macOS/Linux,提供桌面端与网页端,可灵活安装插件扩展能力。

热门文章

最新文章