avatar
fork() and execve() in Unix# Unix - 噫吁兮,危乎高哉
h*m
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
avatar
o*z
2
the control wasn't switched from parent to child until this was done
avatar
n*t
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

avatar
h*m
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.

avatar
N*r
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.

相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。