Communication between two process using signals in C
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():
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.
Please Login to comment...