/* * Run as: * pgrm 1 10 1 & -> child diventa zombie * pgrm 1 10 2 & -> father fa gestine SIG_CHLD e figlio diventa zombie * pgrm 1 10 3 & -> father fa SIG_IGN (synch sincrona) e figlio NON diventa zombie * pgrm 1 10 4 & -> father fa wait (synch asincrona) e figlio NON diventa zombie * and check status with -> ps -l */ #include #include #include #include #include static void sigChld ( int signo ) { if (signo == SIGCHLD) printf("Received SIGCHLD\n"); else printf("Received wrong SIGNAL\n"); sleep (5); return; } int main ( int argc, char *argv[] ) { int tC, tF, flag; pid_t pid; tC = atoi (argv[1]); tF = atoi (argv[2]); flag = atoi (argv[3]); pid = fork(); if (pid == 0){ // Child fprintf(stdout, "Chlld sleeps (%d secs) and returns.\n", tC); sleep (tC); exit (1); } else { // Father switch (flag) { case 1: fprintf(stdout, "Father sleeps (%d secs) and returns (child==zombie).\n", tF); sleep (tF); break; case 2: if (signal(SIGCHLD, sigChld) == SIG_ERR) { fprintf (stderr, "Signal Handler Error.\n"); return (1); } fprintf(stdout, "Father sleeps (%d secs) and deals with SIG_CHLD (child=zombie).\n", tF); sleep (tF); break; case 3: if (signal(SIGCHLD, SIG_IGN) == SIG_ERR) { fprintf (stderr, "Signal Handler Error.\n"); return (1); } fprintf(stdout, "Father sleeps (%d secs) and IGNORE signal (child=NOzombie).\n", tF); sleep (tF); break; case 4: fprintf(stdout, "Father waits and sleeps (%d secs) and waits (child=NOzombie).\n", tF); wait ((int *) 0); sleep (tF); break; default: fprintf(stdout, "Father Error.\n"); } } return 0; }