c-管道似乎不在被exec()覆盖的fork()进程之间进行通信



我一直在努力学习如何为IPC使用管道,虽然我可以用一些琐碎的例子来操作,但我无法通过那些非常简单的程序来正确操作。

一方面,我认为我可能有一些比赛条件问题-我计划在管道工作后让信号量工作-所以如果这是问题所在,我很高兴听到它。

另一方面,我只是不知道我的代码掉到哪里了。。。

我有三块拼图:

  1. OUTER进程-分叉、执行和设置管道
  2. INNER进程-exec'd到一个fork上,并通过管道将消息发送到OUTER
  3. DISPL进程-exec被拖到一个fork上,并打印出一条从OUTER通过管道发送的消息

这3个进程的分叉和执行都很好,我只是从来没有从INNER管道中读取过任何内容,这会导致消息缓冲区包含一个空字符串。因此,DISPL从不显示任何内容。

我希望DISPL显示每一块9个字符以及包含的内容。由于我在这一点上什么都得不到,所以没有组合读取缓冲区来进行漂亮的打印。

我的问题是,为什么这些管道不传输任何数据

一如既往,我们欣然接受所有援助。

外部:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <string.h>
#define READ 0
#define WRITE 1
#define READ_BLOCK_SIZE 9
#define PROCESS_COUNT 2
#define SLEEP_TIME 2
int pfdInnerPipe[2];
int pfdDisplPipe[2];
int main()
{
pid_t processID;
char readBuffer[READ_BLOCK_SIZE+1];
ssize_t bytesRead;
char pipeFdStr_inner[10];
char pipeFdStr_displ[10];
if (pipe(pfdInnerPipe) < 0 || pipe(pfdDisplPipe) < 0)   exit(1);
sprintf(pipeFdStr_inner, "%d", pfdInnerPipe[WRITE]);
sprintf(pipeFdStr_displ, "%d", pfdDisplPipe[READ]);
for (int count = 0; count < PROCESS_COUNT; count++)
{
processID = fork();
switch (processID)
{
case 0:
if (count == 0) // spawn inner
{
// Inner will only write to pipe 1
close(pfdInnerPipe[READ]);
close(pfdDisplPipe[WRITE]);
close(pfdDisplPipe[READ]);
execl("./pipe_inner.exe", "pipe_inner.exe", pipeFdStr_inner, (char *)NULL); 
exit(2);
} else if (count == 1) // spawn display
{
// Display will only read from display pipe
close(pfdDisplPipe[WRITE]);
close(pfdInnerPipe[WRITE]);
close(pfdInnerPipe[READ]);
execl("./pipe_displ.exe", "pipe_displ.exe", pipeFdStr_displ, (char *)NULL); 
exit(2);
}
break;
case -1:
perror("fork failed");
exit(3);
break;
default :
continue;
}
}
// parent process
// parent will only read from INNER pipe and write to DISPL pipe
close(pfdDisplPipe[READ]);
close(pfdInnerPipe[WRITE]);
sleep(SLEEP_TIME); // allow time for something to be on the pipe
char messBuffer[] = "";
bytesRead = read(pipeFdStr_inner[READ], readBuffer, READ_BLOCK_SIZE);     
while (bytesRead > 0)
{
readBuffer[bytesRead] = '';
strcat(readBuffer, messBuffer);
printf("Outer: Read %li bytesn", bytesRead);
printf("Outer: Message Buffer: %sn", readBuffer);
bytesRead = read(pipeFdStr_inner[READ], readBuffer, READ_BLOCK_SIZE);
}
close(pipeFdStr_inner[READ]);
write(pipeFdStr_displ[WRITE], messBuffer, strlen(messBuffer));
sleep(SLEEP_TIME); // keep the pipe open to read from
close(pipeFdStr_displ[WRITE]);
}

