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.
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
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.
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!