2.12 Father and child processes communicate through anonymous pipes

table of Contents

1.pipe function

2. Check the size of the pipeline buffer


1.pipe function

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

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

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

Program example 1: The child process sends once, the parent process receives once

#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;
}

Program example 2: The child process keeps sending, the parent process keeps receiving

#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;
}

Program example 3: The child process keeps sending data, and the parent process receives the data; the parent process also keeps sending data, and the child process receives the data

#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. Check the size of the pipeline buffer

Command mode:

As you can see from the picture below, it is 4K

Function method:

#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;
}


Reference: Niuke.com C++ high-paying job search project "Linux High Concurrency Server Development" 2.12 father and son processes communicate through anonymous channels

Exclusive offer link:

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

Guess you like

Origin blog.csdn.net/m0_38062470/article/details/113827454