1. Write a program that blocks and then ignores the signal, SIGINT in 7 steps as
ID: 3602536 • Letter: 1
Question
1. Write a program that blocks and then ignores the signal, SIGINT in 7 steps as
follows
1) Set up the handler for the signal SIGINT
• Use signal( ) to set up a handler for SIGINT
• The handler function should print out signal number and description about the signal : use the passed argument for the signal number and strsignal( ) for description
2) Block SIGINT for 10 seconds
• Use sigprocmask( ) to block SIGINT and
• Use sleep( ) to keep 10 seconds
• Try Control-C (it generates a signal SIGINT) to see the pending signals
include SIGINT at the next step (the handler should not be called).
3) Show pending signals
• Use sigpending( ) and printSigset( )
• Pending signals will be shown
4) Ignore SIGINT
• Use SIG_IGN option for signal( )
5) Show pending signals again
• Use sigpending( ) and printSigset( )
• Nothing will be shown
6) Set up the handler for SIGINT again
• Use signal( )
7) Unblock SIGINT
• Use sigprocmask( )
• Use sleep( ) to keep for 10 seconds
• The handler function should be working printing out “I caught signal 2 (Interrupt)” when you type in “Control-C”.
The functions introduced in each step are required to be used for this
homework.
You can also use other related functions covered in the lecture notes.
Your program should print out a statement indicating each step, for example,
$ ./homework4
Step 1) Setting up …
Step 2) Blocking (10 seconds) …
^CStep 3) Pending signals are 2 (Interrupt) Step 4) Ignoring …
Step 5) Pending signals are …
Step 6) Setting up again … Step 7) Unblocking …
^CI caught signal 2 (Interrupt)
Explanation / Answer
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
void /* Print list of signals within a signal set */
printSigset(FILE *of, const char *prefix, const sigset_t *sigset)
{
int sig, cnt;
cnt = 0;
for (sig = 1; sig < NSIG; sig++) {
if (sigismember(sigset, sig)) {
cnt++;
fprintf(of, "%s%d (%s) ", prefix, sig, strsignal(sig));
}
}
if (cnt == 0)
fprintf(of, "%s<empty signal set> ", prefix);
}
void signal_callback_handler(int signum)
{
/* print signal num and description*/
printf(" Signal number: %d ",signum);
printf(" Singal description: %s ",strsignal(signum));
}
int main() {
printf(" Step 1) Setting up...");
fflush(stdout);
/*set up signal handler*/
signal(SIGINT, signal_callback_handler);
sigset_t signal_set;
/*add SIGINT to singal_set*/
sigaddset(&signal_set, SIGINT);
printf(" Step 2) Blocking (10 seconds) ...");
fflush(stdout);
/*block the SIGINT signal*/
sigprocmask(SIG_BLOCK, &signal_set, NULL);
sleep(10);
sigset_t pending_set;
/*get pending signal set*/
sigpending(&pending_set);
/*print pending signal set*/
printSigset(stdout," Step 3) Pending Signals are ", &pending_set);
fflush(stdout);
printf(" Step 4) Ignoring ...");
fflush(stdout);
/*ignore SIGINT*/
signal(SIGINT, SIG_IGN);
sigset_t pending_set1;
/*get pending signal set again*/
sigpending(&pending_set1);
/*print pending signal set again*/
printSigset(stdout," Step 5) Pending Signals are ", &pending_set1);
fflush(stdout);
/*set up another singal handler*/
printf(" Step 6) Setting up again...");
signal(SIGINT, signal_callback_handler);
sigset_t signal_set1;
/*add SIGINT to singal_set*/
sigaddset(&signal_set1, SIGINT);
printf(" Step 7) Unblocking...");
fflush(stdout);
/*unblock the SIGINT signal*/
sigprocmask(SIG_UNBLOCK, &signal_set1, NULL);
/*sleep for 10 secs*/
sleep(10);
printf(" I caught signal 2(Interrupt) ");
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.