I got 2 cod of parent.c and child.c here:
parent.c:
#include <sys/wait.h>
#define NULL 0
int main(void)
{
if(fork()==0){execve("child",NULL,NULL);exit(0);}
printf("Process[%d]arent in execution\n",getpid());
sleep(2);
if(wait(NULL)>0)printf("Process[%d]arent detects terminatin child",getpid());
printf("Process[%d]arent 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 the result of execution of parent.c is:
Parent in execution.
child in execution.
child terminating.
Parent detects terminating.
Parent terminating.
It was said that execve() makes the child program take place of the parent,so why
printf("Process[%d]arent in execution\n",getpid());
was executed first?How could the whole procedure happen?
I'm vey confused.
Thanks a lot!
parent.c:
#include <sys/wait.h>
#define NULL 0
int main(void)
{
if(fork()==0){execve("child",NULL,NULL);exit(0);}
printf("Process[%d]arent in execution\n",getpid());
sleep(2);
if(wait(NULL)>0)printf("Process[%d]arent detects terminatin child",getpid());
printf("Process[%d]arent 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 the result of execution of parent.c is:
Parent in execution.
child in execution.
child terminating.
Parent detects terminating.
Parent terminating.
It was said that execve() makes the child program take place of the parent,so why
printf("Process[%d]arent in execution\n",getpid());
was executed first?How could the whole procedure happen?
I'm vey confused.
Thanks a lot!