Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/62.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_System_Child Process - Fatal编程技术网

C 有关子进程终止的更多信息?

C 有关子进程终止的更多信息?,c,system,child-process,C,System,Child Process,我在谷歌上搜索答案,但我发现的所有线程似乎都建议使用另一种方法来终止子进程:函数_Exit() 我想知道使用“return 0;”是否真的终止子进程?我在我的程序中对此进行了测试(我在父进程中使用了waitpid()来捕获子进程的终止),它似乎工作得很好 有人能确认一下这个问题吗?return语句是否真的终止了一个进程,比如exit函数,或者它只是发送一个信号,指示调用进程已经“完成”,而该进程实际上仍在运行 提前感谢,, 丹 示例代码: pid = fork() if (pid == 0)

我在谷歌上搜索答案,但我发现的所有线程似乎都建议使用另一种方法来终止子进程:函数_Exit()

我想知道使用“return 0;”是否真的终止子进程?我在我的程序中对此进行了测试(我在父进程中使用了waitpid()来捕获子进程的终止),它似乎工作得很好

有人能确认一下这个问题吗?return语句是否真的终止了一个进程,比如exit函数,或者它只是发送一个信号,指示调用进程已经“完成”,而该进程实际上仍在运行

提前感谢,, 丹

示例代码:

pid = fork()

if (pid == 0) // child process
{
   // do some operation
   return 0; // Does this terminate the child process?
}
else if (pid > 0) // parent process
{
   waitpid(pid, &status, 0);
   // do some operation
}

在main函数中使用return语句将立即终止进程并返回指定的值。该过程完全终止

int main (int argc, char **argv) {
    return 2;
    return 1;
}
这个程序永远不会到达第二个return语句,值2返回给调用者

编辑-fork发生在另一个函数中时的示例 但是,如果return语句不在main函数中,则子进程将不会终止,直到它再次到达main()为止。下面的代码将输出:

Child process will now return 2
Child process returns to parent process: 2
Parent process will now return 1
代码(在Linux上测试):


如果您已经从中返回,您的子进程正在运行什么代码?@MikeW我有:If(A)then execvp(),else If(B)then调用我编写的函数。我知道execvp()将终止子进程,除非它失败,但如果条件B满足,我仍然需要终止子进程。请提供一些代码。这个问题听起来相当混乱和不清楚。谢谢!我想我的问题应该是“main函数中的return语句是否终止了进程”:)我在其他场景中添加了一个edit,以防有人怀疑。
pid_t pid;

int fork_function() {
    pid = fork();
    if (pid == 0) {
        return 2;
    }
    else {
        int status;
        waitpid (pid, &status, 0); 
        printf ("Child process returns to parent process: %i\n", WEXITSTATUS(status));
    }
    return 1;
}

int main (int argc, char **argv) {
    int result = fork_function();
    if (pid == 0) {
        printf ("Child process will now return %i\n", result);
    }
    else {
        printf ("Parent process will now return %i\n", result);
    }
    return result;
}