Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Aptitude
  • Engineering Mathematics
  • Discrete Mathematics
  • Operating System
  • DBMS
  • Computer Networks
  • Digital Logic and Design
  • C Programming
  • Data Structures
  • Algorithms
  • Theory of Computation
  • Compiler Design
  • Computer Org and Architecture
Open In App
Next Article:
Communication between two process using signals in C
Next article icon

Communication between two process using signals in C

Last Updated : 03 Feb, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

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.


Next Article
Communication between two process using signals in C

K

Kishor Mishra
Improve
Article Tags :
  • Misc
  • Operating Systems
  • Linux-Unix
  • C Language
Practice Tags :
  • Misc

Similar Reads

    Inter-process Communication using a shared stack
    Inter Process Communication through shared memory is a concept where two or more process can access the common memory.the communication is done via this shared memory where changes made by one process can be viewed by another process. So, we can use one stack as a shared memory, where users(processe
    8 min read
    Chat application between two processes using signals and shared memory
    Prerequisite: C signal handling, IPC through shared memory A signal is used in the UNIX system to notify a process that a particular event has occurred. A signal may be received either synchronously or asynchronously depending on the source and the reason for the event being signalled. A signal must
    3 min read
    Inter Process Communication (IPC)
    Processes need to communicate with each other in many situations. Inter-Process Communication or IPC is a mechanism that allows processes to communicate. It helps processes synchronize their activities, share information, and avoid conflicts while accessing shared resources.Types of Process Let us f
    5 min read
    Interprocess Communication in Distributed Systems
    Interprocess Communication (IPC) in distributed systems is crucial for enabling processes across different nodes to exchange data and coordinate activities. This article explores various IPC methods, their benefits, and challenges in modern distributed computing environments.Interprocess Communicati
    7 min read
    Client Server Communication in Operating System
    In an Operating System, Client Server Communication refers to the exchange of data and Services among multiple machines or processes. In Client client-server communication System one process or machine acts as a client requesting a service or data, and Another machine or process acts like a server f
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences