码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2014-08-22 02:46:15      阅读:293      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   os   io   strong   for   

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

  • 父子进程之间只进行一次传递
     1 /*============================================
     2   > Copyright (C) 2014 All rights reserved.
     3   > FileName:onepipe.c
     4   > author:donald
     5   > details:
     6 ==============================================*/
     7 #include <unistd.h>
     8 #include <stdio.h>
     9 #include <stdlib.h>
    10 #include <string.h>
    11 #define N 512
    12 int main(int argc, const char *argv[])
    13 {
    14     int pipefd[2];
    15    pid_t pid;
    16 
    17     if(pipe(pipefd) == -1){
    18         perror("pipe failed");
    19         exit(-1);
    20     }
    21     printf("%u\n",pid);
    22     pid == fork();//这里一个个大大的bug,自己的误操作,debug了很久才搞定了
    23     printf("%u\n",pid);
    24     if(0 == pid){
    25         close(pipefd[1]);//0 read 1 write
    26       //一个约定,父子进程都需遵守
    27 
    28         char buf[N];
    29         memset(buf,0,N);
    30         read(pipefd[0],buf,N);
    31         printf("child read:%s\n",buf);
    32 
    33         printf("child exit\n");
    34         exit(1);
    35     }else{
    36         close(pipefd[0]);//0 read
    37         char line[N];
    38         printf("parent begin\n");
    39 
    40         memset(line,0,N);
    41         fgets(line,N,stdin);
    42 
    43         write(pipefd[1],line,strlen(line));
    44         printf("parent exit\n");
    45         wait(NULL);//等待子进程的结束
    46     }
    47     return 0;
    48 }
    49         

     

  • 父子进程通过管道,进行多次读写操作,先贴上一个比较奇葩的方法(就是一个错误):

    bubuko.com,布布扣
     1 /*============================================
     2   > Copyright (C) 2014 All rights reserved.
     3   > FileName:my_pipe.c
     4   > author:donald
     5   > details:
     6 ==============================================*/
     7 #include <unistd.h>
     8 #include <stdio.h>
     9 #include <stdlib.h>
    10 #include <string.h>
    11 #define N 1024
    12 int main(int argc, const char *argv[])
    13 {
    14     int fds[2];
    15     if(pipe(fds) == -1){//只能有一对进行读写
    16         perror("failed");
    17         exit(1);
    18     }
    19     pid_t pid = fork();
    20     if(pid == -1){
    21         perror("error");
    22         exit(1);
    23     }
    24 
    25     while(1){
    26         if(pid == 0){//child read
    27             close(fds[1]);//1 write
    28             char buf[1024] = "";
    29             read(fds[0],buf,1024);//只能有一个读,
    30             printf("child read:%s\n",buf);
    31             //exit(1);
    32         }else{//parent write
    33 
    34             close(fds[0]);//0 read
    35             //        char *p = "hello,donald";
    36             char line[N];
    37           //  memset(line,0,N);
    38             fgets(line,N,stdin);
    39             write(fds[1],line,strlen(line));
    40             //wait(NULL);
    41         }
    42     }
    43     return 0;
    44 }
    View Code

    再贴上正确的方法:

     1 /*============================================
     2   > Copyright (C) 2014 All rights reserved.
     3   > FileName:twopipe.c
     4   > author:donald
     5   > details:
     6 ==============================================*/
     7 #include <unistd.h>
     8 #include <stdio.h>
     9 #include <stdlib.h>
    10 #include <string.h>
    11 #define N 512
    12 int main(int argc, const char *argv[])
    13 {
    14     int pipefd[2];
    15     pid_t pid;
    16     //pid = fork();
    17 
    18     if(pipe(pipefd) == -1){
    19         perror("pipe failed");
    20         exit(-1);
    21     }
    22     pid = fork();
    23     if(pid == 0){
    24         close(pipefd[1]);//0 read
    25             char buf[N];
    26         while(1){
    27             memset(buf,0,N);
    28             if(read(pipefd[0],buf,N) == 0){
    29                 break;
    30             }
    31             printf("child read:%s\n",buf);
    32         }
    33         printf("child exit\n");
    34         exit(1);
    35     }else{
    36         close(pipefd[0]);
    37         char line[N];
    38         while(memset(line,0,N),fgets(line,N,stdin) != NULL ){
    39             write(pipefd[1],line,strlen(line));
    40         }
    41         close(pipefd[1]);
    42         printf("parent exit\n");
    43         wait(NULL);
    44     }
    45     return 0;
    46 }

     

管道通信之无名管道---pipe(),布布扣,bubuko.com

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

标签:style   blog   http   color   os   io   strong   for   

原文地址:http://www.cnblogs.com/ben1314/p/3928468.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!