Only one transmission between parent and child Processes1/* ============================================== ======= 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 51212 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 (); // large bugs are found here, which may result in incorrect operations, after debugging for a long time, it took 23 printf ("% u \ n", pid); 24 if (0 = PID) {25 close (pipefd [1]); // 0 read 1 write26 // a parent and child processes must comply with 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 read37 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 ); // wait until the child process ends 46} 47 return 0; 48} 49
The Parent and Child processes perform multiple read/write operations through pipelines. First, paste a strange method (that is, an error ):
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 102412 int main (INT argc, const char * argv []) 13 {14 int FDS [2]; 15 if (pipe (FDS) =-1) {// only one pair of read/write 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 read27 close (FDS [1]); // 1 write28 char Buf [1024] = ""; 29 read (FDS [0], Buf, 1024); // only one read is allowed, 30 printf ("child read: % s \ n", Buf ); 31 // exit (1); 32} else {// parent write33 34 close (FDS [0]); // 0 read35 // 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 codePaste the correct method:
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 51212 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 read25 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 }