The initial code contains a function waitfg:
1 /*
2 * waitfg - Block until process pid is no longer the foreground process
3 * If pid is no longer in the job list, just return.
4 * Otherwise, wait while the state of pid is FG.
5 * Wait 1 sec and check the state again. Keep waiting for 1 sec and
6 * checking the state until the state is not FG.
7 */
8 void waitfg(pid_t pid)
9 {
10 return;
11 }
It is a bad idea to have this function and the SIGCHLD handler both call waitpid.
So this function should not!