c - 管道陷入读取状态(C - 系统调用)

标签 c pipe system-calls

这是我第一次使用 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 之外(因此它至少应该读取最后一条消息),但程序仍然停留在读取中。 因此,如果有人可以帮助我,我将非常感激。

最佳答案

你的代码充满了问题。

switch(currentPid = fork()){
    case 0:

这里需要关闭管道的读取端:

        close(p[0]);

通常你会有一个dup2之前execl这里。通常,如果您的子进程要加载另一个程序,您需要将其标准输出重定向到管道的写入端,如 dup2(p[1], 1); close(p[1]); 所示。 .

        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);

这个wait调用位置错误。如果子进程输出的数据多于管道一次可以容纳的数据,则这里会出现死锁(子进程将阻塞在 write 中,等待父进程为管道提供 read 一些数据,然后才能退出;父进程将阻止 wait ,等待子进程退出后再从管道读取)。

在你给 child 做任何事之前等待 child 退出也是没有意义的。这不是应该是一种类似协进程的设计吗,即子级应该继续沿着父级运行,根据请求进行算术并将结果发送回来?

        printf("\nParentpid:  %d, Childpid: %i \n", getpid(), currentPid);

        //close(p[1]);

这是为什么 close评论掉了?父进程关闭管道的写入端至关重要(因为否则从管道的读取端读取数据将会阻塞,因为写入端仍然在某处打开(在本例中是在同一进程中))。

        if(read(p[0], &val, sizeof(val) == -1){

这一行甚至无法编译。它缺少 ) .

            perror("read()\n");
            exit(1);
        }

您缺少 EOF 检查。 read当到达数据末尾时将返回 0(并保持 val 未初始化)。

您应该使用管道中的所有可用数据(直到 read 返回 0 ),然后才 waitpid(currentPid, NULL, 0); .

关于c - 管道陷入读取状态(C - 系统调用),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50886782/

相关文章:

c++ - 在线编译器对计算机内存的影响

具有日志功能的 c Pipes shell 守护进程无法正常工作

go - 在不影响当前进程的情况下,使用 rlimit 限制子进程的内存使用

c - wait3(waitpid别名)在不应将errno设置为ECHILD的情况下返回-1

linux - 进程在执行系统调用时切换会发生什么?

c - 避免释放字符串文字

c - 使用 memcpy 进行二维动态数组?

c - 嵌入式域中 strlen() 的缺点

pipe - Grep Hcitool Lescan输出

javascript - 从 ScrollContainer 传递事件