【发布时间】:2018-06-16 09:47:22
【问题描述】:
这是我第一次使用 C 语言,所以到目前为止非常困难。 我的程序读取一个file.txt。 在第一行是我必须创建的子进程的数量(var: NPROC),在剩余的行中我必须读取子进程的进程 ID、两个整数和一个字符,这是我必须计算的操作: 例如:
NPROC
(id) (n1) (op) (n2)
2
2 3 + 1
1 5 / 2
2 9 * 3
我必须使用消息队列(完美运行)将操作(逐行)发送给孩子,然后孩子应该使用管道发回结果。
这是代码:
(父亲)
//other code
for(i=0; i < NPROC; i++) {
if(pipe(p) == -1) {
perror("pipe");
exit(1);
}
switch(currentPid = fork()){
case 0:
execl("./child", "./child", NULL);
return 0;
case -1:
printf("Error when forking\n");
break;
default:
// in the father
childrenPids[i] = currentPid; // store current child pid
wait(NULL);
printf("\nParentpid: %d, Childpid: %i \n", getpid(), currentPid);
//close(p[1]);
if(read(p[0], &val, sizeof(val) == -1){
perror("read()\n");
exit(1);
}
printf("Parent(%d) received value: %d\n", getpid(), val);
break;
}
//other code
(child)
//other code
while (1) {
long msgtype = m->mtype;
if(msgrcv(msgid, m, sizeof(message) - sizeof(m->mtype), msgtype, IPC_NOWAIT) == -1) {
// if(close (p[1]) == -1){
// perror("close p[1] of the child\n");
// }
//perror("\nmsgrcv");
exit(1);
}
result[i] = calcolate_results(m);
// printf("%i %i %c %i", m->id, m->num1, m->op, m->num2);
printf("\nresult: %i\n", result[i]);
val = result[i];
if(write (p[1], &val, sizeof(val)) == -1) {
perror("write\n");
exit(1);
}
printf("Child(%d) send val: %d\n", getpid(), val);
}
//other code
现在我的问题是父亲的阅读似乎没有得到消息。 当我执行程序不退出时,它会在父亲读取时循环(保持不变)。 我试图将读取放在 for 之外(因此它至少应该读取最后一条消息)但程序仍然停留在读取中。 所以如果有人可以帮助我,我将非常感激。
【问题讨论】:
-
代码中可能有一些拼写错误,因为我将代码翻译成英文
-
翻译是个好主意。但请确认它仍然按照描述的方式运行。 IE。确保它仍然是minimal reproducible example。
-
@Yunnosch 我只翻译了变量、函数、cmets 的名称
-
无论如何,显示的代码绝对不是minimal reproducible example,这使问题偏离主题:“寻求调试帮助的问题(“为什么这段代码不起作用?”)必须包括期望的行为、特定问题或错误以及在问题本身中重现它所需的最短代码。没有明确问题陈述的问题对其他读者没有用处。请参阅:如何创建minimal reproducible example。"
-
我已经发布了一个答案,列出了我可以从您的代码摘录中看到的问题。
read阻塞的常见问题是在子级中忘记close(p[0]),在父级中忘记close(p[1])。
标签: c pipe system-calls