📌  相关文章
📜  使用管道在 C 中子进程和父进程之间进行双向通信 - C 编程语言代码示例

📅  最后修改于: 2022-03-11 15:04:40.581000             🧑  作者: Mango

代码示例1
#include 
#include
#include 
#include
int main()
{
    pid_t pid;
    int r;
    char *ch=NULL;
    char *ch1=NULL;
    int readpipe[2];
    int writepipe[2];
    int a;
    int b;
    a=pipe(readpipe);
    b=pipe(writepipe);
      // check a and b

    pid=fork();
    // check pid

    if(pid==0)
    { //CHILD PROCESS
            close(readpipe[1]);
            close(writepipe[1]);
            read(readpipe[0],ch,sizeof(ch));
            printf("\nREAD = %s",ch);
            close(readpipe[0]);
            ch1="YES";
            write(writepipe[1],ch1,sizeof(ch1));
            close(writepipe[1]);
    }
    else
    { //PARENT PROCESS
            close(writepipe[0]);
            close(writepipe[1]);
            ch="HI!! YOU THERE";
            write(readpipe[1],ch,sizeof(ch));
            close(readpipe[1]);
            read(writepipe[1],ch1,sizeof(ch1));
            printf("\nACK RECEIVED %s",ch1);
            close(writepipe[1]);
    }
    return 0;
}