I am reading the book Advanced Programming in the Unix Environment.
There is a test program to test the fork function. It works well in my Ubuntu. But what I confused is that why there is no command indicator prompt after the child process exits. The original program is like the below.
#include <stdio.h>
#include <unistd.h>
#include <sys/wait.h>
int main()
{
pid_t pid;
if ((pid = fork()) < 0)
{
printf("fork error.\n");
return 1;
}
else if (pid == 0)
{
if ((pid = fork()) < 0)
{
printf("fork error.\n");
return 1;
}
else if (pid > 0)
{
printf("first child %d exit.\n", getpid());
exit(0);
}
sleep(2);
printf("second child %d has parent pid = %d.\n",getpid(), getppid());
exit(0);
}
printf("waitid %d.\n", pid);
if (waitpid(pid, NULL, 0) != pid)
{
printf("waitid error.\n");
return 1;
}
printf("process %d will exit..\n", getpid());
exit(0);
}
Then I run
jerryli@ubuntu:~/project/unix$ ./p183.o
waitid 2256
first child 2256 exit.
process 2255 will exit..
jerryli@ubuntu:~/project/unix$ second child 2257 has parent pid = 1.
After above line prompt, there is nothing continue. I expect it would show
jerryli@ubuntu:~/project/unix$
In order to have above indicator, I have to type Enter.
Could you know the reason? Is that because the second child's parent process becomes the system init process?