c - 管道似乎无法在 exec() 覆盖的 fork() 进程之间进行通信
问题描述
我一直在尝试学习如何为 IPC 使用管道,虽然我可以得到一些琐碎的例子来工作,但我无法通过那些非常简单的程序来正确运行。
一方面,我认为我可能有一些竞争条件问题——我计划在管道工作后让信号量工作——所以如果这是问题所在,我很高兴听到它。
另一方面,我只是不知道我的代码在哪里崩溃了......
这个谜题有 3 个部分:
- OUTER 进程 - 分叉、执行和设置管道
- INNER 进程 - 执行到 fork 并将消息通过管道传递给 OUTER
- DISPL 进程 - 执行到 fork 和 printf 从 OUTER 管道传输的消息
这 3 个进程正在分叉和执行正常,我只是从来没有从内部管道中读取任何内容,这会导致消息缓冲区包含一个空字符串。因此,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] = '\0';
strcat(readBuffer, messBuffer);
printf("Outer: Read %li bytes\n", bytesRead);
printf("Outer: Message Buffer: %s\n", 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] = '\0';
printf("Display: Read %li bytes - '%s'\n", bytesRead, readBuffer);
bytesRead = read(readFd, readBuffer, READ_BLOCK_SIZE);
}
printf("Display: Finished reading from pipe 2\n");
close(readFd);
}
解决方案
这是踢自己的时间......你有:
bytesRead = read(pipeFdStr_inner[READ], readBuffer, READ_BLOCK_SIZE);
while (bytesRead > 0)
{
readBuffer[bytesRead] = '\0';
strcat(readBuffer, messBuffer);
printf("Outer: Read %li bytes\n", bytesRead);
printf("Outer: Message Buffer: %s\n", 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] = '\0';
strcat(readBuffer, messBuffer);
printf("Outer: Read %li bytes\n", bytesRead);
printf("Outer: Message Buffer: %s\n", 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(堆栈溢出问题)存储库中使用了一些代码作为文件和stderr.c
src /libsoq子目录。合理的完全检查版本是这样的,我也进行了错误检查,类似地。请注意,我更改了程序名称,删除了前缀和后缀。此外,在退出之前等待其两个孩子都死了。stderr.h
outer.c
inner.c
displ.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 %d\n", 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 pipe\n");
err_remark("read %zd bytes [[%.*s]]\n", bytesRead, (int)bytesRead, readBuffer);
while (bytesRead > 0)
{
readBuffer[bytesRead] = '\0';
strcat(readBuffer, messBuffer);
printf("Outer: Read %li bytes\n", bytesRead);
printf("Outer: Message Buffer: %s\n", readBuffer);
bytesRead = read(pfdInnerPipe[READ], readBuffer, READ_BLOCK_SIZE);
if (bytesRead < 0)
err_syserr("failed to read from pipe\n");
}
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%.4X\n", corpse, status);
err_remark("exits\n");
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
推荐阅读
- vba - 将多个 .txt 文件导入到每周更新的一个 excel 表中(所有的标题和列都相同)
- c# - BsonDateTimeOptions 与日期时间?
- bash - 使用连接组合基于同一列的 2 个 csv 文件
- python - 使用多处理的 Python 并行化函数
- c# - 从 c# winform 中的另一个类调用标签文本
- php - Laravel:使用 Eloquent 加入查询
- java - 房间数据库更新问题
- mongodb - 从 mongodb 中的多个集合中选择
- excel - Excel 2016 - 用商合并两个数据透视表
- php - Yii2 : 将原始查询转换为 ActiveRecord