Академический Документы
Профессиональный Документы
Культура Документы
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():
filter_none
brightness_4
// 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;
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);
{
signal(SIGHUP, sighup); /* reset signal */
printf("CHILD: I have received a SIGHUP\n");
}
{
signal(SIGINT, sigint); /* reset signal */
printf("CHILD: I have received a SIGINT\n");
}
Output:
Recommended Posts:
Kishor Mishra
Check out this Author's contributed articles.
If you like GeeksforGeeks and would like to contribute, you can also write an
article using contribute.geeksforgeeks.org or mail your article to
contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main
page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the
"Improve Article" button below.
Article Tags : C
Linux-Unix
Operating Systems
linux-command
Practice Tags : Operating Systems
C
thumb_up
Be the First to upvote.
2
Based on 1 vote(s)
Please write to us at contribute@geeksforgeeks.org to report any issue with the
above content.
Post navigation
Previous
first_page Pre-increment and Post-increment in C/C++
Next
last_page
Tasks in Real Time systems
Writing code in comment? Please use ide.geeksforgeeks.org, generate link and share
the link here.
Most popular in C
Dividing a Large file into Separate Modules in C/C++, Java and Python
Different ways to Initialize all members of an array to the same value in C
Program to copy the contents of one array into another in the reverse order
Program to check if two strings are same or not
Measure execution time with high precision in C/C++
Related Articles
Advertise Here