Assignment 4
Assignment 4
A signal is an event generated by the UNIX and Linux systems in response to some condition, upon receipt of which a
process may in turn take some action. We use the term raise to indicate the generation of a signal, and the term catch
to indicate the receipt of a signal. Signals are raised by some error conditions, such as memory segment violations,
floating-point processor errors, or illegal instructions. They are generated by the shell and terminal handlers to cause
interrupts and can also be explicitly sent from one process to another as a way of passing information or modifying
behavior. In all these cases, the programming interface is the same. Signals can be raised, caught and acted upon, or
(for some at least) ignored.
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <signal.h>
void sighandler(int);
int main () {
signal(SIGINT, sighandler);
while(1) {
printf(“Going to sleep for a second... \n”);
sleep(1);
}
return(0);
}
Ignoring a signal:
#include <stdio.h>
#include <signal.h>
#include <unistd.h>
int main() {
// Press Ctrl+C to ignore (SIGINT)
signal(SIGINT, SIG_IGN);
while (1) {
printf("Press Ctrl+C (ignored)... \n");
sleep(1);
}
return 0;
}