内部:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#define READ_BLOCK_SIZE 9
#define SLEEP_TIME 10
int main(int argc, char *argv[])
{
int writeFd;
char *strFromChild = "Message from INNER to OUTER";
if(argc != 2) {
exit(1);
}
writeFd = atoi(argv[1]);
write(writeFd, strFromChild, strlen(strFromChild));
sleep(SLEEP_TIME); // keep pipe open for a while
close(writeFd);
}

显示:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#define READ_BLOCK_SIZE 9
#define SLEEP_TIME 5
int main(int argc, char *argv[])
{
int readFd;
char readBuffer[READ_BLOCK_SIZE+1];
ssize_t bytesRead;
if(argc != 2) {
exit(1);
}
readFd = atoi(argv[1]);
sleep(SLEEP_TIME); // allow time for everything else to happen
bytesRead = read(readFd, readBuffer, READ_BLOCK_SIZE);
while (bytesRead > 0) {
readBuffer[bytesRead] = '';
printf("Display: Read %li bytes - '%s'n", bytesRead, readBuffer);
bytesRead = read(readFd, readBuffer, READ_BLOCK_SIZE);
}
printf("Display: Finished reading from pipe 2n");
close(readFd);
}

这是踢自己的时间…你有:

bytesRead = read(pipeFdStr_inner[READ], readBuffer, READ_BLOCK_SIZE);     
while (bytesRead > 0)
{
readBuffer[bytesRead] = '';
strcat(readBuffer, messBuffer);
printf("Outer: Read %li bytesn", bytesRead);
printf("Outer: Message Buffer: %sn", readBuffer);
bytesRead = read(pipeFdStr_inner[READ], readBuffer, READ_BLOCK_SIZE);
}
close(pipeFdStr_inner[READ]);
write(pipeFdStr_displ[WRITE], messBuffer, strlen(messBuffer));

从中读取和写入的"文件描述符"是字符串的第一个字符,它会自动转换为int。您需要使用:

bytesRead = read(pfdInnerPipe[READ], readBuffer, READ_BLOCK_SIZE);     
while (bytesRead > 0)
{
readBuffer[bytesRead] = '';
strcat(readBuffer, messBuffer);
printf("Outer: Read %li bytesn", bytesRead);
printf("Outer: Message Buffer: %sn", readBuffer);
bytesRead = read(pfdInnerPipe[READ], readBuffer, READ_BLOCK_SIZE);
}
close(pfdInnerPipe[READ]);
write(pfdDisplPipe[WRITE], messBuffer, strlen(messBuffer));

通过错误检查系统调用发现了问题。检查的read()报告:

outer: 2020-09-30 23:15:48.086 - pid=36674: failed to read from pipe
error (9) Bad file descriptor

仔细查看文件描述符不难发现它不是文件描述符。

