write a c program to avoid zombie process by forking twice code example

Example: write a c program to avoid zombie process by forking twice

#include<stdio.h>#include <sys/wait.h>#include<errno.h>#include<stdlib.h>int main(){pid_t pid;if ((pid = fork()) < 0){printf("fork error");}else if (pid == 0){ /* first child */if ((pid = fork()) < 0)printf("fork error");else if (pid > 0)exit(0);sleep(2);printf("second child, parent pid = %d\n", getppid()); exit(0);}if (waitpid(pid, NULL, 0) != pid) /* wait for first child */printf("waitpid error");exit(0);}