INTER PROCESS COMMUNICATION

• Signal is a concept through which one process gives a signal to another process when some exceptional condition is occurred. But with the help of signal one process cannot send any data to another process. • Inter Process Communication is a mechanism by which one process can send data to another process. In other words, a process can communicate with another process, by exchanging data and information. • There are several concepts available for implementing Inter process Communication between two processes. But in this chapter we only discussed two of them. Those are given below: 1. Pipes. 2. Socket. • Pipes: o Pipe is a mechanism of Linux Kernel which gives feelings of a File in Main Memory. Inside a Pipe, Linux Kernel uses a Buffer to store data and which gives an illusion of storing data in a File. o The concept of Pipe can be illustrated in the following way.

So, actually Linux Kernel creates a illusion of a File in Main Memory by using this Buffer. Like a File Name describes a File in Storage. Similarly, File Descriptors are some integer numbers which act like the File Name of the Pipe (which creates an illusion of a file). o Every Pipe has exactly two File descriptors. The Input File Descriptor is used to create an Input Stream and stores the data into the PIPE.

Similarly, the Output File Descriptor can be related to an output Stream to read data from the PIPE. o PIPE is a unidirectional data channel. That means, data can be entered into the file in one direction and can be going out from the Pipe in another direction. o A PIPE can be used to implement Inter Process Communication between a Parent and Child Process. The below figure illustrate this idea more precisely.

This idea describes that, if a Parent process want to send some data to child process then it can do that by using a PIPE. That means, give the Input Stream of a PIPE to Parent process and assign the Output Stream of the Pipe to child process. Now parent need to write all data into the Pipe which it want to send to its child, then after that the child process reads the data from the Pipe that was send by the parent process. And this how Inter process Communication between the Parent and Child process can be possible using Pipe concept. o Linux provides various System Calls to create and manage a Pipe. So, the system call for creating a Pipe is given below:

This function creates a Pipe and store the two File Descriptor of the Pipe in the array “pipefd[2]”. The read end File Descriptor of the Pipe is stored in “pipefd[0]” cell and the write end File Descriptor of the Pipe is stored in “pipefd[1]” location. This function returns 0 on success and -1 on failure.

o The below function used to allocate a Stream to the File Descriptor of the Pipe. This is like allocating the Input and Output Stream to a File.

This function associates a Stream to the File Descriptor which is given as the first argument to this function. If input file descriptor is given then input stream is associated & if output file descriptor is given then output stream is associated. The second argument to this function is the “*mode”. Mode can be “r” or “w”. This function returns the address of the allocated stream on success and returns NULL on failure. o The below function is used to close a File Descriptor.

This function disconnects any stream that was related to the stream “fd”. Remember that; always close the File Descriptor of write end of the file when a process read into a Pipe. Similarly, close the Read file descriptor when a process writes into the Pipe. o The below program illustrate an Inter Process Communication between the Parent & Child Process.

o Socket is a Bidirectional Data Channel which is mainly used to communicate between the processes which are run on different independent machines. 3. So. If we want to send some data from one computer to another computer then we need to connect these sender and receiver computer through set of wires. Various computer experts and scientists proposed various architectures for creating a Computer Network.• Socket: o Socket is another mechanism by which a Process can communicate with another process. this connection of computers with each other through wires is called Computer Network. We can also connect more than two computers in a single network. 5. 4. a single process can communicate with more than one process. These architectures are called Network Topology. which may run on same machine or in another machine. a Process can send data to another computer by using the concept of socket. o To implement a Socket Concept. Here. . Using socket. o So. first we need to connect more than one computer by using wires. So. which are executed on different independent machines. o Networks of Computers: 1. we describe few network topologies and its advantage and limitations. 2. we need to change and add some hardware with our system. A network topology describes how a computer is connected with another computer in a network.

