“A Process is an address space with one or more threads executing within that address space and required system resources for those threads.”
IPC (Inter Process Communicaton) is a set of methods for the exchange of data between multiple threads in one or more processes. Processes may be running on one or more computers connected by a network. There are several methods to do this like:
a) file : A record stored on the disk that can be accessed by name by any process.
b) Signal: A system message sent from one process to another, not usually used to store information but instead give commands.
c) Socket: A data stream sent over a network interface, either to a different process on the same computer or to another computer.
d) Message Queue: An anonymous data stream similar to the pipe, but stores and retrieves information in packets.
e) Pipe: A two-way data stream interfaced through standard input and output and is read character by character.
f) Named Pipe: A pipe implemented through a file on the file system instead of standard input and output.
g) Semaphore: A simple structure that synchronizes threads or processes acting on shared resources.
h) Shared Memory: Multiple processes given access to the same memory, allowing all to change it and read changes made by other processes.
Process table: It is like a data structure describing all of the processes that are currently loaded with their PID, status, command string, and sort of information. Operating system manages processes using their PID’s. To see the list of processes, we use “ps” command.
eg: ps -ax, ps-af, etc
Pipes:
Pipes provide unidirectional flow of communication between processes within the same system. In other words, they are half-duplex, that is, data flows in only one direction. A pipe is created by invoking the pipe system call, which creates a pair of file descriptors. These descriptors point to a pipe inode and the file descriptors are returned through the filedes argument. In the file descriptor pair, filedes[0] is used for reading whereas filedes[1] is used for writing.
One of the major disadvantage of pipes is that the they can not be accesed using their names by any other process other than child and the parent as they do not get listed in the directory tree.
The work around for this porblem is to create a named pipe which is also called as a FIFO, which stands for First in First out, meaning the data that is written into the pipe first will be read out first always.
FIFO: The fifos get listed in the directory tree and any process can access it using its name by providing the approproiate path.
fifo are created using the function mkfifo() which takes as arguments
1. The name of the fifo that has to be created
2. The permissions for the file.
Once the file is created, it needs to be opened using the system call open() and the data can be read and written from the file using read() and write system calls.
One of the examples you can think of using a named pipe is communication between a server and a client. If ther are two fifos one of the server and the other of the client, then the client can send request to the server on the server fifo which the server will read and respond back with the reply on the client’s fifo.
Another advantage of a fifo over the pipes is that fifo are birectoinal, that is the same fifo can be read from as well and written into.
Shared Memory
Shared memory allows one or more processes to communicate via memory that appears in all of their virtual address space.
shared memory mechanism consisting of four steps. in order of:
- Creating the segment and connecting -shmget (shared memory get)
- Using the pointer to get the shared memory address – shmat (shared memory attach),
- Detaching the shared memory area after use – shmdt (shared memory detach) and
- Finally using the address to control accesses, permissions, receive information and destroy the shared memory area – shmctl (shared memory control).