Open In App
Related Articles

Communication between two process using signals in C

Like Article
Save Article
Report issue

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 program to implement sighup(), sigint()
// and sigquit() signal functions
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
// function declaration
void sighup();
void sigint();
void sigquit();
// driver code
void main()
    int pid;
    /* get child process */
    if ((pid = fork()) < 0) {
    if (pid == 0) { /* child */
        signal(SIGHUP, sighup);
        signal(SIGINT, sigint);
        signal(SIGQUIT, sigquit);
        for (;;)
            ; /* loop for ever */
    else /* parent */
    { /* pid hold id of child */
        printf("\nPARENT: sending SIGHUP\n\n");
        kill(pid, SIGHUP);
        sleep(3); /* pause for 3 secs */
        printf("\nPARENT: sending SIGINT\n\n");
        kill(pid, SIGINT);
        sleep(3); /* pause for 3 secs */
        printf("\nPARENT: sending SIGQUIT\n\n");
        kill(pid, SIGQUIT);
// sighup() function definition
void sighup()
    signal(SIGHUP, sighup); /* reset signal */
    printf("CHILD: I have received a SIGHUP\n");
// sigint() function definition
void sigint()
    signal(SIGINT, sigint); /* reset signal */
    printf("CHILD: I have received a SIGINT\n");
// sigquit() function definition
void sigquit()
    printf("My DADDY has Killed me!!!\n");




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.

Last Updated : 03 May, 2023
Like Article
Save Article
Share your thoughts in the comments
Similar Reads