以下代码通过管道从其他进程读取消息。所有进程都正确打印出所有消息,但是它们永远不会继续执行 while 循环。尝试在 Eclipse 中进行调试,读取所有消息后,它将停止在该 while 循环处。
索引是分配给每个进程的编号。第一个进程的索引为 0。消息本身只是发送消息的进程的索引。
索引是分配给每个进程的编号。第一个进程的索引为 0。消息本身只是发送消息的进程的索引。
while((n = read(fd[index][0], &mymsg, sizeof(int))) == sizeof(int))
printf("process%d has received a message from process%d\n", index, mymsg);
有什么想法是为什么会发生这种情况吗?
以下是每个进程如何向另一个进程写入:
// Write to other process
if(write(fd[index2][1], &index, sizeof(int)) != sizeof(int))
sys_error(2);
这将重复五次。fd是每个进程的读写端口的表格。
while
循环? - David Schwartzwhile
循环。你的代码似乎正在按照编码要求执行,不断从连接中读取直到出现错误。由于没有发生错误,所以它会继续读取。(它怎么知道另一个进程不会发送消息给它呢?) - David Schwartz