Interprocess Communication ( IPC )
-
Interprocess communication allows a process to communicate with another process.
-
Communications can be one of two types
- Between related process ( parent and child )
- Between unrelated processes ( one or more different processes)
-
IPC can use
- Pipes
- First In First Out ( FIFO) - Queue
- Message Queues
- Shared Memory
- Semaphores
- Signals
Pipes
-
Pipes are half-duplex, they can only read or write a process but only in one direction,
-
x
ls -lR /home | more
# -R : List recursive subdirectory
-
First In, First Out (Named Pipes)
-
FIFO are also called as named pipes, and are half duplex as pipes.
-
xxxxxxxxxx
mkfifo mypipe
# once data send to mypipe, till the other end ready to receive it, echo program will not exit.
echo "Hello Pipe world" > mypipe
# open another terminal window to receive the data
cat mypipe
- when you create a pipe, to identify it, execute
ls -l
command to the pipe, you can see if its a pipe or not
x
bash-3.2$ ls -l namedpipe00*
prw-r--r-- 1 xxxx xxxx 0 Jul 19 11:00 namedpipe001
prw-r--r-- 1 xxxx xxxx 0 Jul 19 11:06 namedpipe002
bash-3.2$
In the above output prw-r--r--
- p denotes that its a pipe.
Shared Memory
- shared memory is full duplex, either process can read and/or write.
- most efficient type of IPC, it does not require any kernel intervention once the shared memory has been allocated/deallocated.
- Required a program
- Any number of processes can read and write to the same shared memory segment.
- You can query shared memory with the
ipcs
command - Processes must manage shared memory
- Process must protect shared memory being wirtten or race conditions will occur( if two process are trying to access same data at same time)
x
[xxxxxx:/root]# ipcs
------ Message Queues --------
key msqid owner perms used-bytes messages
------ Shared Memory Segments --------
key shmid owner perms bytes nattch status
------ Semaphore Arrays --------
key semid owner perms nsems
0x020078cb 3 root 600 1
[xxxxxx:/root]#
Message Queues
- created by syscall
- managed by kernel
- kernel will delete the message from queue once it is read
- each read and write creates a syscall to the kernel
- the message queue helps eliminate the occurrences of race conditions but comes at the expense of performance due to the syscall interrupt(trap)
- data will remain in the queue until it is read and once read its gone.
Semaphores
-
Semaphores are used to protect critical/common regions of memory shared between multiple processes.
-
So other process cant use this shared memory till the process that owned the memory releases it.
-
Semaphores are the atomic structures of operating systems
-
Two Types of semaphores:
- Binary: Only two states 0 and 1, locked/unlocked, or available, unavailable etc
- Counting Semaphores : Allow arbitrary resource counters
-
When a process 'allocates' a semaphore, it blocks the other processes from access until the first process issues a 'release' indicating it has completed its operation.
-
The kernel then makes the semaphore available again for allocation.
Signals
-
A signal is a notification of an event occurrence.
-
A signal is also know as a trap, or software interrupt.
-
if you perform 'kill -l' you can see list of killl siganls implemented in your kernel
- for example in a linux machine
x
[xxxxx.xxxx:/home/users/xxxxxx]$ kill -l
1) SIGHUP 2) SIGINT 3) SIGQUIT 4) SIGILL 5) SIGTRAP
6) SIGABRT 7) SIGBUS 8) SIGFPE 9) SIGKILL 10) SIGUSR1
11) SIGSEGV 12) SIGUSR2 13) SIGPIPE 14) SIGALRM 15) SIGTERM
16) SIGSTKFLT 17) SIGCHLD 18) SIGCONT 19) SIGSTOP 20) SIGTSTP
21) SIGTTIN 22) SIGTTOU 23) SIGURG 24) SIGXCPU 25) SIGXFSZ
26) SIGVTALRM 27) SIGPROF 28) SIGWINCH 29) SIGIO 30) SIGPWR
31) SIGSYS 34) SIGRTMIN 35) SIGRTMIN+1 36) SIGRTMIN+2 37) SIGRTMIN+3
38) SIGRTMIN+4 39) SIGRTMIN+5 40) SIGRTMIN+6 41) SIGRTMIN+7 42) SIGRTMIN+8
43) SIGRTMIN+9 44) SIGRTMIN+10 45) SIGRTMIN+11 46) SIGRTMIN+12 47) SIGRTMIN+13
48) SIGRTMIN+14 49) SIGRTMIN+15 50) SIGRTMAX-14 51) SIGRTMAX-13 52) SIGRTMAX-12
53) SIGRTMAX-11 54) SIGRTMAX-10 55) SIGRTMAX-9 56) SIGRTMAX-8 57) SIGRTMAX-7
58) SIGRTMAX-6 59) SIGRTMAX-5 60) SIGRTMAX-4 61) SIGRTMAX-3 62) SIGRTMAX-2
63) SIGRTMAX-1 64) SIGRTMAX
[xxxx.xxxxx:/home/users/xxxxx.xxxx]$
- And in Mac,
x
bash-3.2$ kill -l
1) SIGHUP 2) SIGINT 3) SIGQUIT 4) SIGILL
5) SIGTRAP 6) SIGABRT 7) SIGEMT 8) SIGFPE
9) SIGKILL 10) SIGBUS 11) SIGSEGV 12) SIGSYS
13) SIGPIPE 14) SIGALRM 15) SIGTERM 16) SIGURG
17) SIGSTOP 18) SIGTSTP 19) SIGCONT 20) SIGCHLD
21) SIGTTIN 22) SIGTTOU 23) SIGIO 24) SIGXCPU
25) SIGXFSZ 26) SIGVTALRM 27) SIGPROF 28) SIGWINCH
29) SIGINFO 30) SIGUSR1 31) SIGUSR2
bash-3.2$
- Signals can be generated by a user, process or kernel
- if its a process, it is supposed to be written to handle them
- Certain signals numeric 9-15 cannot be handled by the process they will immediately cause termination of the process and cannot be blocked, these are called 'process crash-outs' ExL CTRL+C, which cant be blocked by the process and terminates the process.
POSIX standards now govern the use and definition of IPC constructs and added support to modern features like threads etc.
0 comments:
Post a Comment