Sunteți pe pagina 1din 41

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.

Socket:
o Socket is another mechanism by which a Process can communicate with another process, which may run on same machine or in another machine. o Socket is a Bidirectional Data Channel which is mainly used to communicate between the processes which are run on different independent machines. o So, a Process can send data to another computer by using the concept of socket. Using socket, a single process can communicate with more than one process, which are executed on different independent machines. o To implement a Socket Concept, we need to change and add some hardware with our system. So, first we need to connect more than one computer by using wires. o Networks of Computers: 1. 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. 2. So, 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. 3. Various computer experts and scientists proposed various architectures for creating a Computer Network. These architectures are called Network Topology. 4. A network topology describes how a computer is connected with another computer in a network. 5. Here, we describe few network topologies and its advantage and limitations.

6. Mesh Topology: This topology describes that, in a network each computer needs to connect with all other computers in the network. For example, if a network contains 15 computers then each computer needs to connect with all other 14 computers. That means, each computer should contain 14 wires.

Advantage: 1. This topology is robust. That means a fault of a single wire/link or computer does not stop the whole network. 2. This topology is very secure. This is because; every sender receiver pair has its own communication wire/line. So other computers cannot view the message of a sender-receiver pair. 3. Fault isolation or detection is easy in this topology. We can find any defect wire by checking every pair of computer. Disadvantage: 1. This topology needs bulk of wires.

2. Reconnection of wires is very difficult. 3. This topology needs bulk of wires and connectors. So it is a very expensive topology. 7. Star Topology: This topology has a central controller, which is called HUB. All the computers in the network are connected to this HUB. HUB is a device which manages the whole network. If a computer wants to send data to another computer, then the sender computer first sends data to the HUB and after that the HUB sends the data to receiver computer. The below example illustrate a computer network which implements a Start Topology.

Advantage: 1. Star topology is less expensive than Mesh topology. Because, in this topology each computer needs a single wire and connector. 2. If a wire is failed, then a single computer is affected, all other computers remain active. 3. Fault isolation is very easy, because every computer has only one cable. Disadvantage:

1. All the computers are dependent on the HUB. If HUB is go out of order, then the whole network stop working. 8. Bus Topology: In a Bus Topology, a Long Cable is act like a backbone of the network. All the computers of the network are connected to this long cable by individual wire.

Advantage: 1. This topology is easy to create. 2. Less expensive than Star and Mesh. Because, Mesh needs bulk of wires and Star need a HUB device. Disadvantage: 1. Difficult to add new computer to the network. Because it need to exchange the whole Backbone Cable. 2. A fault on the backbone cable stops the whole network. o To transmit data from one computer to another computer, creating a Computer network is not sufficient. We need to give a logical name to every computer in the network. A network contains more than one computer. Then how a sender computer would know, which is the receiver computer. So to eliminate this problem, various protocols (set of rules) are used to give a logical name/address to each computer in the network. By giving logical name to each computer, a sender computer can sends data to the receiver computer by mentioning the logical name of the receiver computer.

o So, there are various rules/protocols available that can be used to assign a logical name to each computer in a network. So, some of available protocols are, 1. Internet Protocol 2. Apple Talk 3. Ipsec 4. IPX, etc o The most usable protocol to assign a logical name to a computer is the Internet Protocol (IP). So, in this chapter also, we only discussed IP. o Internet Protocol: This protocol describes that, each network has a logical address and each computer on a network also have a logical address. So, Internet Protocol follows to assign a logical address to each computer on a network. This logical address is called, IP Address. An IP Address is a 32bit address, where it is divided into 4 octets separated by dots. Where, each octet contains 8bits. So, the range of a possible IP Address is given below:

A IP Address has two parts, 1. Network ID 2. Host ID Network ID/Address is a logical address which is given to a whole single network. Every computer network has different network id. Network ID is useful in case of network of networks. That means, let more than one computer networks are interconnected with each other. So, if a computer of one network want to transmit data to another computer of other network, then the sender computer should need to identify the network to which the receiver computer is belongs to.

So, this Network ID is given to each network which is helpful to identify a receiver network, in a network of networks. A network of networks cannot contain two networks with same Network ID. Host ID is a logical address which is given to each computer in a Network. Remember that, a network cannot contain two computers with same Host ID/Address. The whole range IP Addresses is divided into 5 classes based upon the choice of Network ID and Host ID. That means, from 4 octets of IP Address, which octet is Host ID and which octet is Network ID, this is decided on the basis of these 5 classes. So, these 5 classes are, 1. Class A 2. Class B 3. Class C 4. 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, the first octet is treated as Network ID and the rest 3 octets are treated as Host ID.

So the below figure illustrate an Internetwork (network of networks), which implements Class A IP Address.

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

In Class B, from the four octets of IP Address, first 2 octets are treated as Network ID and the rest two octets are treated as Host ID.

3. Class C IP Address: The range of Class C IP Address is varies from,

In Class C, from the four octets of IP Address, first 3 octets are treated as Network ID and the rest one octet is treated as Host ID.

