double_pipe.c

来自「这是一个linux下的双管道」· C语言 代码 · 共 54 行

C
54
字号
//这个程序是创建一个双管道#include<stdio.h>#include<stdlib.h>#include<string.h>#include<unistd.h>#include<errno.h>#include<sys/types.h>#include<sys/wait.h>int main(int argc,char **argv){    int pip[2];    char wbuf[100];    char rbuf[100];    pid_t pid;    int pi;    memset(rbuf,0,sizeof(rbuf));    pi=pipe(pip);    if(-1 == pi)     {         printf("pipe error\n");         exit(0);     }    pid=fork();    if(-1 == pid)     {        printf("fork error\n ");        exit(0);     }    if(0 == pid)//第一个读,     {        close(pip[1]);        read(pip[0],rbuf,100);        printf("the child Read rbuf is = %s\n",rbuf);        close(pip[0]);        exit(0);     }    else//第二个写    {       close(pip[0]);       sprintf(wbuf,"I am a student \n");       write(pip[1],wbuf,100);       printf("the father write wbuf is = %s",wbuf);       wait(&pip[0]);       close(pip[1]);           }   exit(0);            }

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?