Prerequisite : Fork system call, Wait system call
A signal is a software generated interrupt that is sent to a process by the OS because of when user press ctrl-c or another process tell something to this process.
There are fix set of signals that can be sent to a process. signal are identified by integers.
Signal number have symbolic names. For example SIGCHLD is number of the signal sent to the parent process when child terminates.
#define SIGHUP 1 /* Hangup the process */ #define SIGINT 2 /* Interrupt the process */ #define SIGQUIT 3 /* Quit the process */ #define SIGILL 4 /* Illegal instruction. */ #define SIGTRAP 5 /* Trace trap. */ #define SIGABRT 6 /* Abort. */
OS Structures for Signals
- For each process, the operating system maintains 2 integers with the bits corresponding to a signal numbers.
- The two integers keep track of: pending signals and blocked signals
- With 32 bit integers, up to 32 different signals can be represented.
In the example below, the SIGINT ( = 2) signal is blocked and no signals are pending.
A signal is sent to a process setting the corresponding bit in the pending signals integer for the process. Each time the OS selects a process to be run on a processor, the pending and blocked integers are checked. If no signals are pending, the process is restarted normally and continues executing at its next instruction.
If 1 or more signals are pending, but each one is blocked, the process is also restarted normally but with the signals still marked as pending. If 1 or more signals are pending and NOT blocked, the OS executes the routines in the process’s code to handle the signals.
Default Signal Handlers
There are several default signal handler routines. Each signal is associated with one of these default handler routine. The different default handler routines typically have one of the following actions:
- Ign: Ignore the signal; i.e., do nothing, just return
- Term: terminate the process
- Cont: unblock a stopped process
- Stop: block the process
Output: Print hello world infinite times. If user presses ctrl-c to terminate the process because of SIGINT signal sent and its default handler to terminate the process.
hello world hello world hello world terminated
User Defined Signal Handlers
A process can replace the default signal handler for almost all signals (but not SIGKILL) by its user’s own handler function.
A signal handler function can have any name, but must have return type void and have one int parameter.
Example: you might choose the name sigchld_handler for a signal handler for the SIGCHLD signal (termination of a child process). Then the declaration would be:
void sigchld_handler(int sig);
When a signal handler executes, the parameter passed to it is the number of the signal. A programmer can use the same signal handler function to handle several signals. In this case the handler would need to check the parameter to see which signal was sent. On the other hand, if a signal handler function only handles one signal, it isn’t necessary to bother examining the parameter since it will always be that signal number.
^CCaught signal 2 // when user presses ctrl-c ^CCaught signal 2
Sending signals via kill()
We can send a signal using kill() to the process.
int kill(pid_t pid, int signal); pid: id of destination process signal: the type of signal to send Return value: 0 if signal was sent successfully
pid_t iPid = getpid(); /* Process gets its id.*/ kill(iPid, SIGINT); /* Process sends itself a SIGINT signal (commits suicide?)(because of SIGINT signal default handler is terminate the process) */
1. What is the Output of the following program?
Child term due to: Interrupt
2. What is the Output of the following program?
val = 15
3. Consider the following code. What is the output?
counter = 1 //(parent’s handler) counter = 3 //(child’s handler) counter = 5 //(parent’s main)
This article is contributed by Kadam Patel. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
- Communication between two process using signals in C
- Program error signals
- kbhit in C language
- Difference between while(1) and while(0) in C language
- fgets() and gets() in C language
- Stopwatch using C language
- C Language Introduction
- chdir() in C language with Examples
- Difference between Java and C language
- Interesting facts about C Language
- How to clear console in C language?
- Constants vs Variables in C language
- isalnum() function in C Language
- isupper() function in C Language
- lvalue and rvalue in C language