• For example. Mesh Topology: • This topology describes that. if a network contains 15 computers then each computer needs to connect with all other 14 computers. every sender receiver pair has its own communication wire/line. . We can find any defect wire by checking every pair of computer. This topology needs bulk of wires. • Advantage: 1. So other computers cannot view the message of a sender-receiver pair. each computer should contain 14 wires. • Disadvantage: 1. Fault isolation or detection is easy in this topology. 3. That means a fault of a single wire/link or computer does not stop the whole network. This is because. 2. This topology is robust. in a network each computer needs to connect with all other computers in the network. That means.6. This topology is very secure.

3. in this topology each computer needs a single wire and connector. All the computers in the network are connected to this HUB. 7. then the sender computer first sends data to the HUB and after that the HUB sends the data to receiver computer. all other computers remain active. So it is a very expensive topology. Star Topology: • This topology has a central controller. If a computer wants to send data to another computer. • Advantage: 1. • HUB is a device which manages the whole network. which is called HUB. • The below example illustrate a computer network which implements a Start Topology. because every computer has only one cable. 2.2. then a single computer is affected. 3. Star topology is less expensive than Mesh topology. Fault isolation is very easy. • Disadvantage: . If a wire is failed. This topology needs bulk of wires and connectors. Because. Reconnection of wires is very difficult.

. 8. a Long Cable is act like a backbone of the network. If HUB is go out of order. Then how a sender computer would know. Less expensive than Star and Mesh. • Advantage: 1. A fault on the backbone cable stops the whole network. Mesh needs bulk of wires and Star need a HUB device. a sender computer can sends data to the receiver computer by mentioning the logical name of the receiver computer. • Disadvantage: 1. So to eliminate this problem. Difficult to add new computer to the network. various protocols (set of rules) are used to give a logical name/address to each computer in the network. Because. Bus Topology: • In a Bus Topology. A network contains more than one computer. All the computers are dependent on the HUB. which is the receiver computer. All the computers of the network are connected to this long cable by individual wire.1. 2. 2. By giving logical name to each computer. o To transmit data from one computer to another computer. creating a Computer network is not sufficient. Because it need to exchange the whole Backbone Cable. We need to give a logical name to every computer in the network. then the whole network stop working. This topology is easy to create.

. So.o So. there are various rules/protocols available that can be used to assign a logical name to each computer in a network. Host ID  Network ID/Address is a logical address which is given to a whole single network. etc o The most usable protocol to assign a logical name to a computer is the Internet Protocol (IP). where it is divided into 4 octets separated by dots. So. IP Address. This logical address is called. each octet contains 8bits. 1. Internet Protocol 2. we only discussed IP. That means.  An IP Address is a 32bit address. then the sender computer should need to identify the network to which the receiver computer is belongs to. if a computer of one network want to transmit data to another computer of other network. So. Ipsec 4. Network ID is useful in case of network of networks. the range of a possible IP Address is given below:  A IP Address has two parts. Internet Protocol follows to assign a logical address to each computer on a network.  So. some of available protocols are.  So. o Internet Protocol:  This protocol describes that. 1. Every computer network has different network id. IPX. let more than one computer networks are interconnected with each other. each network has a logical address and each computer on a network also have a logical address. Apple Talk 3. Where. in this chapter also. Network ID 2.

which implements Class A IP Address. So. from 4 octets of IP Address. in a network of networks.  The whole range IP Addresses is divided into 5 classes based upon the choice of Network ID and Host ID. this Network ID is given to each network which is helpful to identify a receiver network. . Remember that. Class D & Class E  Class A IP Address:  The range of a Class A IP Address is given below:  From the 4 octets of a Class A IP Address. these 5 classes are. Class A 2. this is decided on the basis of these 5 classes.  Host ID is a logical address which is given to each computer in a Network.So. Class C 4. That means. 1. the first octet is treated as Network ID and the rest 3 octets are treated as Host ID. A network of networks cannot contain two networks with same Network ID. which octet is Host ID and which octet is Network ID.  So the below figure illustrate an Internetwork (network of networks). a network cannot contain two computers with same Host ID/Address. Class B 3.

