0% found this document useful (0 votes)
69 views2 pages

Progm 4

This C program code handles signals using signal handlers. It sets handlers for SIGINT, SIGQUIT, SIGALRM, and SIGCHLD signals. It uses alarm to trigger SIGALRM after 5 seconds, forks a child process, and the signal handler prints the exited child process ID. The output shows the different signal handlers executing and the child process exiting.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
69 views2 pages

Progm 4

This C program code handles signals using signal handlers. It sets handlers for SIGINT, SIGQUIT, SIGALRM, and SIGCHLD signals. It uses alarm to trigger SIGALRM after 5 seconds, forks a child process, and the signal handler prints the exited child process ID. The output shows the different signal handlers executing and the child process exiting.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 2

PROGRAM CODE

#include<stdio.h>
#include<signal.h>
#include<stdlib.h>
int keep_going =1;
void siginthandler(int sig)
{
printf("You Pressed Ctrl + C ending\n");
exit(1);
}
void sigquithandler(int sig)
{
printf("You pressed Ctrl + \\ ending\n");
exit(1);
}
void catch_alarm(int sig)
{
printf("Alarm Occurred\n");
keep_going =0;
}
void chldhandler(int sig)
{
int id;
id = wait(NULL);
printf("Pid %d exited.\n",id);
}
int main()
{
signal(SIGQUIT,sigquithandler);
signal(SIGINT,siginthandler);
signal(SIGALRM,catch_alarm);
alarm(5);
while(keep_going);
signal(SIGCHLD,chldhandler);
if(!fork())
{
printf("Child pid is %d\n",getpid());
exit(0);
}
printf("Parent process is %d\n",getpid());

getchar();
return 0;
}

EXECUTION STEPS
gcc signal.c
./a.out

OUTPUT
Case 1:
You Pressed Ctrl + C ending
Case 2:
You pressed Ctrl + \ ending
Case 3:
Alarm Occurred
Parent process is 5244
Child pid is 5246
Pid 5246 exited.

You might also like