日期:2014-05-16 浏览次数:20663 次
1 #include<stdio.h> 2 #include<unistd.h> 3 #include<stdlib.h> 4 5 int main() 6 { 7 int n; 8 pid_t pid; 9 int fd[2]; 10 char line[56]; 11 if(pipe(fd) < 0) 12 { 13 printf("pipe error\n"); 14 } 15 if((pid = fork()) > 0){ 16 close(fd[1]); 17 n = read(fd[0],line,56); 18 if(n > 0){ 19 printf("%s\n",line); 20 } 21 }else if(pid == 0){ 22 dup2(fd[1],1); 23 close(fd[1]); 24 close(fd[0]); 25 int x = 20; 26 while(x){ 27 printf("hello world\n"); 28 x--; 29 sleep(1); 30 } 31 return 0; 32 } 33 return 0; 34 } 35