h******m 发帖数: 10 | 1 I got 2 cod of parent.c and child.c here:
parent.c:
#include
#define NULL 0
int main(void)
{
if(fork()==0){execve("child",NULL,NULL);exit(0);}
printf("Process[%d]:Parent in execution\n",getpid());
sleep(2);
if(wait(NULL)>0)printf("Process[%d]:Parent detects terminatin
child",getpid());
printf("Process[%d]:Parent terminating\n",getpid());
}
child.c:
int main()
{
printf("Process[%d]:child in execution\n",getid());
sleep(1);
printf("Process[%d]:child terminating\n",getid());
}
and | o***z 发帖数: 133 | 2 the control wasn't switched from parent to child until this was done | n******t 发帖数: 4406 | 3
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
It is no way to make sure child exec first after fork call
unless you synchronize it.
【在 h******m 的大作中提到】 : I got 2 cod of parent.c and child.c here: : parent.c: : #include : #define NULL 0 : int main(void) : { : if(fork()==0){execve("child",NULL,NULL);exit(0);} : printf("Process[%d]:Parent in execution\n",getpid()); : sleep(2); : if(wait(NULL)>0)printf("Process[%d]:Parent detects terminatin
| h******m 发帖数: 10 | 4
parent,so
So the lines after
if(fork()==0){execve("child",NULL,NULL);exit(0);}
were executed before child,right? But why it was said that the child took
over?And why
if(wait(NULL)>0)printf("Process[%d]:Parent detects terminatin
was executed after the termination of the child?
Thanks for attention.
【在 n******t 的大作中提到】 : : ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ : It is no way to make sure child exec first after fork call : unless you synchronize it.
| N*********r 发帖数: 40 | 5 if( fork() == 0 ) { /* A */ }
else { /* B */ }
A is the child process and B is the parent process. As netghost has said,
there is no way to make sure A get executed first or B get executed first.
now in A, you called execve. so another executable 'child' takes over the
child process. but it doesn't affect the parent process, nor the timing
relationship between parent and child.
【在 h******m 的大作中提到】 : : parent,so : So the lines after : if(fork()==0){execve("child",NULL,NULL);exit(0);} : were executed before child,right? But why it was said that the child took : over?And why : if(wait(NULL)>0)printf("Process[%d]:Parent detects terminatin : was executed after the termination of the child? : Thanks for attention.
|
|