0% found this document useful (0 votes)
84 views4 pages

Assignment08 20208038

The document describes two programming assignments involving signals in C. The first asks to implement a program that uses sigaction() to specify the handler for the SIGCHLD signal. The code provided forks a child process that terminates itself with SIGKILL, and the parent waits for the child to die while printing messages. The second asks to modify a similar program to mask other signals while the handler is processing, by using sigprocmask() in the handler to block signals and remain executing in a loop before unblocking. The code provided defines signal handlers for the child and parent that print the signal, sleep for 5 seconds, and unblock signals before returning.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
84 views4 pages

Assignment08 20208038

The document describes two programming assignments involving signals in C. The first asks to implement a program that uses sigaction() to specify the handler for the SIGCHLD signal. The code provided forks a child process that terminates itself with SIGKILL, and the parent waits for the child to die while printing messages. The second asks to modify a similar program to mask other signals while the handler is processing, by using sigprocmask() in the handler to block signals and remain executing in a loop before unblocking. The code provided defines signal handlers for the child and parent that print the signal, sleep for 5 seconds, and unblock signals before returning.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Cyril Kevin

20208038

Programming Lab Assignment week - 7


1. Implement program # 3 of assignment for week # 7, and specify the ac on to be
associated with the SIGCHLD signal using sigac on() func on.

CODE:-
#include<stdio.h>
#include<signal.h>
#include<wait.h>

int stat;

void handler(int sig){


prin ("Child died\n");
if (WIFSIGNALED(stat))
psignal(WTERMSIG(stat), "Child terminated");
}

int main(void){
pid_t pid=fork();

if(pid==0){
int i;
for(i=0;i<10000;i++);
kill(getpid(), SIGKILL);
}
else{
if(signal(SIGCHLD,SIG_IGN)!=SIG_IGN){
int iret;
struct sigac on sAc on;
sAc on.sa_ ags = 0;
sAc on.sa_handler = handler;
sigemptyset(&sAc on.sa_mask);
iret=sigac on(SIGCHLD,&sAc on,NULL);
}
prin ("wai ng for child to die\n");
wait(&stat);
}
}

tf

ti
ti

tf

ti

ti
ti
fl

ti
ti

ti

ti
ti

ti

2. Implement program # 4 of assignment for week # 7, using sigac on() and modify
the program to mask any other signal that appears, while the signal handler is busy
processing a signal . Hint: De ne the signal_handler in such a way, that it remains
under execu on for some me (loop may be used), when another signal gets
generated.

CODE:-
#include<stdio.h>
#include<signal.h>

int cpid;
sigset_t sSet;

void childhandler(int sig){


sigemptyset(&sSet);int iRet;
sigaddset(&sSet, sig);
iRet = sigprocmask(SIG_BLOCK, &sSet, NULL);

prin ("%d",sig);
prin ("Signal number %d recieved\n",sig);
if(sig==SIGQUIT){
exit(0);
}
sleep(5000);
iRet = sigprocmask(SIG_UNBLOCK, &sSet, NULL);
}

void parenthandler(int sig){


sigemptyset(&sSet);
int iRet;
sigaddset(&sSet, sig);
iRet = sigprocmask(SIG_BLOCK, &sSet, NULL);
kill(cpid,sig);
if(sig==SIGQUIT){
exit(0);
}
sleep(5000);
iRet = sigprocmask(SIG_UNBLOCK, &sSet, NULL);
}

int main(void){
pid_t pid=fork();
if(pid==0){






tf
tf

ti

ti

fi

ti
int i;
for(i=1;i<33;i++)
if( signal(i,SIG_IGN)!=SIG_IGN){
int iret;
struct sigac on sAc on;
sAc on.sa_ ags = 0;
sAc on.sa_handler = childhandler;
sigemptyset(&sAc on.sa_mask);
iret=sigac on(i,&sAc on,NULL);
}
while(1)
pause();
}
else{
cpid=pid;
int i;
for(i=1;i<33;i++)
if( signal(i,SIG_IGN)!=SIG_IGN){
int iret;
struct sigac on sAc on;
sAc on.sa_ ags = 0;
sAc on.sa_handler = parenthandler;
sigemptyset(&sAc on.sa_mask);
iret=sigac on(i,&sAc on,NULL);
}
while(1)
pause();
}

3. Implement a program using sigac on() and signal_set to block any of the signals SIGINT,
SIGHUP and SIGABRT. The program should also display the list of blocked/pending signals.

CODE:-
#include<stdio.h>
#include<signal.h>
#include<sys/wait.h>















ti
ti
ti
ti

ti
ti
ti
ti
fl
fl
ti
ti
ti
ti

ti
ti
ti

#include<unistd.h>
#include<stdlib.h>
int main()
{
sigset_t sset,s2;
sigemptyset(&sset);
sigaddset(&sset,SIGINT);
sigaddset(&sset,SIGHUP);
sigaddset(&sset,SIGABRT);

sigprocmask(SIG_SETMASK,&sset,NULL);
kill(getpid(),SIGINT);
kill(getpid(),SIGHUP);
kill(getpid(),SIGABRT);
sigpending(&s2);

if(sigismember(&s2,SIGINT))
prin ("SIGINT IS blocked\n");

if(sigismember(&s2,SIGHUP))
prin ("SIGHUP IS blocked\n");

if(sigismember(&s2,SIGABRT))
prin ("SIGABRT IS blocked\n");

tf
tf
tf

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy