This document discusses various inter-process communication (IPC) types including shared memory, mapped memory, pipes, FIFOs, message queues, sockets, and signals. Shared memory allows processes to directly read and write to the same region of memory, requiring synchronization between processes. Mapped memory permits processes to communicate by mapping the same file into memory. Pipes and FIFOs allow for sequential data transfer between related and unrelated processes. Message queues provide a way for processes to exchange messages via a common queue. Signals are used to asynchronously notify processes of events.
3. SHARED MEMORY
Simplest and fastest form of IPC.
All process shares same piece of memory.
This memory is shared between some different processes
so synchronization needed.
Means, only one process should be allowed to read/write at
a time.(we can do this by using semaphore).
First create shared memory with a specific key using
shmget.
Attach that memory to process’s address space to make it
available for that process using shmat.
ipcs –m. (To see the list of shared memory in our pc.)
4. Shmget & shmat
shm_id = shmget( key , bytes , flag).
Key - unrelated process can access same shared segment by
specifying same key.
Bytes – number of bytes in segment.
Flag – IPC_CREAT | IPC_EXCL | S_IRUSR | S_IWUSR.
char *shm = shmat(shm_id, ptr, flag);
shm_id – shared segment identifier returned by shmget.
ptr – pointer that specifies where in your process’s address space you
want to map shared memory.
flag – SHM_RND, SHM_RDONLY.
shmdt(shm) – detach shared memory from our process address
space.
shmctl( shm_id, flag, NULL).
flag = IPC_STAT , IPC_RMID etc.
5. MAPPED MEMORY
Permit different process to communicate via a shared
file.
Mapped memory forms an association between a file
and a process’s memory.
Linux splits the file into page-sized chunks and then
copies them into virtual memory pages so that they
can be made available in a process’s address space.
mmap call is used.
6. Mmap()
mmap( addr, len, prot, flag, fd, offset).
o Addr – address at which you would like linux to map the
file into your process’s address space(NULL).
o Len – length of map In bytes.
o Prot – PROT_READ | PROT_WRITE | PROT_EXEC.
o Flag – MAP_FIXED, MAP_PRIVATE (store write to new
file), MAP_SHARED(store write immediately to file, do
not buffer it).
o Fd – file descriptor opened to file to be mapped.
o Offset – offset from beginning of file from which to start
the map.
7. PIPE & FIFO
PIPE permits sequential communication from one process to a
related process.
Data written to the “write end” of the pipe is read back from the
“read end.”
Eg. ls | less : output of “ls” will be written to pipe and on other
side it becomes input for “less”.
Int pipe( int fd[2]);
FIFOs : fifo are similar to pipes, except that unrelated processes
can communicate.
Write mkfifo foo (on first terminal).
Write echo hello > foo (first terminal).
Write cat < foo (second terminal).
8. MESSAGE QUEUE
Two (or more) processes can exchange information via
access to a common system message queue.
The sending process places via some (OS) message
passing module a message onto a queue which can be
read by another process.
Each message is given an identification or type so that
processes can select the appropriate message.
Before we start sending/receiving message, we have to
initialize message queue.
Initializing msgq : int msgget( key_t key , int msgflg ).
9. Msgget, msgsnd, msgrcv
msgid = msgget( key_t key, int msgflg)
Key : processes must share common key to gain access to the queue.
Msdflg : IPC_CREAT | IPC_EXCL | 0666;
Int msgsnd( int msgid, void *msgp, size_t msgsz,
int msgflg ). // man msgsend.
Int msgrcv( int msgid , void *msgp , size_t msgsz,
long msgtyp , int msgflg ).
struct mymsg {
long mtype; /* message type */
char mtext[MSGSZ]; /* message text of length MSGSZ */
} *msgp;
10. SIGNAL
Used to signal asynchronous events to one or more
processes.
An application program can specify a function called a
signal handler to be invoked when specific signal is
received.
Signal hander than catches the signal by using :
int (*signal(int sig , void (*func())));
“ Kill –l ” gives list of different signals.
Process can ignores most of signals except SIGSTOP
and SIGKILL.