Basic Guide of Interprocess Communication and Pipes
Interprocess Communication (IPC) is a method of communication by exchanging data / information among multiple threads or multiple process. The usefulness of creating a child process from parent process is limited if they don’t have ability to communicate between the process. Interprocess communication (IPC) enables the exchange of information / data that are running on same platform or same CPU. The oldest form and most commonly used of IPC is called a pipe.
IPC Basic Guide of Interprocess Communication and Pipes
Mainly, communication using pipe consists of two file descriptors (one for reading and one for writing). There are two kinds of pipes :
- Half Duplex, a pipe which can send data/information in one direction.
- Full Duplex, a pipe which can send and receiving data/information at the same time.
You can create pipes by using Pipe function, which returns two files (a read file and a write file) descriptor in an integer array format. In C and C++ Programming Language, Interprocess Communications (IPC) Functions can be obtained from unistd.h
List of Some Interprocess Communication Functions from unistd.h
Interprocess Communication Example in C
In this example we will show you a complete program that creates a pipe to communicating between parent and child process.
Parent Process Example
Parent process will create a new process (child) and communicate using a pipe
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253#include <stdio.h>#include <unistd.h>#include <wait.h>#include <string.h>#define contentMessage "Parent successfully communicate using pipe"int main (void){int communicate[2]; /* Variable of array for the pipe*/pid_t apid; /*forked process id*/int status; /*for identifying status of communication*//*Now lets create Interprocess Communications (IPC) pipe*/if (pipe(communicate) < 0)printf ("Error initiating communication using pipe\n");apid = fork(); /*create a new process*/if (apid < 0){printf ("Error Create New Process\n");if (close (communicate[0]) != 0)printf("Error closing file descriptor \n);if (close (communicate[1]) != 0)printf("Error closeing file secriptor \n);}else if (apid == 0){printf ("Process Created With ID %d\n", getpid());if (execl("child.exe", "child.exe", NULL) < 0)printf ("Error replace image process\n");}else{printf ("Process Created With ID %d\n", getpid());if (close (communicate[0]) != 0)printf ("Error Closing Pipe\n");sleep(1);printf ("Parent Process Writing '%s'\n", contentMessage);if (write (communicate[1], contentMessage, sizeof(contentMessage)) < 0)printf ("Error Writing message\n");if (write (communicate[1], "\n", 1) < 0);printf ("Error Writing message\n");printf ("Waiting....\n");if (wait(&status) == apid)printf ("Status = WEXITSTATUS(status));elseprintf ("Error Waiting Process\n");}printf ("Parent Process Stopping\n");return (0);}
Child Process Example
Child Process will be a program that launched from parent
12345678910111213141516171819202122#include <stdio.h>#include <string.h>#include <unistd.h>int main (void){char text[64]; /* Message buffer *//* Display the child process ID */printf (" Child Process ID %d\n", getpid());/* Read a newline delimited string from the standard input */if (fgets (text, sizeof (text) - 1, stdin) == NULL)printf ("Error Reading Standard Input\n");/* Display the message read from standard input */printf (" Child Process Reading '%s'\n", text);printf (" Child Process Stopping\n");/* Return 1 to parent to indicate successful completion */return (1);}
Now it’s done. Try to compile them. If it can’t be compiled maybe you can check for common errors.
You May Want to See :
- Parallel Programming with Multiprocess and Threads Parallel Programming with Multiprocess and Threads
- Handling with Objects and Classes in PHP Handling with Objects and Classes in PHP
- Why is Exchange Mailbox Not Receiving Email from External IDs? Why is Exchange Mailbox Not Receiving Email from External IDs?
- Understanding the basics of HTML Understanding the basics of HTML
- Reading Files Without Filehandle PHP Reading Files Without Filehandle PHP
- How To Create a Simple Search Engine How To Create a Simple Search Engine
- What is CSS and What CSS can do ? What is CSS and What CSS can do ?
- R vs Python, Which one is better R vs Python, Which one is better
- String Functions PHP String Functions PHP
- 3 Common Programming Errors 3 Common Programming Errors
- User Defined Functions in MySQL User Defined Functions in MySQL
- How To Use AirSnort to crack WEP keys How To Use AirSnort to crack WEP keys
- Connecting Ruby to Java Programming Connecting Ruby to Java Programming
- Recursive Function in Program
- How to Test and Debug A Program How to Test and Debug A Program
- Put a Tile to Computer on the Start Screen Put a Tile to Computer on the Start Screen
- How To Use Cloud-Based Storage Wisely How To Use Cloud-Based Storage Wisely
- Improve Security With VERIS Framework Improve Security With VERIS Framework
- How To Create a Web Crawler and Data Miner How To Create a Web Crawler and Data Miner
- Apache Rewrite Rules Guide Apache Rewrite Rules Guide
- Extend Your Wordpress Theme With WP Child Themes Extend Your Wordpress Theme With WP Child Themes
One thought on “Basic Guide of Interprocess Communication and Pipes”
magnificent submit, very informative. I’m wondering why the other specialists of this sector do not understand this.
You should continue your writing. I’m sure, you’ve a huge readers’ base already!
This site uses Akismet to reduce spam. Learn how your comment data is processed.