2.12父子进程通过匿名管道通信

目录

1.pipe函数

2.查看管道缓冲大小


1.pipe函数

#include <unistd.h>
int pipe(int pipefd[2]);
    功能:创建一个匿名管道,用来进程间通信
    参数:int pipefd[2],这是数组是一个传出参数
        pipefd[0] 对应的是管道的读端
        pipefd[1] 对应的是管道的写端
    返回值:
        成功:0
        失败:-1

管道默认是阻塞的:如果管道中没有数据,read阻塞;如果管道满了,write阻塞

注意:匿名进程只能用于具有血缘关系的进程之间的通信(父子进程、兄弟进程等)

程序示例1:子进程发送一次,父进程接收一次

#include <stdio.h>
#include <unistd.h>
#include <string.h>
//子进程发送数据给父进程,父进程读取到数据后输出
int main()
{
    //在fork之前创建管道
    int pipefd[2];
    int ret=pipe(pipefd);
    if(ret==-1)
    {
        perror("pipe");
        return -1;
    }

    //创建子进程
    pid_t pid=fork();
    if(pid>0)
    {
        //父进程 从管道的读取端读取数据
        char buf[1024]={0};
        int len=read(pipefd[0],buf,sizeof(buf));
        printf("parent recv :%s,pid=%d\n",buf,getpid());
    }
    else if(pid==0)
    {
        //子进程 向管道的写入端写入数据
        //sleep(10);  //如果有这句话,子进程会休眠10秒再写入,父进程会一直等待直到子进程将数据写入再读取
        char *str="hello,I am the child";
        write(pipefd[1],str,strlen(str));
    }
    return 0;
}

程序示例2:子进程不断发送,父进程不断接收

#include <stdio.h>
#include <unistd.h>
#include <string.h>
//子进程发送数据给父进程,父进程读取到数据后输出
int main()
{
    //在fork之前创建管道
    int pipefd[2];
    int ret = pipe(pipefd);
    if (ret == -1)
    {
        perror("pipe");
        return -1;
    }

    //创建子进程
    pid_t pid = fork();
    if (pid > 0)
    {
        printf("parent process,pid=%d\n", getpid());
        //父进程 从管道的读取端读取数据
        char buf[1024] = {0};
        while (1)
        {
            int len = read(pipefd[0], buf, sizeof(buf));
            printf("parent recv :%s,pid=%d\n", buf, getpid());
        }
    }
    else if (pid == 0)
    {
        //子进程
        printf("child process,pid=%d\n", getpid());
        while (1)
        { //向管道的写入端写入数据
            char *str = "hello,I am the child";
            write(pipefd[1], str, strlen(str));
            sleep(1);
        }
    }
    return 0;
}

程序示例3:子进程不断发送数据,父进程接收该数据;父进程也不断发送数据,子进程接收该数据

#include <stdio.h>
#include <unistd.h>
#include <string.h>
//子进程发送数据给父进程,父进程读取到数据后输出
int main()
{
    //在fork之前创建管道
    int pipefd[2];
    int ret = pipe(pipefd);
    if (ret == -1)
    {
        perror("pipe");
        return -1;
    }

    //创建子进程
    pid_t pid = fork();
    if (pid > 0)
    {
        printf("parent process,pid=%d\n", getpid());
        //父进程 从管道的读取端读取数据
        char buf[1024] = {0};
        while (1)
        {
            //读取数据
            int len = read(pipefd[0], buf, sizeof(buf));
            printf("parent recv :%s,pid=%d\n", buf, getpid());
            bzero(buf,1024);

            //写入数据
            char* str="hello,I am parent";
            write(pipefd[1],str,strlen(str));
            sleep(1);
        }
    }
    else if (pid == 0)
    {
        //子进程
        printf("child process,pid=%d\n", getpid());
        char buf[1024]={0};
        while (1)
        { //向管道的写入端写入数据
            char *str = "hello,I am child";
            write(pipefd[1], str, strlen(str));
            sleep(1);

            //读取数据
            read(pipefd[0],buf,sizeof(buf));
            printf("child recv :%s,pid=%d\n", buf, getpid());
            bzero(buf,1024);
        }
    }
    return 0;
}

//注:父进程和子进程的读写顺序要相反,不然两个都会阻塞,不能运行下去


2.查看管道缓冲大小

命令方式:

从下图可知,为4K

函数方式:

#include <stdio.h>
#include <unistd.h>
#include <string.h>

int main()
{
    int pipefd[2];
    pipe(pipefd);
    //获取管道的大小

    long ret=fpathconf(pipefd[0],_PC_PIPE_BUF);
    printf("管道大小:%ld\n",ret);
    ret=fpathconf(pipefd[0],_PC_PIPE_BUF);
    printf("管道大小:%ld\n",ret);

    return 0;
}


参考:牛客网 C++高薪求职项目《Linux高并发服务器开发》2.12父子进程通过匿名管道通信

专属优惠链接:

https://www.nowcoder.com/courses/cover/live/504?coupon=AvTPnSG

  • 3
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
TensorFlow 2.12是一个机器学习框架的版本。要安装TensorFlow 2.12,你可以按照以下步骤进行操作: 1. 直接安装TensorFlow:从2022年12月起,tensorflow-gpu已经合并到tensorflow包中了。你可以使用以下命令安装TensorFlow 2.12.0版本: ``` pip install tensorflow==2.12.0 -i https://pypi.tuna.tsinghua.edu.cn/simple ``` 2. 激活环境:如果你使用的是conda环境,可以使用以下命令激活tensorflow2_cpu环境: ``` conda activate tensorflow2_cpu ``` 3. 安装TensorFlow 2.12.0(CPU版本):如果你想安装CPU版本的TensorFlow 2.12.0,可以使用以下命令: ``` pip install tensorflow-cpu==2.12.0 -i https://pypi.tuna.tsinghua.edu.cn/simple/ ``` 4. 适配notebook:如果你想在Jupyter Notebook中使用TensorFlow 2.12.0,可以使用以下命令安装ipykernel并将其适配到tensorflow2_cpu环境: ``` pip install ipykernel python -m ipykernel install --name tensorflow2_cpu ``` 需要注意的是,截至到写这篇回答时(2023.4.4),国内的pip源可能还没有更新TensorFlow 2.12.0的包名,所以建议使用官方源或者等待一段时间再尝试安装。如果安装出错,可以多次尝试,可能是因为网络不好导致的。 #### 引用[.reference_title] - *1* [【已解决】【Tensorflow2.12.0版本以后合并CPU和GPU版】Tensorflow-gpu==2.12.0 安装失败解决办法](https://blog.csdn.net/void_zk/article/details/131379478)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down1,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* *3* [tensorflow1 tensorflow 2 安装配置(cpu+gpu)新版gpu2.12](https://blog.csdn.net/ziqibit/article/details/129959068)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值