C 如何跟踪wait()和fork()命令
我很难理解子进程如何以及何时返回到父进程。看看这个例子,我用不同的网站把它放在一起:C 如何跟踪wait()和fork()命令,c,fork,C,Fork,我很难理解子进程如何以及何时返回到父进程。看看这个例子,我用不同的网站把它放在一起: #include <stdio.h> #include <stdlib.h> main( int argc, char *argv[] ) { int i; int pid; int wpid; int status; time1.tv_sec = 5L; time1.tv_nsec = 0L; for ( i = 0; i < 10
#include <stdio.h>
#include <stdlib.h>
main( int argc, char *argv[] )
{
int i;
int pid;
int wpid;
int status;
time1.tv_sec = 5L;
time1.tv_nsec = 0L;
for ( i = 0; i < 10; i++ )
{
pid = fork();
if ( pid < 0 ) {
printf ( "Error" );
exit ( 1 );
}
else if ( pid == 0 ) {
break;
}
}
while ( ( wpid = wait ( &status ) ) > 0 )
{
printf ( "Exit status of %d was %d (%s)\n", ( int ) wpid, status, ( status == 0 ) ? "accept" : "reject" );
}
return 0;
}
我不明白的是孩子到底回来了什么,什么时候/在哪里?在fork命令之后,子循环进入第二个循环,但它将永远不会退出该循环以进一步向下到达return命令。我是否应该设置一个条件,以便只有父级执行该循环?孩子是否忽略wait命令
我发现,对于像我这样的人来说,这是一个经验丰富的程序员没有足够的沉默的问题。我找不到一个解决这个问题的答案。等待接收进程的退出代码,它是一个int。该值可以通过exit1进行设置
您的代码如下所示
time1.tv_nsec = 0L; // only "parent" thread here.
for ( i = 0; i < 10; i++ )
{
pid = fork(); // create a child.
if ( pid < 0 ) { //
printf ( "Error" );
exit ( 1 );
}
else if ( pid == 0 ) { // only the child will enter this if, so it will exit the loop.
break;
}
}
while ( ( wpid = wait ( &status ) ) > 0 ) // children and parent run this condition, but the
// children don't have children so they will just
// exit the wait function with -1, so they don't run
// loop
{
printf ( "Exit status of %d was %d (%s)\n", ( int ) wpid, status, ( status == 0 ) ? "accept" : "reject" ); // only the parent executes this
}
return 0; // return from main, this value is the one that will be received by the wait (in case
// of children processes. There is also the option of using exit(0); exit ends program
// execution everywhere it is used.
wait接收进程的退出代码,它是一个int。例如,可以通过exit1设置该值
您的代码如下所示
time1.tv_nsec = 0L; // only "parent" thread here.
for ( i = 0; i < 10; i++ )
{
pid = fork(); // create a child.
if ( pid < 0 ) { //
printf ( "Error" );
exit ( 1 );
}
else if ( pid == 0 ) { // only the child will enter this if, so it will exit the loop.
break;
}
}
while ( ( wpid = wait ( &status ) ) > 0 ) // children and parent run this condition, but the
// children don't have children so they will just
// exit the wait function with -1, so they don't run
// loop
{
printf ( "Exit status of %d was %d (%s)\n", ( int ) wpid, status, ( status == 0 ) ? "accept" : "reject" ); // only the parent executes this
}
return 0; // return from main, this value is the one that will be received by the wait (in case
// of children processes. There is also the option of using exit(0); exit ends program
// execution everywhere it is used.
我们来看看叉子是如何工作的。< /P> 当调用fork时,将通过克隆调用它的进程来创建一个新进程。从现在起,子进程与父进程的第一个进程完全相同。这意味着这两个进程将继续在fork后面的命令处执行 现在,你如何区分哪一个是父母,哪一个是孩子?基于fork的返回状态 成功完成后,fork将向子进程返回0 并应将子进程的进程ID返回给父进程 过程 因此,如果我们假设一切正常,没有发生错误,那么中断;代码的语句在子进程中执行,父进程继续生成子进程,直到循环完成 现在,休息之后;每个孩子都在等待。从等待的主页面: wait,waitpid-等待子进程停止或终止 但是等一下!每个子进程都没有要等待的子进程!因此,ECHILD的等待失败,孩子们终止 此外,在执行所有循环迭代之后,父进程将发现自己在等待。父进程有几个子进程,并在它们终止时一次获取它们的退出状态。子项不会以任何特定顺序终止,这取决于操作系统,您不能假设一个将在另一个之前终止 我希望上述内容能够澄清一些问题,并且现在能够修复代码,使其正常工作
>
< P> >让我们来看看叉子是如何工作的。< /P> 当调用fork时,将通过克隆调用它的进程来创建一个新进程。从现在起,子进程与父进程的第一个进程完全相同。这意味着这两个进程将继续在fork后面的命令处执行 现在,你如何区分哪一个是父母,哪一个是孩子?基于fork的返回状态 成功完成后,fork将向子进程返回0 并应将子进程的进程ID返回给父进程 过程 因此,如果我们假设一切正常,没有发生错误,那么中断;代码的语句在子进程中执行,父进程继续生成子进程,直到循环完成 现在,休息之后;每个孩子都在等待。从等待的主页面: wait,waitpid-等待子进程停止或终止 但是等一下!每个子进程都没有要等待的子进程!因此,ECHILD的等待失败,孩子们终止 此外,在执行所有循环迭代之后,父进程将发现自己在等待。父进程有几个子进程,并在它们终止时一次获取它们的退出状态。子项不会以任何特定顺序终止,这取决于操作系统,您不能假设一个将在另一个之前终止 我希望上述内容能够澄清一些问题,并且现在能够修复代码,使其正常工作注意。是的,您需要提供条件逻辑,以指导子流程的工作。由于fork将设置父对象的stack变量,而不是子对象的stack值,因此可以执行以下操作:
pid_t pid1 = 0;
printf("*** Parent is about to fork process 1 ***\n");
if ((pid1 = fork()) < 0) {
printf("Failed to fork process 1\n");
exit(1);
}
else if (pid1 == 0)
{
// Work of the child process
}
else
{
// Work of the parent process
}
是的,您需要提供条件逻辑来指导子流程的工作。由于fork将设置父对象的stack变量,而不是子对象的stack值,因此可以执行以下操作:
pid_t pid1 = 0;
printf("*** Parent is about to fork process 1 ***\n");
if ((pid1 = fork()) < 0) {
printf("Failed to fork process 1\n");
exit(1);
}
else if (pid1 == 0)
{
// Work of the child process
}
else
{
// Work of the parent process
}
调用fork时,子进程和父进程在同一位置启动:就在调用fork之后。唯一的区别是,fork为子进程返回0,而子进程的PID>0为父进程。这就是为什么在调用fork之后需要一个条件
因此,在您的子进程中,它将检查返回值0和break
在while循环中插入。父进程将停留在for循环中,在那里它将继续派生子进程,直到完成。然后它将进入while循环
在您的子进程中,wait函数可能返回ECHILD see,因为没有子进程,所以您应该确保子线程没有调用它,或者确保它们能够优雅地处理错误。当您调用fork时,子进程和父进程在同一位置启动:在调用fork之后。唯一的区别是,fork为子进程返回0,而子进程的PID>0为父进程。这就是为什么在调用fork之后需要一个条件
因此,在您的子进程中,它将检查返回值0和break,然后进入while循环。父进程将停留在for循环中,在那里它将继续派生子进程,直到完成。然后它将进入while循环
在您的子进程中,wait函数可能返回ECHILD-see,因为没有子进程,所以您应该确保子线程没有调用它,或者确保它们能够优雅地处理错误。抱歉,我的问题是关于等待的条件,而不是叉子,我知道叉子之后需要一些条件来防止无限子代。对不起,我的问题是关于等待的条件,而不是叉子,我知道叉子之后需要一些条件来防止无限子代。