Communication between two process using signals in C
Last Updated :
03 Feb, 2025
Prerequisite : C signal handling In this post, the communication between child and parent processes is done using kill() and signal(), fork() system call.
- fork() creates the child process from the parent. The pid can be checked to decide whether it is the child (if pid == 0) or the parent (pid = child process id).
- The parent can then send messages to child using the pid and kill().
- The child picks up these signals with signal() and calls appropriate functions.
Example of how 2 processes can talk to each other using kill() and signal():
C
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
// function declaration
void sighup();
void sigint();
void sigquit();
void main()
{
int pid;
/* get child process */
if ((pid = fork()) < 0) {
perror("fork");
exit(1);
}
/* child */
if (pid == 0) {
signal(SIGHUP, sighup);
signal(SIGINT, sigint);
signal(SIGQUIT, sigquit);
for (;;)
/* loop forever */
;
}
/* parent */
else
/* pid holds id of child */
{
write(STDOUT_FILENO, "\nPARENT: sending SIGHUP\n\n", 25);
kill(pid, SIGHUP);
/* pause for 3 secs */
sleep(3);
write(STDOUT_FILENO, "\nPARENT: sending SIGINT\n\n", 25);
kill(pid, SIGINT);
/* pause for 3 secs */
sleep(3);
write(STDOUT_FILENO, "\nPARENT: sending SIGQUIT\n\n", 26);
kill(pid, SIGQUIT);
sleep(3);
}
}
// sighup() function definition
void sighup()
{
/* reset signal */
signal(SIGHUP, sighup);
write(STDOUT_FILENO, "CHILD: I have received a SIGHUP\n", 31);
}
// sigint() function definition
void sigint()
{
/* reset signal */
signal(SIGINT, sigint);
write(STDOUT_FILENO, "CHILD: I have received a SIGINT\n", 32);
}
// sigquit() function definition
void sigquit()
{
write(STDOUT_FILENO, "My DADDY has Killed me!!!\n", 26);
exit(0);
}
Output: 
FAQ:
What is signal handling in C?
Signal handling is a way of dealing with interrupts, exceptions, and signals sent to a process by the operating system or another process. In C, we can use the signal() function to register signal handlers to handle specific signals.
What is the purpose of fork() system call in the program?
The fork() system call creates a new child process by duplicating the calling process. The child process is an exact copy of the parent process, including all its memory and open file descriptors.
How does the parent process communicate with the child process in this program?
The parent process sends signals to the child process using the kill() function, passing the child process ID and the signal number as arguments.
How does the child process handle signals sent by the parent process?
The child process sets up signal handlers for the signals it expects to receive using the signal() function. When a signal is received, the corresponding signal handler function is called.
What is the purpose of the for(;;) loop in the child process?
The for(;;) loop is an infinite loop that keeps the child process running and waiting for signals to be received. Without this loop, the child process would exit immediately after setting up its signal handlers.
Similar Reads
Getting System and Process Information Using C Programming and Shell in Linux Whenever you start a new process in Linux it creates a file in /proc/ folder with the same name as that of the process id of the process. In that folder, there is a file named "status" which has all the details of the process. We can get those Process Information Through shell as follows: cat /proc/
2 min read
How to execute zombie and orphan process in a single program? Prerequisite: Zombie and Orphan Processes in C Zombie Process: A zombie process is a process that has completed execution but still has an entry in the process table. This entry is still needed to allow the parent process to read its child's exit status. A process that terminates cannot leave the sy
2 min read
Factorial calculation using fork() in C for Linux Write a Unix C program using the fork() system call that generates the factorial and gives a sequence of series like 1, 2, 6, 24, 120... in the child process. The number of the sequence is provided in the command line. Examples: Input :gfg@ubuntu:~/$ gcc -o fork fork.c gfg@ubuntu:~/$ ./fork 6 Output
3 min read
Zombie and Orphan Processes in C Prerequisite: fork() in C Zombie Process: A process which has finished the execution but still has entry in the process table to report to its parent process is known as a zombie process. A child process always first becomes a zombie before being removed from the process table. The parent process re
2 min read
Pass the value from child process to parent process Prerequisite: Pipe() and Fork() Basic Write a C program in which the child process takes an input array and send it to the parent process using pipe() and fork() and then print it in the parent process. Examples: Suppose we have an array a[]={1, 2, 3, 4, 5} in child process, then output should be 1
2 min read
raise() function in C++ csignal header file declared the function raise() to handle a particular signal. Signal learns some unusual behavior in a program, and calls the signal handler. It is implemented to check if the default handler will get called or it will be ignored. Syntax: int raise ( int signal_ ) Parameter: The f
3 min read