Monday, March 17, 2008

Inter-process communication

Inter-process communication

Inter-Process Communication (IPC) is a set of techniques for the exchange of data among two or more threads in one or more processes. Processes may be running on one or more computers connected by a network. IPC techniques are divided into methods for message passing, synchronization, shared memory, and remote procedure calls (RPC). The method of IPC used may vary based on the bandwidth and latency of communication between the threads, and the type of data being communicated.
IPC may also be referred to as inter-thread communication and inter-application communication.
IPC, on pair with the address space concept, is the foundation for address space independence/isolation

Inter-process communication (IPC) is a set of interfaces, which is usually programmed in other for a programmer to communicate between a series of processes. This allows the running of programs concurrently in an operating system.
There are quite a number of methods used in inter-process communications. They are:

q Pipes: This allows the flow of data in one direction only. Data from the output is usually buffered until the input process receives it which must have a common origin. In computer programming, especially in Unix operating systems, a pipe is a technique for passing information from one program process to another. Unlike other forms of interprocess communication (IPC), a pipe is one-way communication only. Basically, a pipe passes a parameter such as the output of one process to another process which accepts it as input. The system temporarily holds the piped information until it is read by the receiving process.
Using a UNIX shell (the UNIX interactive command interface), a pipe is specified in a command line as a simple vertical bar () between two command sequences. The output or result of the first command sequence is used as the input to the second command sequence. The pipe system call is used in a similar way within a program.
For two-way communication between processes, two pipes can be set up, one for each direction. A limitation of pipes for interprocess communication is that the processes using pipes must have a common parent process (that is, share a common open or initiation process and exist as the result of a fork system call from a parent process).


q Named Pipes: This is a pipe with a specific name. It can be used in processes that do not have a shared common process origin. Example is FIFO where the data is written to a pipe is first named. In computer programming, a named pipe is a method for passing information from one computer process to other processes using a pipe or message holding place that is given a specific name. Unlike a regular pipe, a named pipe can be used by processes that do not have to share a common process origin and the message sent to the named pipe can be read by any authorized process that knows the name of the named pipe.
A named pipe is sometimes called a "FIFO" (first in, first out) because the first data written to the pipe is the first data that is read from it.

q Message queuing: This allows messages to be passed between messages using either a single queue or several message queues. This is managed by the system kernel. These messages are co-ordinated using an application program interface (API) in programming, message queueing is a method by which process (or program instances) can exchange or pass data using an interface to a system-managed queue of messages. Messages can vary in length and be assigned different types or usages. A message queue can be created by one process and used by multiple processes that read and/or write messages to the queue. For example, a server process can read and write messages from and to a message queue created for client processes. The message type can be used to associate a message with a particular client process even though all messages are on the same queue.
The message queue is managed by the operating system (or kernel). Application programs (or their processes) create message queues and send and receive messages using an application program interface (API). In Unix systems, the C programming language msgget function is used with various parameters specifying the action requested, message queue ID, message type, and so forth.
The maximum size of a message in a queue is limited by the operating system and is typically 8,192 bytes.
q Semaphores: This is used in solving problems associated with synchronization and avoiding race conditions. They are integers values which are greater than or equal to zero In programming, especially in Unix systems, semaphores are a technique for coordinating or synchronizing activities in which multiple processes compete for the same operating system resources. A semaphore is a value in a designated place in operating system (or kernel) storage that each process can check and then change. Depending on the value that is found, the process can use the resource or will find that it is already in use and must wait for some period before trying again. Semaphores can be binary (0 or 1) or can have additional values. Typically, a process using semaphores checks the value and then, if it using the resource, changes the value to reflect this so that subsequent semaphore users will know to wait.
Semaphores are commonly use for two purposes: to share a common memory space and to share access to files. Semaphores are one of the techniques for interprocess communication (IPC). The C programming language provides a set of interfaces or "functions" for managing semaphores.
q Shared Memory: This allows the interchange of data through a defined area of memory. Semaphore value has to be obtained before data can get access to shared memory. In computer programming, shared memory is a method by which program processes can exchange data more quickly than by reading and writing using the regular operating system services. For example, a client process may have data to pass to a server process that the server process is to modify and return to the client. Ordinarily, this would require the client writing to an output file (using the buffers of the operating system) and the server then reading that file as input from the buffers to its own work space. Using a designated area of shared memory, the data can be made directly accessible to both processes without having to use the system services. To put the data in shared memory, the client gets access to shared memory after checking a semaphore value, writes the data, and then resets the semaphore to signal to the server (which periodically checks shared memory for possible input) that data is waiting. In turn, the server process writes data back to the shared memory area, using the semaphore to indicate that data is ready to be read.

q Sockets: This method is mostly used to communicate over a network, between a client and a server. It allows for a standard connection which I computer and operating system independent. Sockets is a method for communication between a client program and a server program in a network. A socket is defined as "the endpoint in a connection." Sockets are created and used with a set of programming requests or "function calls" sometimes called the sockets application programming interface (API). The most common sockets API is the Berkeley Unix C interface for sockets. Sockets can also be used for communication between processes within the same computer.
This is the typical sequence of sockets requests from a server application in the "connectionless" context of the Internet in which a server handles many client requests and does not maintain a connection longer than the serving of the immediate request:
socket()bind()recvfrom()(wait for a sendto request from some client)(process the sendto request)sendto (in reply to the request from the client...for example, send an HTML file)
A corresponding client sequence of sockets requests would be:
socket()bind()sendto()recvfrom()
Sockets can also be used for "connection-oriented" transactions with a somewhat different sequence of C language system calls or functions.

Mutual exclusion processes has a shortcoming which is the fact that it wastes the processor time.
There are primitive interprocesses that block instead of wasting the processor time.
Some of these are:

Sleep and Wakeup

SLEEP is a system call that causes the caller to block, that is, be suspended until another process wakes it up. The WAKEUP call has one parameter, the process to be awakened.

The Producer-Consumer Problem

In this case, two processes share a common, fixed-size buffer. One of the processes puts information into the buffer, and the other one, the consumer, takes it out. This could be easy with 3 or more processes in which one wakeup waiting bit is insufficient, another patch could be made, and a second wakeup waiting bit is added of 8 or 32 but the problem of race condition will still be there.

Events Counter

This involves programming a program without requiring mutual exclusion. Event counters value can only increase and never decrease. There are three operations defined on an event counter for example, E:
1. Read (E): Return value of E

2. Advance (E): Atomically increment E by 1.

3. Await (E, v): Wait until E has a value of v or more.

Two events counters are used. The first one, in would be to count the cumulative number of items that the producer discussed above has put into the buffer since the program started running. The other one out, counts the cumulative number of items that the consumer has removed from the buffer so far. Therefore, it is clear that in must be greater than or equal to out, but not more that the size of the buffer. This is method that works with pipes discussed above.

Monitors
This is about the best way of achieving mutual exclusion.
A Monitor is a collection of procedures, variables, and data structures that are grouped together in a special kind of module or package. The monitor uses the wait and signal. The "WAIT" is to indicate to the other process that the buffer is full and so causes the calling process to block and allows the process that was earlier prohibited to enter the process at this point. "SIGNAL" will allow the other process to be awakened by the process that entered during the "WAIT".

No comments: