Operating system 进程间通信、操作系统、管道
我在一本书中读到,为了在两个进程之间使用管道进行进程间通信,最好使用两个管道,一个用于孩子在其中写字,另一个用于父亲阅读,另一个用于相反的通信。为什么这是一个更好的方法?我们不能只使用一个管道,以便父级和子级都可以读取和写入它吗?您需要一种方法来协调进程之间的通信,否则进程将反复读取/写入它所写/读取的内容。e、 g.如果使用一根管道:Operating system 进程间通信、操作系统、管道,operating-system,pipe,ipc,Operating System,Pipe,Ipc,我在一本书中读到,为了在两个进程之间使用管道进行进程间通信,最好使用两个管道,一个用于孩子在其中写字,另一个用于父亲阅读,另一个用于相反的通信。为什么这是一个更好的方法?我们不能只使用一个管道,以便父级和子级都可以读取和写入它吗?您需要一种方法来协调进程之间的通信,否则进程将反复读取/写入它所写/读取的内容。e、 g.如果使用一根管道: //parent while(1) { write(p1); //need a logic to wait so as to read what
//parent
while(1)
{
write(p1);
//need a logic to wait so as to read what child wrote back and also so
// that we may not end up reading back what we wrote.
read(p1);
}
//child
while(1)
{
read(p1);
//need a logic to wait so as to read what child wrote back and also so
// that we may not end up reading back what we wrote.
write(p1);
}
找到一个简单的逻辑来同步或使用两个管道。我是说,傻瓜式的等待,因为像sleep()
或signals
这样的简单技术很容易受到操作系统人员在其工作中概述的调度问题的影响
管道本身是块构造,因此依赖它们来同步
//parent
while(1)
{
write(p1);
//pipe blocks until till child writes something in pipe
read(p2);
}
//child
while(1)
{
//pipe waits for parent to write.
read(p1);
//parent is waiting to read.
write(p2);
}