Inter Process Communication


Advantages of process cooperation

  • Information sharing
  • Computation speed-up
  • Modularity
  • Convenience

Disadvantage of process cooperation

  • Data corruption, deadlocks, increased complexity
  • Requires processes to synchronize their processing

Semaphores & Mutex

Event Flags

http://www.ecoscentric.com/ecospro/doc.cgi/html/ref/kernel-flags.html

Shared Memory

If threads are in different process, we need to request a segment of shared memory from kernel, and processes communicate through this shared memory by reading and writing data on it.
If threads are in the same process, there are several types of shared memory:
1) Shared global variable: global variables within source code of multiple threads.
2) Shared private data: private variables whose address are given to other threads.
3) Static variable: all static variables within a shared function will become shared data. All functions which are called in a shared function are also shared functions.
With shared memory, synchronization should be achieved by processes/threads themselves to avoid data corruption.

Message Passing

In message passing, there is no shared memory between processes. Using kernel function, kernel will copy data from the source process to kernel space, and then copy the data from kernel space to destination process. There are basically two operations, send(pid, message) and receive(pid, message). Synchronization and data protection are handled by kernel.

Blocking & Non-Blocking
  • Blocking Send — sender blocked until message received by mailbox or process
  • Nonblocking Send — sender resumes operation immediately after sending
  • Blocking Receive — receiver blocks until a message is available
  • Nonblocking Receive — receiver returns immediately with either a valid or null message.
Buffering

All messaging system require framework to temporarily buffer messages. These queues are implemented in one of three ways:

  • Zero Capacity — No messages may be queued within the link, requires sender to block until receives retrieves message.
  • Bounded Capacity — Link has finite number of message buffers. If no buffers are available(buffer is full) then sender must block until one is freed up.
  • Unbounded Capacity — Link has unlimited buffer space, consequently send never needs to block. It can be approximated by linked list, while it still has a boundary (when heap section is full).

Pipe

Pipe can link one process's input to another process's output, it is one-way communication. One process only read, and another process only write. Like message passing, It can be blocking or non-blocking.

using C

#include <sys/wait.h>
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
int
main(int argc, char *argv[])
{
    int pfd[2];
    pid_t cpid;
    char buf;
    assert(argc == 2);
    if (pipe(pfd) == -1) { perror("pipe"); exit(EXIT_FAILURE); }     // create pipe
       // it generate a pair of file descriptors, one for read "pfd[0]", one for write "pfd[1]" 
    cpid = fork();                         // fork a child process
    if (cpid == -1) { perror("fork"); exit(EXIT_FAILURE); }
    if (cpid == 0) {                   
        close(pfd[1]);                // child process close unused write end
        while (read(pfd[0], &buf, 1) > 0)         // child reads from pipe to buf
            write(STDOUT_FILENO, &buf, 1);
        write(STDOUT_FILENO, "\n", 1);           // child outputs what it reads onto terminal
        close(pfd[0]);               // close pipe
        _exit(EXIT_SUCCESS);
    } else {                
        close(pfd[0]);          // parent close unused read end 
        write(pfd[1], argv[1], strlen(argv[1]));       // parent writes argv[1] to pipe
        close(pfd[1]);          // reader will see EOF
        wait(NULL);             // wait for child
        exit(EXIT_SUCCESS);
    }
}

Output:

./test abcd
abcd

Explanation:
First, call pipe() to generate a pair of file descriptor. After, one process write to the write end file descriptor, and another process read from the read end file descriptor.

using Shell

  • Implement by linux shell language — otherprog | prog —> linke otherprog's standard output to prog's standard input

Signal

A signal is a limited form of inter-process communication used in Unix, Unix-like, and other POSIX-compliant operating systems. Essentially it is an asynchronous notification sent to a process in order to notify it of an event that occurred. When a signal is sent to a process, the operating system interrupts the process's normal flow of execution. Execution can be interrupted during any non-atomic instruction. If the process has previously registered a signal handler, that routine is executed. Otherwise the default signal handler is executed.
Signal handling is vulnerable to race conditions. Because signals can be asynchronously triggered during the execution of another signal handling routine.

Reference

www.arl.wustl.edu/~fredk/Courses/cse422/sp04/Lectures/ipc.ppt
http://en.wikipedia.org/wiki/Signal_(computing)

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License