Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 永远等待_C_Linux_Posix_Wait_Waitpid - Fatal编程技术网

C 永远等待

C 永远等待,c,linux,posix,wait,waitpid,C,Linux,Posix,Wait,Waitpid,我对waitpid函数有点困惑: int main(int argc, char** argv) { if (pid_t pid = fork()) { setpgid(pid, pid); waitpid(pid, NULL, 0); } else { setpgid(0, 0); char *args[] = {"man", "2", "pipe", NULL}; execvp(args[0],

我对waitpid函数有点困惑:

int main(int argc, char** argv) {
    if (pid_t pid = fork()) {
        setpgid(pid, pid);
        waitpid(pid, NULL, 0);
    } else {
        setpgid(0, 0);
        char *args[] = {"man", "2", "pipe", NULL};
        execvp(args[0], args);
    }

    return 0;
}
在本例中,程序只是块,而不是块。同样的故事发生在每一个等待你按下“q”的程序中。另一方面,如果我用say“ls”替换man,那么一切都会完美地工作:它打印目录的内容

char *args[] = {"ls", NULL};
那么男人有什么特别之处呢

编辑:我忘了说我需要成为团队领导者的过程。如果我删除setpgid(),一切都正常:

int main(int argc, char** argv) {
    if (pid_t pid = fork()) {
        waitpid(pid, NULL, 0);
    } else {
        char *args[] = {"man", "2", "pipe", NULL};
        execvp(args[0], args);
    }

    return 0;
}

您正在将子进程放入其自己的进程组中。会话的控制终端在任何给定的时间只有一个前台进程组,您不需要将子进程组放在前台。因此,它不接收键盘输入,其标准输出不显示在终端上

因为您在子系统中执行的命令等待键盘输入,而当该命令在后台时,键盘输入永远不会出现,所以该命令永远不会退出,并且
waitpid()
永远不会收集它


除非您有特定的理由将子进程放在不同的进程组中,否则您可能不应该这样做。

您在什么操作系统上运行此程序?我在使用Ubuntu 14.04。您是否正在创建自己的shell?我不知道进程组的确切用途,但在
setpgid()
手册页中,shell似乎使用它们来重新组合同一作业的进程,因此,除非您正在编写shell,否则尝试重写它可能不是个好主意。是的,我正在使用管道编写shell。我需要进程组向管道中的所有程序发送信号。你能用C编译器编译吗?(如果条件中的声明)非常感谢!如果确实需要将子进程放入其自己的进程组,则应使用
tcsetpgrp
控制终端的前台进程组。