该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
代码如下:
#include
#include
#include
#include
int main (int argc,char *argv[])
{
int f_des[2];
static char message[1024];
if(argc!=2){
fprintf(stderr,"Usage:%s message\n",*argv[0]);
exit(1);
}
if(pipe(f_des)==-1){
perror("pipe");
exit(2);
}
switch (fork()){
case -1:
perror("Fork");
exit(3);
case 0:
close(f_des[1]);
if(read(f_des[0],message,1024)!=-1){
printf("Message received by child:[%s]\n",message);
fflush(stdout);
}else{
perror("Read");
exit(4);
}
break;
default:
close(f_des[0]);
if(write(f_des[1],argv[1],strlen(argv[1]))!=-1){
printf("Message sent by parent:[%s]\n",argv[1]);
fflush(stdout);
}else{
perror("write");
exit(5);
}
}
exit(0);
}