Вы находитесь на странице: 1из 4

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():
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;

/* get child process */


if ((pid = fork()) < 0) {
perror("fork");
exit(1);
}

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);
sleep(3);
}
}

// 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");
exit(0);
}

Output:

Recommended Posts:

Message based Communication in IPC (inter process communication)


Inter Process Communication
Inter-process Communication using a shared stack
Signals in C language
Program error signals
Operating System | Process Table and Process Control Block (PCB)
Create n-child process from same parent process using fork() in C
Interprocess Communication: Methods
Process states and Transitions in a UNIX Process
Pass the value from child process to parent process
Get/Set process resource limits in C
Process Synchronization | Monitors
Operating System | Process Synchronization | Set 2
Operating System | Process Scheduler
Priority of process in Linux | nice value

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++

Most visited in Linux-Unix

aptitude command in Linux with examples


Basic Operators in Shell Scripting
Basic Shell Commands in Linux
Bash program to check if the Number is a Prime or not
Conditional Statements | Shell Script

Related Articles

kill command in Linux with Examples


sdiff command in Linux with Examples
Program for Deadlock free condition in Operating System
Computer Organization | RAM vs ROM
Fixed (or static) Partitioning in Operating System
nslookup command in Linux with Examples
Operating System | Free space management
Operating System | Kernel I/O Subsystem (I/O System)
aptitude command in Linux with examples
What to do when load on a Linux based web server goes high?
mkdir command in Linux with Examples
cal command in Linux with Examples
Basic Shell Commands in Linux
Create Directory or Folder with C/C++ Program
bg command in Linux with Examples

Advertise Here

Вам также может понравиться