Unit 3
Unit 3
Unit 3
Contents
• Shared memory mechanism
• Pipes
• Asynchronous communication
• Signals
• POSIX API for IPC and programs using it
Types of processes
• Processes executing concurrently in the operating system may be
either independent processes or cooperating processes.
• A process is cooperating if it can affect or be affected by the other
processes executing in the system. Means, any process that shares
data with other processes is a cooperating process.
• Any process that does not share data with any other process is
independent process.
InterProcess Communication(IPC)
• Cooperating processes obviously share data with each other
• The mechanism used to share the data is interprocess
communication(IPC)
IPC
Shared Message
Memory Passing
Shared Memory
• An area of memory shared among the processes
• The communication is under the control of the user
processes not the operating system.
• Processes can then exchange information by
reading and writing data to the shared region.
• Major issues is to provide mechanism that will
allow the user processes to synchronize their
actions when they access shared memory.
• Faster than Message Passing
Shared Memory Continued..
• Generally, shared memory segment resides in memory of process
creating it.
• Shared memory IPC requires to remove the restriction of not
accessing other process space.
• Processes should also ensure that they are not writing the same
address at same time.
• Example- Producer Consumer Problem
Functions of IPC Using Shared Memory
• Two functions shmget() and shmat() are used for IPC using shared
memory.
• shmget() function is used to create the shared memory segment
• int shmget (key_t key, size_t size, int shmflg)
• shmat() function is used to attach the shared segment with the
process's address space.
• void *shmat(int shmid, const void *shmaddr, int shmflg)
• shmctl():assign ownership to another user
• shmdt(): detach from process address space
Pipes
• Pipes are used for IPC for:
• Communication in single process
• Communication within parent-child processes
• A pipe file is created using the pipe system call
• Only one process can access a pipe at a time.
• Pipe creates two descriptors, first one is connected to read from the pipe and
other one is connected to write into the pipe.
• Descriptor pipedes[0] is for reading and pipedes[1] is for writing
• A system call would return zero on success and -1 in case of failure
Pipes continued..
• For a communication from Process A to Process B the following
should happen:
• Process A should keep its write end open and close the read end of the pipe.
• Process B should keep its read end open and close its write end.
• When a pipe is created, it is given a fixed size in bytes.
Limitations to pipe:
• A pipe operates in one direction only.
• Pipes cannot support broadcast i.e. sending message to multiple
processes at the same time.
• The read end of a pipe reads any way. It does not matter which
process is connected to the write end of the pipe. Therefore, this is
very insecure mode of communication.
• For two way communication, two pipes can be used
Algorithm:
• Create a pipe.
• Send a message to the pipe.
• Retrieve the message from the pipe and write it to the standard
output.
Sample Code
Named pipes
• Pipes were meant for communication between related processes.
• we can achieve unrelated processes communication with the
Named Pipes(ex- client server process)
• single named pipe can be used for two-way communication
• named pipe is also known as FIFO (First-In-First-Out).
• System call to create FIFO:
int mknod(const char *pathname, mode_t mode, dev_t dev)
int mkfifo(const char *pathname, mode_t mode)
• Study link:
https://www.tutorialspoint.com/inter_process_communication/inter_proc
ess_communication_named_pipes.htm
Message Passing
• Message system – processes communicate with each other without
resorting to shared variables
• The message size is either fixed or variable in size.
• A message generally have header and body
• A standard message can have two parts: header and body.
The header part is used for storing message type, destination id,
source id, message length, and control information. The control
information contains information like what to do if runs out of buffer
space, sequence number, priority. Generally, message is sent using
FIFO style.
Message Passing Continued..
• If processes P and Q wish to communicate, they need to:
• Establish a communication link between them
• Exchange messages via send/receive
• Implementation issues:
• How are links established?
• Can a link be associated with more than two processes?
• How many links can there be between every pair of communicating
processes?
• What is the capacity of a link?
• Is the size of a message that the link can accommodate fixed or variable?
• Is a link unidirectional or bi-directional?
Message Passing Continued..
• Implementation of communication link
• Physical:
• Shared memory
• Hardware bus
• Network
• Logical:
• Direct or indirect
• Synchronous or asynchronous
• Automatic or explicit buffering
Asynchronous Communication:
• Message passing can be
Synchronous/Asynchronous(blocking/unblocking)
• The solution to the producer–consumer problem becomes trivial
when we use blocking send() and receive() statements
• There won't be any immediate response or immediate feedback.
• Some examples of asynchronous communication situations include:
• Pre-recorded video messages on platforms like Loom
• Email messages
• Communication platform like Slite
Asynchronous Comm continued..
• Neither asynchronous nor synchronous communication are better
ways to exchange information.
• They're simply different and serve different purposes.
• In asynchronous operations, you can move to another task before
the previous one finishes
• Asynchronous programming is often related to parallelization.
• You should only use asynchronous programming if you’re dealing
with independent tasks.
Signals
• A signal is a notification to a process indicating the occurrence of an
event.
• Signal is also called software interrupt
• It is not predictable to know its occurrence, hence it is also called
an asynchronous event.
• Signal can be specified with a number or a name, usually signal
names start with SIG.
• the actions performed for the signals are as follows −
• Default Action
• Handle the signal by altering the default action
• Ignore the signal
Signal continued..
• Whenever a signal is raised (either programmatically or system
generated signal), a default action is performed
• It is possible to ignore the signal, if required
• Ignoring the signal implies neither performing the default
action nor handling the signal.
• The signals which can’t be either ignored or handled/caught
are SIGSTOP and SIGKILL.
• Study link:
https://www.tutorialspoint.com/inter_process_communication/inter_process_
communication_signals.htm
Sample Code:
#include <signal.h>
typedef void (*sighandler_t) (int);
sighandler_t signal(int signum, sighandler_t handler);
int sigaction(int signum, const struct sigaction *act, struct sigaction
*oldact)
Study link:
https://www.tutorialspoint.com/inter_process_communication/inter_p
rocess_communication_signals.htm
POSIX( Portable Operating System Interface)
API
• Portable Operating System Interface standards specified by
IEEE to define application programming interface (API).
• POSIX covers all the three forms of IPC
• POSIX System calls: https://docs.oracle.com/cd/E19048-
01/chorus5/806-6897/auto1/index.html
POSIX Interprocess Communication
• POSIX IPC objects have read and write, but not execute,
permissions for the owner, the owner's group, and for others.
• There is no way for the owner of a POSIX IPC object to assign
a different owner. POSIX IPC includes the following features:
• Messages allow processes to send formatted data streams to arbitrary
processes.
• Semaphores allow processes to synchronize execution.
• Shared memory allows processes to share parts of their virtual address
space.
• Unlike the System V IPC interfaces, the POSIX IPC interfaces
are all multithread safe.
POSIX Messages
Interface Name Purpose