c - 我试图在 C 中多次调用 fork
问题描述
这就是我想要的结构,过程传递一个 int 并添加到它每次打印出结果。
管理多个管道。
|Main|<--------\ 0 1 2 3
| \ Pipes: read [a] [c] [e] [g] (each column
| | write [b] [d] [f] [h] is a pipe)
\/ |
|Process 1| | Process 1 reads from 'a' in pipe 0, and writes to 'd' in pipe 1
| index: 0 | Process 2 reads from 'c' in pipe 1, and writes to 'f' in pipe 2
| | Process 3 reads from 'e' in pipe 2, and writes to 'h' in pipe 3
\/ |
|Process 2| |
| index: 1 |
| |
\/ |
|Process 3|_____ /
index: 2
我希望它打印这样的东西:
Main process sent 5
(0) Got 5
(0) Sent 6
(1) Got 6
(1) Sent 7
(2) Got 7
(2) Sent 8
The final result is 8
但是无论出于何种原因,当我运行代码时,它都会挂在那里。构建它不会显示任何错误。我检查了它是否有错别字,所以我不认为它是那样的。我猜这是一个逻辑错误,但我只是没有看到。这是基于 C 手册的基本内容,所以我不知道为什么它不起作用。
#define PROCESS_NUM 3
// 3 processes with 4 pipes to connect them all
int
main(int argc, char *argv[])
{
int pids[PROCESS_NUM]; // array for storing process id'
int pipes[PROCESS_NUM + 1][2];
int i;
for (i = 0; i < PROCESS_NUM + 1; i++) {
if (pipe(pipes[i]) == -1) {
printf("Errorwith creating pipe\n");
return 1;
}
}
for (i = 0; i < PROCESS_NUM; i++) {
pids[i] = fork();
if (pids[i] == -1) {
printf("Error with creating process \n");
return 2;
}
if (pids[i] == 0) {
// child process
// i for first process 0 hence goes to pipe 0
int j;
for (j = 0; j < PROCESS_NUM + 1; j++) {
if (i != j) {
close(pipes[j][0]);
}
if (i + 1 != j) {
close(pipes[j][1]);
}
}
int x;
if (read(pipes[i][0], &x, sizeof(int)) == -1) {
printf("Error ar reading\n");
return 3;
}
printf("(%d) Got %d\n", i, x);
x++;
if (write(pipes[i + 1][1], &x, sizeof(int)) == -1) {
printf("Error at writing\n");
return 4;
}
printf("(%d) Got %d\n", i, x);
close(pipes[i][0]);
close(pipes[i + 1][1]);
return 0;
// Once this process compleatss the program stopes so we don't make an infinete number of processes.
}
for (i = 0; i < PROCESS_NUM + 1; i++) {
wait(NULL);
}
return 0;
}
// Main process
int j;
for (j = 0; j < PROCESS_NUM + 1; j++) {
if (j != PROCESS_NUM) {
close(pipes[j][0]);
}
if (j != 0) {
close(pipes[j][1]);
}
}
int y = 5;
printf("Main process sent: %d\n", y);
if (write(pipes[0][1], &y, sizeof(int)) == -1) {
printf("Error at writing\n");
return 4;
}
if (read(pipes[PROCESS_NUM][0], &y, sizeof(int)) == -1) {
printf("Error ar reading\n");
return 3;
}
printf("The final result is: %d\n", y);
close(pipes[0][1]);
close(pipes[PROCESS_NUM][0]);
for (i = 0; i < PROCESS_NUM; i++) {
wait(NULL);
}
return 0;
}
解决方案
在主fork
循环结束时,您有以下代码行:
for (i = 0; i < PROCESS_NUM + 1; i++) {
wait(NULL);
}
return 0;
}
// Main process
主进程执行return 0
并且永远不会到达注释之后的代码行Main process
。删除那个等待循环并返回。
推荐阅读
- java - MacOS 无法识别 MacOS 10.14.6 上更新的 Java
- java - 调用 Spring 服务并通过 Constructor 发送参数
- python - PCA - 将主成分追溯到一个维度
- typescript - 打字稿,当被调用者数组与空数组联合时,Array.prototype.map() 有错误“表达式不可调用”
- python - 将 youtube_dl 与 discord.py 语音客户端一起使用时没有声音
- javascript - 如何在 NativesScrip Sidekick 中使用插件?
- javascript - 如何使用 CSS 或 JS 定位类的父级
- mysql - Mysql 内存使用量不断增长,直到 OOM 和系统终止/重新启动
- python-3.x - gmplot 标记在标记 256 点后不起作用
- python - “SQLTable”对象没有属性“insert_statement”