日期:2014-05-16 浏览次数:20871 次
#include<unistd.h> #include<sys/types.h> #include<iostream> #include<string.h> int main(int argc,char* argv[]) { int command_pipe[2]; pid_t pid; pid = fork(); char command_string[255]; if(pipe(command_pipe) < 0) std::cerr<<"pipe failed"<<std::endl; if(pid<0) std::cerr<<"fork failed!"<<std::endl; if(pid == 0) { char ch[255] = {'\0'}; close(command_pipe[1]); int n=read(command_pipe[0],ch,255); std::cout<<"child read:"<<n<<std::endl; std::cout<<ch<<std::endl; } else if(pid >0) { close(command_pipe[0]); char m[255]; strcpy(m,"111"); if(-1!= write(command_pipe[1],m,4)) std::cout<<"write success"<<std::endl; else std::cout<<"write fail"<<std::endl; close(command_pipe[1]); } return 0; }