管道通信之无名管道---pipe()

时间:2023-03-08 16:12:23
管道通信之无名管道---pipe()

pipe()函数在子进程产生之前就应该存在。

  • 父子进程之间只进行一次传递
     /*============================================
    > Copyright (C) 2014 All rights reserved.
    > FileName:onepipe.c
    > author:donald
    > details:
    ==============================================*/
    #include <unistd.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #define N 512
    int main(int argc, const char *argv[])
    {
    int pipefd[];
    pid_t pid; if(pipe(pipefd) == -){
    perror("pipe failed");
    exit(-);
    }
    printf("%u\n",pid);
    pid == fork();//这里一个个大大的bug,自己的误操作,debug了很久才搞定了
    printf("%u\n",pid);
    if( == pid){
    close(pipefd[]);//0 read 1 write
    //一个约定,父子进程都需遵守 char buf[N];
    memset(buf,,N);
    read(pipefd[],buf,N);
    printf("child read:%s\n",buf); printf("child exit\n");
    exit();
    }else{
    close(pipefd[]);//0 read
    char line[N];
    printf("parent begin\n"); memset(line,,N);
    fgets(line,N,stdin); write(pipefd[],line,strlen(line));
    printf("parent exit\n");
    wait(NULL);//等待子进程的结束
    }
    return ;
    }
  • 父子进程通过管道,进行多次读写操作,先贴上一个比较奇葩的方法(就是一个错误):

     /*============================================
    > Copyright (C) 2014 All rights reserved.
    > FileName:my_pipe.c
    > author:donald
    > details:
    ==============================================*/
    #include <unistd.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #define N 1024
    int main(int argc, const char *argv[])
    {
    int fds[];
    if(pipe(fds) == -){//只能有一对进行读写
    perror("failed");
    exit();
    }
    pid_t pid = fork();
    if(pid == -){
    perror("error");
    exit();
    } while(){
    if(pid == ){//child read
    close(fds[]);//1 write
    char buf[] = "";
    read(fds[],buf,);//只能有一个读,
    printf("child read:%s\n",buf);
    //exit(1);
    }else{//parent write close(fds[]);//0 read
    // char *p = "hello,donald";
    char line[N];
    // memset(line,0,N);
    fgets(line,N,stdin);
    write(fds[],line,strlen(line));
    //wait(NULL);
    }
    }
    return ;
    }

    再贴上正确的方法:

     /*============================================
    > Copyright (C) 2014 All rights reserved.
    > FileName:twopipe.c
    > author:donald
    > details:
    ==============================================*/
    #include <unistd.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #define N 512
    int main(int argc, const char *argv[])
    {
    int pipefd[];
    pid_t pid;
    //pid = fork(); if(pipe(pipefd) == -){
    perror("pipe failed");
    exit(-);
    }
    pid = fork();
    if(pid == ){
    close(pipefd[]);//0 read
    char buf[N];
    while(){
    memset(buf,,N);
    if(read(pipefd[],buf,N) == ){
    break;
    }
    printf("child read:%s\n",buf);
    }
    printf("child exit\n");
    exit();
    }else{
    close(pipefd[]);
    char line[N];
    while(memset(line,,N),fgets(line,N,stdin) != NULL ){
    write(pipefd[],line,strlen(line));
    }
    close(pipefd[]);
    printf("parent exit\n");
    wait(NULL);
    }
    return ;
    }