Signals : Detecting the termination of multiple child processes : Implement the C program to demonstrate the use of SIGCHLD signal. A parent process Creates multiple child process (minimum three child processes). Parent process should be Sleeping until it creates the number of child processes. Child processes send SIGCHLD signal to parent process to interrupt from the sleep and force the parent to call wait for the Collection of status of terminated child processes.

//Program: os5c.c

#include <stdlib.h> 
#include <unistd.h> /* needed for fork() and getpid() */
#include <signal.h> /* needed for signal */
#include <stdio.h>  
#include<sys/wait.h>

#define NUMPROCS 4

int nprocs;
void catch(int);

int main()
{
    pid_t pid;
    int i;

    signal(SIGCHLD,catch);

    for(i=0;i<NUMPROCS;i++)
    {
        if(pid=fork()==0)
        {
            printf("\tChild[%d] : Child PID=%d , Sleeping for %d seconds\n",i,getpid(),i);
            sleep(i);
            printf("\tChild[%d]: I'm Exitinng!!!\n",i);
            exit(i);
        }
        else
            nprocs++;
    }
    printf("Parent: Going to sleep!!!\n");

    while(nprocs!=0)
    {
        printf("Parent : Sleeping\n");
        sleep(60);
    }
    printf("Parent : Exiting\n");
    exit(0);
}

void catch(int snum)
{
    pid_t pid;
    int status;
    pid=wait(&status);
    printf("parent: child process pid=%d exited with value %d\n",pid, WEXITSTATUS(status));
    nprocs--;
    signal(SIGCHLD, catch);
}

//OutPut

huzaif@huzaif-Lenovo-Z51-70:~/Desktop/os/5$ gcc os5c.c
huzaif@huzaif-Lenovo-Z51-70:~/Desktop/os/5$ ./a.out
    Child[0] : Child PID=10940 , Sleeping for 0 seconds
    Child[0]: I'm Exitinng!!!
Parent: Going to sleep!!!
Parent : Sleeping
    Child[1] : Child PID=10941 , Sleeping for 1 seconds
parent: child process pid=10940 exited with value 0
Parent : Sleeping
    Child[2] : Child PID=10942 , Sleeping for 2 seconds
    Child[3] : Child PID=10943 , Sleeping for 3 seconds
    Child[1]: I'm Exitinng!!!
parent: child process pid=10941 exited with value 1
Parent : Sleeping
    Child[2]: I'm Exitinng!!!
parent: child process pid=10942 exited with value 2
Parent : Sleeping
    Child[3]: I'm Exitinng!!!
parent: child process pid=10943 exited with value 3
Parent : Exiting
huzaif@huzaif-Lenovo-Z51-70:~/Desktop/os/5$ 
Signals : Detecting the termination of multiple child processes : Implement the C program to demonstrate the use of SIGCHLD signal. A parent process Creates multiple child process (minimum three child processes). Parent process should be Sleeping until it creates the number of child processes. Child processes send SIGCHLD signal to parent process to interrupt from the sleep and force the parent to call wait for the Collection of status of terminated child processes. Signals : Detecting the termination of multiple child processes : Implement the C program to demonstrate the use of SIGCHLD signal. A parent process Creates multiple child process (minimum three child processes). Parent process should be Sleeping until it creates the number of child processes. Child processes send SIGCHLD signal to parent process to interrupt from the sleep and force the parent to call wait for the Collection of status of terminated child processes. Reviewed by Huzaif Sayyed on April 17, 2017 Rating: 5

No comments:

Powered by Blogger.