简体   繁体   English

为什么我的程序在读取管道的中间冻结?

[英]Why does my program freeze in the middle of a pipe read?

I'm writing a C++ program for class that requires a set of N child processes to communicate with their parent process on a Unix-based system. 我正在为一个类编写C ++程序,该程序需要一组N个子进程与基于Unix的系统上的父进程进行通信。 To that end, I declared two arrays of N pipes, one for each direction of communication, before forking the process: 为此,在派生该过程之前,我声明了两个由N个管道组成的数组,用于每个通信方向:

int pipesToParent[N][2];
int pipesToChild[N][2];

... // for int i = 0 to N-1, pipe(pipesToParent[i]), pipe(pipesToChild[i]),
    // fork(), yada yada, I included error checking and everything.

int pipesToChild[N][2];

Most of the code for the parent process is in a while loop; 父进程的大多数代码都在while循环中; near the beginning of this loop, it reads a message from a particular process: 在此循环开始附近,它从特定进程中读取一条消息:

read(pipesToParent[processToServe][READ_END], charArray, BUF_SIZE);

The first time it goes through the loop, reading a message from process X, it works fine. 它第一次通过循环,从进程X读取一条消息时,它工作正常。 Then it's instructed to do the same for process Y, the message from which contains exactly the same sort of data as process X (I have each process cout its message when it writes it to its pipe), and the process just stops. 然后,它被指示对进程Y执行相同的操作,该消息中包含的消息与进程X完全相同(我将每个进程将其消息写入其管道时都将其消息删除),并且该进程只是停止。 It doesn't exit, it just stays frozen at that line. 它不会退出,只会停留在该行。 I've tried everything I can think of to figure out what might be wrong with the message I'm sending, but it all looks just fine. 我已经尽力想尽一切办法弄清楚我发送的消息出了什么问题,但一切都很好。 What's going on here? 这里发生了什么?

(Sorry if my detail's insufficient; I couldn't figure out what else I'd have to include and what I could remove to paste something that'd reproduce the problem.) (很抱歉,如果我的详细信息不足;我无法弄清楚还必须包括什么内容,以及可以删除哪些内容以粘贴可以重现该问题的内容。)

ETA: 预计到达时间:

The child process has its own internal while loop, in which it reads from its own pipe: 子进程具有自己的内部while循环,在其中它从自己的管道读取:

read(pipesToChild[thisProcessNumber][READ_END], charArray, BUF_SIZE);

If the parent's ready, it'll send its message: 如果父母已经准备好,它将发送其消息:

write(pipesToParent[thisProcessNumber][WRITE_END], message, BUF_SIZE);

Some tinkering has revealed that process Y, much like the parent, is going through this loop once fine, then getting stuck on the read the second time. 某些修补发现,进程Y与父进程非常相似,一旦正常,就会经历此循环,然后第二次被卡住。 What exactly would flushing do, and can I do it if I wrote to/read from the pipe this way? 冲洗到底会做什么,如果我以这种方式写入/读取管道,该怎么办? (If it's relevant, I've #included cstdlib, but not cstdio.) (如果相关的话,我已经包含了cstdlib,但没有cstdio。)

Seems like deadlock to me - both your processes are waiting for another one to write something - but none of them actually writes anything, so everything stops. 在我看来,僵局-您的两个进程都在等待另一个进程来写一些东西-但它们实际上都没有写任何东西,因此一切都停止了。 Make sure that your comunication design is deadlock-free (eg. clients reding conantly and writing only on reading READY comunicate, and server reading only fter writing READ comunicate). 确保您的通信设计没有死锁(例如,客户端不断变红并仅在读取READY通信时写入,而服务器在写入READ通信后才读取)。 You might also consider going to non-blocking pipes. 您可能还考虑使用非阻塞管道。

As far as i know pipes created with pipe() call are unbuffered - the only buffer involved is pipe buffer, but as this is the same buffer from which reading in target app is done, it cannot create comunnication problems. 据我所知,使用pipe()调用创建的管道是无缓冲的-所涉及的唯一缓冲区是管道缓冲区,但是由于这是与完成目标应用程序中读取操作的缓冲区相同,因此不会产生通信问题。 Application-level happens only if you map file descriptors to C-like FILE pointers, but it seems it's not the case. 仅当您将文件描述符映射到类似C的FILE指针时,才发生应用程序级,但事实并非如此。

If you still think that everything is ok, post minimal compilable code that shows this behavior, so I can perhaps say more. 如果您仍然认为一切正常,请发布可显示这种行为的最少可编译代码,所以我可以说更多。

EDIT: 编辑:

Since you accepted an answer, it would be nice if you mentioned also which solution (if any) solved your problem. 既然您接受了答案,那么如果您还提到哪种解决方案(如果有)可以解决您的问题,那将是很好的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM