在linux的无名管道通信中,两个进程进行读写一次后,到第二次开始,读写总是失败,这是什么原因。 (无名管道 linux)

1) 出错的时候用perror把错误号打出来;

2)使用管道要注意一点:如果管道的读悉扒出端关闭,但是还有进程尝试向管道写入的话,试图写入的进程将收到一个SIGPIPE信号 ,睁清昌会导致进程正枣退出。

#include

#include

#include

#include

#include

#include

#define MAX_DATA_LEN 256

#define DELAY_TIME 1

int main(void)

{

pid_t pid;

int pfd;

char buf;

const char data = “Pipe Message”;

int real_read,real_write;

int time = 10;

memset((void *)buf,0,sizeof(buf));

if(pipe(pfd) == -1)

{

perror(“pipe”);

exit(1);

}

if((pid = fork()) == -1)

{

perror(“fork”);

exit(1);

}else if(pid == 0)

{

close(pfd);

sleep(DELAY_TIME);

while(time–)

{

sleep(1);

if((real_read = read(pfd,buf,MAX_DATA_LEN)) > 0)

{

printf(“%d bytes read from the pipe is %s\n”,real_read,buf);

}

}

close(pfd);

exit(0);

}else

{

close(pfd);

sleep(DELAY_TIME);

while(time–)

{

sleep(1);

if((real_write = write(pfd,data,strlen(data))) > 0)

{

printf(“parent wrote %d bytes:%s\n”,real_write,data);

}

}

close(pfd);

waitpid(pid,NULL,0);

exit(0);

}

}

你也没贴出什圆睁么错误,给缺腔核你个示例,你自己研究下, 这个是伏掘可以运行的,反复读写管道。。

相关问题拓展阅读:

    无名管道 linux的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于无名管道 linux,在linux的无名管道通信中,两个进程进行读写一次后,到第二次开始,读写总是失败,这是什么原因。的信息别忘了在本站进行查找喔。


    数据运维技术 » 在linux的无名管道通信中,两个进程进行读写一次后,到第二次开始,读写总是失败,这是什么原因。 (无名管道 linux)