0% found this document useful (0 votes)
5 views

Assignment 4

Uploaded by

titax63591
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Assignment 4

Uploaded by

titax63591
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Signal

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.

Sending and handling a signal:

#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);
}

void sighandler(int signum) {


printf("Caught signal %d, coming out... \n", signum);
exit(1);
}

Ignoring a signal:

#include <stdio.h>

#include <signal.h>
#include <unistd.h>

void handle_sigint(int signum) {


printf("Received Ctrl+C, but ignoring it. \n"); //This function is no more necessary.
}

int main() {
// Press Ctrl+C to ignore (SIGINT)
signal(SIGINT, SIG_IGN);
while (1) {
printf("Press Ctrl+C (ignored)... \n");
sleep(1);
}
return 0;
}

You might also like