我使用了一些代码,这些代码在GitHub上的SOQ(堆栈溢出问题(存储库中作为src/libsoq子目录中的stderr.cstderr.h文件提供。outer.c的合理完全检查版本是这样的,我也同样错误地检查了inner.cdispl.c。请注意,我更改了程序名称,删除了pipe_前缀和.exe后缀。此外,outer.c在退出之前等待其两个子代死亡。

#include "stderr.h"
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
#include <string.h>
#define READ 0
#define WRITE 1
#define READ_BLOCK_SIZE 9
#define PROCESS_COUNT 2
#define SLEEP_TIME 2
int pfdInnerPipe[2];
int pfdDisplPipe[2];
int main(int argc, char **argv)
{
if (argc > 0)
err_setarg0(argv[0]);
err_setlogopts(ERR_PID | ERR_MILLI);
pid_t processID;
char readBuffer[READ_BLOCK_SIZE + 1];
ssize_t bytesRead;
char pipeFdStr_inner[10];
char pipeFdStr_displ[10];
if (pipe(pfdInnerPipe) < 0 || pipe(pfdDisplPipe) < 0)
exit(1);
err_remark("inner (%d,%d), displ (%d,%d)n",
pfdInnerPipe[READ], pfdInnerPipe[WRITE],
pfdDisplPipe[READ], pfdDisplPipe[WRITE]);
sprintf(pipeFdStr_inner, "%d", pfdInnerPipe[WRITE]);
sprintf(pipeFdStr_displ, "%d", pfdDisplPipe[READ]);
for (int count = 0; count < PROCESS_COUNT; count++)
{
processID = fork();
switch (processID)
{
case 0:
if (count == 0)     // spawn inner
{
// Inner will only write to pipe 1
close(pfdInnerPipe[READ]);
close(pfdDisplPipe[WRITE]);
close(pfdDisplPipe[READ]);
// execl("./pipe_inner.exe", "pipe_inner.exe", pipeFdStr_inner, (char *)NULL);
execl("./inner", "inner", pipeFdStr_inner, (char *)NULL);
err_syserr("failed to execute ./inner");
exit(2);
}
else if (count == 1)       // spawn display
{
// Display will only read from display pipe
close(pfdDisplPipe[WRITE]);
close(pfdInnerPipe[WRITE]);
close(pfdInnerPipe[READ]);
// execl("./pipe_displ.exe", "pipe_displ.exe", pipeFdStr_displ, (char *)NULL);
execl("./displ", "displ", pipeFdStr_displ, (char *)NULL);
err_syserr("failed to execute ./displ");
exit(2);
}
break;
case -1:
perror("fork failed");
exit(3);
break;
default:
err_remark("forked %dn", processID);
continue;
}
}
// parent process
// parent will only read from INNER pipe and write to DISPL pipe
close(pfdDisplPipe[READ]);
close(pfdInnerPipe[WRITE]);
sleep(SLEEP_TIME); // allow time for something to be on the pipe
char messBuffer[] = "";
bytesRead = read(pfdInnerPipe[READ], readBuffer, READ_BLOCK_SIZE);
if (bytesRead < 0)
err_syserr("failed to read from pipen");
err_remark("read %zd bytes [[%.*s]]n", bytesRead, (int)bytesRead, readBuffer);
while (bytesRead > 0)
{
readBuffer[bytesRead] = '';
strcat(readBuffer, messBuffer);
printf("Outer: Read %li bytesn", bytesRead);
printf("Outer: Message Buffer: %sn", readBuffer);
bytesRead = read(pfdInnerPipe[READ], readBuffer, READ_BLOCK_SIZE);
if (bytesRead < 0)
err_syserr("failed to read from pipen");
}
close(pfdInnerPipe[READ]);
write(pfdDisplPipe[WRITE], messBuffer, strlen(messBuffer));
sleep(SLEEP_TIME); // keep the pipe open to read from
close(pfdDisplPipe[WRITE]);
int status;
int corpse;
while ((corpse = wait(&status)) > 0)
err_remark("child %d exited with status 0x%.4Xn", corpse, status);
err_remark("exitsn");
return 0;
}

样本输出:

outer: 2020-09-30 23:26:24.222 - pid=36852: inner (3,4), displ (5,6)
outer: 2020-09-30 23:26:24.224 - pid=36852: forked 36853
outer: 2020-09-30 23:26:24.224 - pid=36852: forked 36854
inner: 2020-09-30 23:26:24.437 - pid=36853: fd = 4
displ: 2020-09-30 23:26:24.590 - pid=36854: fd = 5
outer: 2020-09-30 23:26:26.226 - pid=36852: read 9 bytes [[Message f]]
Outer: Read 9 bytes
Outer: Message Buffer: Message f
Outer: Read 9 bytes
Outer: Message Buffer: rom INNER
Outer: Read 9 bytes
Outer: Message Buffer:  to OUTER
inner: 2020-09-30 23:26:34.441 - pid=36853: exiting
outer: 2020-09-30 23:26:36.441 - pid=36852: child 36853 exited with status 0x0000
Display: Finished reading from pipe 2
displ: 2020-09-30 23:26:36.441 - pid=36854: exiting
outer: 2020-09-30 23:26:36.442 - pid=36852: child 36854 exited with status 0x0000
outer: 2020-09-30 23:26:36.442 - pid=36852: exits

最新更新