4. Class D & E IP Address: Cass D & E IP Address are used in case of Research Works. It cannot be used by other persons. 5. Network Mask: Network mask defines the class of the IP Address. 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 . 0 . 0 . 0 255 . 255 . 0 . 0 255 . 255 . 255 . 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.

6. The ifconfig Command: The ifconfig command shows the IP Address and its Network Mask of your system. For example:

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

For example:

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

Let, the sender process successfully sends data to the receiver for first time by accessing the PID of receiver process. But when the sender process wants again to send data to receiver process, let during 1st and 2nd data transmission the receiver process is restarted due to some reason, now the sender process cannot communicate with the receiver by referring the PID, because the PID of receiver process is now changed. So, to overcome this type of problem, Linux System provides a Logical Permanent Address for a process. This address is called Port Number. A Port Number is assigned to software by the programmer, during the creation of software. Once assigned, a Port Number for software is same for each time when software is started, restarted or executed. Remember that, two softwares with same PORT number cannot execute in a same machine, in a same network, and in a same internetwork. A Port number can be varies from, 0 to 65535. The port numbers 0 to 1023 is reserved for the process of Operating System. The /etc/services/ file contains the port numbers which are used in our system. If a programmer wants to develop software which can access all over the world over the internet, then he needs to register the port number of that software in www.iana.og. Over internet two softwares cannot run with same port number. For that reason, we need to register our port number, for uniqueness.

o At this point, we succeed in creating a computer network, and assigning IP Addresses to each computer on the network. But this is not sufficient. We can only transmit data from one computer to another, if both have same nature; else they cannot understand the data of other computer. o Computer is a machine, so we can change the nature (execution flow) of a computer by following a protocol.

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

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

This function creates a data channel Socket, and returns its File Descriptor. The first argument to this function defines the protocol used to give logical name to the network and to the computers in a network. The possible value of the namingProtocol can be,

The second argument to this function is described how a message is transferred from sender to receiver. Whether message in transferred in form of datagram, or inform of continuous stream, or in form of continuous packets, etc. So the value of messageType can be,

The third argument to this function describes the connection protocol, whether it is UDP or TCP. So, the value of commProtocol can be,

To transfer data using socket, both sender and receiver process need to create a socket using socket() function. The sender needs to write the data into socket and the receiver reads the data from the socket. Socket is created, but we need to assign the Port number and IP address of both sender and receiver to the socket. So, the bind() function is used to bind the socket with Port number and IP Address.

This function takes the File Descriptor of the Socket, as its 1st argument. The file descriptor of a socket is returned by the socket() function. The second argument is a pointer of struct sockaddr, which contains the IP Address and PORT number of sender/receiver. The third argument is the length of the struct sockaddr. This function returns 0 on success and -1 on failure.

Remember that, there is no type like struct sockaddr, this is just a Generics type (abstract type). But there is a another structure that is struct sockaddr_in. So we need to typecast the variables of struct sockaddr_in to the struct sockaddr. So, the declaration of struct sockaddr_in, is given below:

After that, we need to transfer data using either UDP or TCP. Then we need to close the connection. So, the function close() used to close a socket and connection.

This function takes the File Descriptor of the socket and closes the socket. This function returns 0 on success and -1 on failure. Inter Process Communication Using UDP: There is a system call available in Linux, which sends a single Datagram from one process to another process. We can use this function to send short messages from sender process to receiver process. The prototype of this function, is given below:

Where the arguments are,

Similarly, there is a system call recvfrom(), which is used by the receiver process to receive the message send by the sender process. 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, where the sender process sends Hello World!! message to receiver process.

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.

The listen() function re-activate the function each time after a message is received. Prototype of this function is given below:

This function takes the File Descriptor of the socket as 1st argument. The second arguments define the number of times the socket is re-activated after receiving a message/data. In other words it defines how many data/message is received. This function returns 0 on success and returns -1 on failure. The connect() function is used by a sender to give a request for connection to sender. 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. This function return 0 on success, and return -1 on failure. The accept() function is used by the receiver process to accept a connection request by the sender process. 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. This function returns File Descriptor of sender process on success and returns -1 on failure. The send() function is used by the sender process to send data to receiver through socket. The prototype of this function is given below:

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.

Client And Server:


Client and Server are two independent processes executed on two different machines, where the client process request for a service to the server process and the server process provides the services to client process. The computer which executes a client process, called Client Computer and the computer which executes server process is called Server Computer. To establish communication between Client and Server, both processes need to follow the same transmission protocol, either UDP or TCP. 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. o The below figure illustrate how a Client process sends a request & how a server process provide services, by using Linux System calls.

o The application shows simple client-server programs.

o Depending on the various types of service provides, Servers can be of various types. Database Server. File Transfer Server. Hyper Text Transfer Server. Mail Server, etc. 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 client is a client process which only demands for records that are stored in the server database.

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. These Client & Server process implements Inter Process Communication using a protocol, named HTTP (Hyper Text Transfer Protocol). For that reason, these Client and Servers are also called HTTP Client & HTTP Server. Where, Hyper Text is a language which can carry video, audio, picture, multimedia, text, etc.

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

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

The below program illustrate a simple Http Client program:

S-ar putea să vă placă și