# Inter-Process Communication - Part 1 LG #104

http://linuxgazette.net/104/ramankutty.html

...making Linux just a little more fun! <-- prev | next -->

Inter-Process Communication - Part 1
By Hiran Ramankutty

Scope
The purpose of this article is to get the readers familiar with the different mechanisms that are available for communicating between two or more processes. This may also serve as a tutorial for the novice programmer. There might be several good tutorials on this subject, but here I will try to communicate my explorations of this subject. This article may not be technically perfect. Please send your suggestions and queries to .

Introduction
Inter-Process Communication, which in short is known as IPC, deals mainly with the techniques and mechanisms that facilitate communication between processes. Now, why do we need special separate mechanisms or techniques for communicating between processes? Why isn't it possible to have information shared between two processes without using such special mechanisms? Let us start from something primitive. Imagine you have two glasses completely filled with water. One glass contains hot water and the other contains cold water. What can you do to make the temperature of water in both the glasses equal? The simplest answer will be to mix the water from both the glasses in a glass with much bigger capacity. Once water is mixed, the temperature becomes equal after some time. If one can remember, this will be framed as a problem with some numerical data in a High-School Physics examination. If we go by principles, then the phenomenon here is conduction. If we go by our topic of IPC, then we can say that since the two glasses were full, we had to use another glass with a larger capacity to mix the contents in order to balance their heat energy. Have you ever wondered about the communication medium used in telephones? What about the blood transporting system in the human body which communicates blood to different parts of the body? What about my fingers which are typing this document? My brain is doing so many things at a time. How is it directing one of my fingers to hit one key and some other finger to hit another key? How is it synchronizing the typing work that is done by both my hands? How is it also directing me to type the letters of a word that are actually coming to my mind? Don't worry. I am not going to give a class in Biology. But it would be good if one can imagine a few more situations where we are using inter-process communication, though not necessarily in the human body or in a computer program.

1 of 11

10/5/2010 7:17 PM

the memory is completely managed by the operating system. d.html So. A pipe is created by invoking the pipe system call. But then.net/104/ramankutty. Basic IPC OK. b. enough of glasses and water. and the glass with the larger capacity is the kernel address space. A process will be allotted some part of the available memory for execution. where the glass with hot water is one process address space. and the other will continuously check for alpha-numeric characters. processes can use the available memory to communicate with each other.Part 1 LG #104 http://linuxgazette. pipes fifos shared memory mapped memory message queues sockets Pipes Pipes were evolved in the most primitive forms of the Unix operating system.h> #include <stdlib. they are half-duplex. What next? There are different IPC mechanisms which come into use based on the different requirements. Primarily. In terms of our water glasses. The IPC mechanisms can be classified into the following categories as given below: a. They provide unidirectional flow of communication between processes within the same system. data flows in only one direction.h> #include <unistd. we need some mechanism or medium for communication. which has access to all the memory available.h> #include <ctype. which creates a pair of file descriptors. the glass with cold water is another. Let us see how the filedes returned by pipe can be of use in this scenario: (Text version: kbdread-pipe. where are we now? We know that some medium or other is required for communication between different processes. when it comes to computer programs. c.txt) /***** KEYBOARD HIT PROGRAM *****/ #include <stdio.Inter-Process Communication . now the question .c. We will create two processes. f.h> #include <pthread.how do different processes with unique address space communicate with each other? The operating system's kernel.h> 2 of 11 10/5/2010 7:17 PM . In other words. so that we pour both hot water and cold water into the glass with larger capacity. filedes[0] is used for reading whereas filedes[1] is used for writing. we can determine the specifics of both pouring the water into the larger glass and how it will be used after beign poured.h> #include <sys/types. In no way will the memory allotted for one process overlap with the memory allotted for another process. one of them will read characters from the keyboard. will act as the communication channel. Similar to our earlier example. Similarly. e. In the file descriptor pair. that is. Let me explain a scenario where we can use the pipe system call: consider a keyboard-reader program which simply exits after any alpha-numeric character is pressed on the keyboard. Imagine what would happen otherwise! So. Then each process will have its own unique user space. These descriptors point to a pipe inode and the file descriptors are returned through the filedes argument.