What is the reason for the code result?
And what happens when an exception happens in the fork()
?
#include<stdio.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/wait.h>
int main(){
int pid=fork();
if(pid==0){
int child=getpid();
printf("child: parent %d\n",getppid());
sleep(4);
printf("child: parent %d\n",getppid());
sleep(100);
}
else{
int parent=getpid();
printf("parent: parent %d\n",getppid());
sleep(2);
int zero=0;
int i=3/zero;
}
return 0;
}
And here's the output:
parent: parent 63742
child: parent 63825
Floating point exception (core dumped)
ubunto@ubuntu:~/Desktop$ child: parent 4497