1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
|
#include<iostream> #include <unistd.h> #include <sys/types.h> #include <errno.h> #include "stdio.h" using std::cin; using std::cout; using std::endl; int main() { int fd[2]; pid_t pid; char r_buffer[500]; int len=0; if(pipe(fd)<0){ cout<<"管道创建失败"<<endl; return 0; } if((pid=fork())==0){ close(fd[1]); sleep(2); for(int i = 0;i<3;i++){ len=read(fd[0],r_buffer,500); if(len>0){ cout<<r_buffer<<endl; cout<<endl; } } }else if(pid>0){ close(fd[0]); const char buffer[3][30]={"first: I am child !n ","second: How are you !n ","third: Good bye !n "}; for(int i = 0;i<3;i++){ write(len,buffer[i],30); sleep(3); } }else{ cout<<"进程创建失败"<<endl; return 0; } return 0; }
|
近期评论