Introduction
An operating system (OS) is a software program that serves as a conduit between computer hardware and the user. It is a piece of software that coordinates the execution of application programs, software resources, and computer hardware. It also aids in the control of software and hardware resources such as file management, memory management, input/output, and a variety of peripheral devices such as a disc drive, printers, and so on. To run other applications, every computer system must have at least one operating system. Browsers, MS Office, Notepad Games, and other applications require an environment to execute and fulfill their functions.
In this article, we will take a look at Communication between Two Signals. Before that let's take a look at signals
Must Read Evolution of Operating System, Multiprogramming vs Multitasking
Signal
A signal is a type of notification to a process indicating the occurrence of an event. It is also called the software interrupt and is not predictable to know its occurrence. Thus it is also known as an asynchronous event.
A signal can be specified with a name or a number; usually, signal names start with SIG.
To get the signal commands supported by your system, use the “kill -l” (l is for list) command; the following is the list of signal commands.
1) SIGHUP 2) SIGINT 3) SIGQUIT 4) SIGILL 5) SIGTRAP 6) SIGABRT 7) SIGBUS 8) SIGFPE 9) SIGKILL 10) SIGUSR1 11) SIGSEGV 12) SIGUSR2 13) SIGPIPE 14) SIGALRM 15) SIGTERM 16) SIGSTKFLT 17) SIGCHLD 18) SIGCONT 19) SIGSTOP 20) SIGTSTP 21) SIGTTIN 22) SIGTTOU 23) SIGURG 24) SIGXCPU 25) SIGXFSZ 26) SIGVTALRM 27) SIGPROF 28) SIGWINCH 29) SIGIO 30) SIGPWR 31) SIGSYS 34) SIGRTMIN 35) SIGRTMIN+1 36) SIGRTMIN+2 37) SIGRTMIN+3 38) SIGRTMIN+4 39) SIGRTMIN+5 40) SIGRTMIN+6 41) SIGRTMIN+7 42) SIGRTMIN+8 43) SIGRTMIN+9 44) SIGRTMIN+10 45) SIGRTMIN+11 46) SIGRTMIN+12 47) SIGRTMIN+13 48) SIGRTMIN+14 49) SIGRTMIN+15 50) SIGRTMAX-14 51) SIGRTMAX-13 52) SIGRTMAX-12 53) SIGRTMAX-11 54) SIGRTMAX-10 55) SIGRTMAX-9 56) SIGRTMAX-8 57) SIGRTMAX-7 58) SIGRTMAX-6 59) SIGRTMAX-5 60) SIGRTMAX-4 61) SIGRTMAX-3 62) SIGRTMAX-2 63) SIGRTMAX-1 64) SIGRTMAX |
Also see, Introduction to System Calls
Signal handler
An application program can specify a function called a signal handler to be invoked by receiving a specific signal. When a signal handler is invoked on receiving a signal, it is said to catch that signal. A process can deal with a signal in any one of the following ways:
- The process can either block the signal (some signals cannot be ignored)
- The process can let the default action happen
- The process can catch the signal with a handler.
This blog teaches about the communication between two processes using signals. The communication between child process and parent processes is done by the use of different system calls- fork(), kill(), and signal() system calls.
- fork() - fork() system call creates the child process from the parent process. The pid can decide whether the process is the child process or the parent process. For child process, pid must be equal to zero, and for parent process, pid is equal to the process id of its child process.
- kill() - The kill() function signals the process or a group of processes. The process (or group of processes) to which the signal is to be sent is specified by the pid. On successful completion of kill(), 0 is returned; otherwise, -1 is returned.
- signal() - The child process picks up the signals with signal() and calls appropriate functions.
The following code explains the communication between two processes using the signal:
// C program to implement sighup(), sigint() and sigquit() signal functions to show the communication between processes
#include <stdio.h>
#include <signal.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
// function declaration of sighup, sigint and sigquit functions
void sighup();
void sigint();
void sigquit();
// main function or driver code
void main()
{
int pid;
// pid variable, which will be used later to identify the process, whether it is child process or parent process
// to get the child process
if ((pid = fork()) < 0)
{
perror("fork");
exit(1);
}
if (pid == 0)
{ /* child process, since pid equals to zero for child process */
signal(SIGHUP, sighup);
signal(SIGINT, sigint);
signal(SIGQUIT, sigquit);
for (;;)
; /* infinite loop i.e. loop for ever */
}
else /* parent process*/
{ // pid hold the process id of child process
printf("\nPARENT: sending SIGHUP\n\n");
kill(pid, SIGHUP);
sleep(3); // pause for 3 seconds
printf("\nPARENT: sending SIGINT\n\n");
kill(pid, SIGINT);
sleep(3); // pause for 3 seconds
printf("\nPARENT: sending SIGQUIT\n\n");
kill(pid, SIGQUIT);
sleep(3); // pause for 3 seconds
}
}
// function definition of sighup()
void sighup()
{
signal(SIGHUP, sighup); /* reset signal */
printf("CHILD: I have received a SIGHUP\n");
}
// function definition of sigint()
void sigint()
{
signal(SIGINT, sigint); /* reset signal */
printf("CHILD: I have received a SIGINT\n");
}
// function definition of sigquit()
void sigquit()
{
printf("My DADDY has Killed me!!!\n");
exit(0);
}
Output:
The following screenshot shows the output shown in the terminal after executing the above code.
Recommended Topic, FCFS Scheduling Algorithm, Open Source Operating System
Frequently Asked Questions
What are system calls?
System calls are special functions that manage OS routines that occur in kernel mode.
What is kernel mode?
Kernel mode, also known as system mode, is a central processing unit (CPU) operating mode. While processes run in kernel mode, they have unrestricted access to the hardware.
What do you mean by preemption of a process?
Preemption is simply one of the means by which the operating system changes the process executing on a CPU.