Class B IP Address:  The range of a Class B IP Address is given below: .2.

from the four octets of IP Address. Class C IP Address:  The range of Class C IP Address is varies from. 5. 0 Network Mask  A Network mask of a Class of IP Address is created by writing the value of the Network ID octets as 255 and the value of Host ID octets as 0. from the four octets of IP Address. 3. 255 . first 2 octets are treated as Network ID and the rest two octets are treated as Host ID. first 3 octets are treated as Network ID and the rest one octet is treated as Host ID. 0 255 . Network Mask:  Network mask defines the class of the IP Address. It cannot be used by other persons. . In Class B. 0 .  In Class C. 0 255 . 0 . 255 . 0 . 4. Class D & E IP Address:  Cass D & E IP Address are used in case of Research Works.  The network mask of all 3 classes of IP Address is given below: Class A IP Address Class B IP Address Class C IP Address Class 255 . 255 .

The “lo” is a default connector in every computer. In the eth0 part data. “Mask 255.255.133” represents the IP Address of the computer. lo) in which network cables can be connected.168.0” represents the Network Mask of the IP Address.6. Every computer has the unique hardware address and it cannot be change.  We can also change the IP Address of a computer or network connector by using “ifconfig” command.255.117. This computer illustrate that it has one network connector. Similarly.  For example: The first column illustrate the name of the connectors (eth0. The “inet addr 192. And the lo part data is viewed same in every computer. that is eth0. The “ifconfig” Command:  The “ifconfig” command shows the IP Address and its Network Mask of your system. “HWaaddr 00:0c:29:d2:ad:9b” represents the physical hardware address of the computer. The format of this command is given below: .

the concept of Process ID cannot work for finding the receiver process. When a process of one computer wants to communicate data with the process of another computer. how a sender process would know which process is the receiver process from all the processes running on the receiver computer? 2. o Ports: 1. How the sender process would find receiver process in the receiver computer? Every computer runs more than one process at a time.  If this command gives the output as “unreachable” then the receiver computer is not active and data communication is not possible. because the PID of a process changes each time when a process is restarted. so as the receiver computer also. . So.  Syntax of ping command is. The “ping” command:  The Ping command is used to check whether a computer in a network is active for data communication or not. Here. a question arises that. For example: 7.

in a same network. two softwares with same PORT number cannot execute in a same machine.iana. then he needs to register the port number of that software in www. o At this point. else they cannot understand the data of other computer. Linux System provides a Logical Permanent Address for a process. and in a same internetwork. Once assigned. for uniqueness. a Port Number for software is same for each time when software is started.       Let. if both have same nature. A Port Number is assigned to software by the programmer. because the PID of receiver process is now changed. restarted or executed. so we can change the nature (execution flow) of a computer by following a protocol. A Port number can be varies from. But this is not sufficient. This address is called Port Number. to overcome this type of problem. The “/etc/services/” file contains the port numbers which are used in our system. let during 1st and 2nd data transmission the receiver process is restarted due to some reason. Remember that. . But when the sender process wants again to send data to receiver process. The port numbers 0 to 1023 is reserved for the process of Operating System. now the sender process cannot communicate with the receiver by referring the PID. So. For that reason. 0 to 65535. and assigning IP Addresses to each computer on the network.og. We can only transmit data from one computer to another. during the creation of software. Over internet two softwares cannot run with same port number. the sender process successfully sends data to the receiver for first time by accessing the PID of receiver process. o Computer is a machine. we succeed in creating a computer network. we need to register our port number. If a programmer wants to develop software which can access all over the world over the internet.

immediately close the connection and after that get busy in other works. a process can only transmit a short amount of data. all the sender data are converted to a package. at once. destination process port number. and other information. it needs to reconnect with its sender. the sender and the receiver computer have the same nature. and connection is terminated. Only a single Datagram is transmitted. no buffer or stream is used to transmit data. It is seems that. That’s why it is called Connection less protocol.  Datagram is a package that contains the source process port number. there is like no connection is established. The most usable protocols are. 2.  If a sender process uses this protocol.  This protocol is used. o So. and then the simplest idea to make it is to change the nature of sender and receiver computer by following a same protocol. Transmission Control Protocol. then it sends a datagram to the network. Using this protocol. the data send by the sender. there is no guarantee that the datagram is successfully reached to the receiver. Even if the sender does not wait for the acknowledgement of the receiver. and transmitted to receiver process.  According to this protocol. which can be used to transmit data from one computer to another. if a process does not want to transmit continuous streams of data. o User Datagram Protocol:  This protocol defines that. . 1. called DATAGRAM. there are various protocols available in market.  According to this protocol.o If we want that. connection is terminated before the datagram is reached to the receiver. This protocol makes a connection for negligible time period and disconnected the connection before the data reached to the receiver. So. User Datagram Protocol.  Every time a process wants to send a message or data.

o Transmission Control Protocol:  This protocol transmits data from sender to receiver in continuous manner. That’s why it is called Connection Oriented Protocol. The first argument to this function defines the protocol used to give logical name to the network and to the computers in a network. But it also used now a days. o Implementing the Socket Concept:  The socket() function creates a socket (a data channel like PIPE) and returns the File Descriptor of that socket. o Now we are able to create and implement a complete computer network. The buffer and stream is de-allocated or the connection to the receiver is disconnected.  This protocol allows continues data transmission until the connection is disconnected.  This is the most usable protocol. This protocol has many limitations. it first creates a buffer and allocates the input and output stream. And then the data is transmitted to the receiver through buffer. Now we need to know the functions which can be used to transmit data from a computer to more than one computer. until all data is transmitted successfully. The possible value of the “namingProtocol” can be. because it is very simple protocol.  That means. . The prototype of socket() function is given below: This function creates a data channel Socket. only after successful transmission of all data. now days. This protocol is only used where the sender process sends a short massage or data to the receiver. if a process follows this protocol. and returns its File Descriptor.

The second argument is a pointer of struct sockaddr. etc. The third argument is the length of the struct sockaddr.  To transfer data using socket. The third argument to this function describes the connection protocol. but we need to assign the Port number and IP address of both sender and receiver to the socket. Whether message in transferred in form of datagram. .The second argument to this function is described how a message is transferred from sender to receiver. The file descriptor of a socket is returned by the socket() function. This function takes the File Descriptor of the Socket.  Socket is created. the value of “commProtocol” can be. both sender and receiver process need to create a socket using socket() function. So. or inform of continuous stream. or in form of continuous packets. This function returns 0 on success and -1 on failure. the bind() function is used to bind the socket with Port number and IP Address. which contains the IP Address and PORT number of sender/receiver. whether it is UDP or TCP. So. So the value of “messageType” can be. The sender needs to write the data into socket and the receiver reads the data from the socket. as its 1st argument.

This function returns 0 on success and -1 on failure. we need to transfer data using either UDP or TCP.Remember that. which sends a single Datagram from one process to another process. the declaration of struct sockaddr_in.  Inter Process Communication Using UDP:  There is a system call available in Linux. . there is no type like struct sockaddr.  So.  The prototype of this function. this is just a Generics type (abstract type). the function close() used to close a socket and connection. is given below: Where the arguments are. Then we need to close the connection. is given below:  After that. So. We can use this function to send short messages from sender process to receiver process. This function takes the File Descriptor of the socket and closes the socket. So we need to typecast the variables of struct sockaddr_in to the struct sockaddr. But there is a another structure that is struct sockaddr_in.

there is a system call recvfrom(). The prototype of this function is given below: Where the arguments of this function are described below:  The below application illustrate the Inter Process Communication. Similarly. . where the sender process sends “Hello World!!” message to receiver process. which is used by the receiver process to receive the message send by the sender process.

.

.

The second arguments define the number of times the socket is re-activated after receiving a message/data. Prototype of this function is given below: This function takes the File Descriptor of the socket as 1st argument. The prototype of this function is given below: .  The listen() function re-activate the function each time after a message is received. In other words it defines how many data/message is received.  The connect() function is used by a sender to give a request for connection to sender. Inter Process Communication Using TCP:  The below figure illustrate what functions the sender and receiver need to call to implement Inter Process Communication using TCP. This function returns 0 on success and returns -1 on failure.

The prototype of this function is given below: .This function creates a connection with the receiver process. It takes the File Descriptor of the socket as 1st argument and the PORT number and IP Address of destination as 2nd argument. The prototype of this function is given below: This function takes the File Descriptor of the socket as 1st argument and the Port number and IP Address of sender as second argument.  The send() function is used by the sender process to send data to receiver through socket. and return -1 on failure. This function return 0 on success. This function returns File Descriptor of sender process on success and returns -1 on failure.  The accept() function is used by the receiver process to accept a connection request by the sender process.

 The recv() function is used by the receiver process to receive a single message from the sender on each call. . The prototype of this function is given below:  The below application illustrate the Inter Process Communication using TCP.

.

.

.

o The below figure illustrate how a Client process sends a request & how a server process provide services. both processes need to follow the same transmission protocol. where the client process request for a service to the server process and the server process provides the services to client process. • To establish communication between Client and Server. • The computer which executes a client process. by using Linux System calls.Client And Server: • Client and Server are two independent processes executed on two different machines. . • Client-Server Using TCP & IPV4: o In this section we going to learn how to create a client and a server which is transmit data by using TCP/IP protocols. either UDP or TCP. called Client Computer and the computer which executes server process is called Server Computer.

.o The application shows simple client-server programs.

.

.

 File Transfer Server. . Servers can be of various types.  Hyper Text Transfer Server.  Database client is a client process which only demands for records that are stored in the server database. o Database Server & Client:  Database server is one type of server which provides the service of accessing a database.  This type of server accesses the requested recorders from the database and gives to its clients.  Database Server.  Mail Server. etc.o Depending on the various types of service provides.

 In the below example. the Client request for a student records by entering the Roll Number of the student. .

.

.

.

o Hypertext Server(Http Server) & Client (Http Client):  A Hypertext Client request for a web page (HTML Page) to the server & the Hypertext Server sends the HTML code of the web page as response to the client. picture. etc. . For that reason. Where. text. multimedia. audio.  These Client & Server process implements Inter Process Communication using a protocol. Hyper Text is a language which can carry video. these Client and Servers are also called HTTP Client & HTTP Server. named HTTP (Hyper Text Transfer Protocol).

 The below figure illustrate the above concept: . And the example of commonly used Hypertext Client (web browsers) is Google Chrome. etc. etc. Weblogic. where each web page has a unique Domain name. web pages are special types of programs. a request for that web page is go to the Server. shows/mapped the html code.  A Hypertext Server contains N number of web-pages. Generally. which can only. ASP. When a user enters the domain name of a web site (or service) in a web browser. Morzila Fireforx. Web browser/ Hypertext Client are an output screen of the web pages. etc. IE. Example of commonly used Hypertext Servers is Apache. After executing the web page the server sends the output code (html codes) of the webpage to the requested client. After that the server selects the particular web page and executes it. PHP. created in languages like JSP. whose output code is called HTML code. Then the requested client shows that output code (html code) to the user.

 To create a web server or HTTP server we need to register the PORT number and IP Address.  Remember that. which is not possible now. from a Http Client program. So here we only show how to create a web client or HTTP client. the request for a web site is given in following format:  The below program illustrate a simple Http Client program: .

Sign up to vote on this title
UsefulNot useful