IPC is nothing but ‘Inter process communication’. This IPC
allows to two process to communicate with each other. The communication between
the process is done by kernel by allocating a space which could be accessed by
both process.
There are various ways by which process communicate with each
other,
Pipes: Pipes provide a way for 2 processes to communicate with
each other. A Named pipe is used for a process on one system to send messages
to remote system.syslog-ng logging service uses the pipes very well.
Shared Memory: Process can also
communicate with each other by using a specified memory area. Once process can
create a area in memory which other process access.
Semaphores: Semaphores simply
coordinates the access of a resource. It Makes sure that Only one process can
access the resource at a time.
(Obtained Online)
Semaphores are a programming construct designed by E. W.
Dijkstra in the late 1960s. Dijkstra's model was the operation of railroads:
consider a stretch of railroad in which there is a single track over which only
one train at a time is allowed. Guarding this track is a semaphore. A train
must wait before entering the single track until the semaphore is in a state
that permits travel. When the train enters the track, the semaphore changes
state to prevent other trains from entering the track. A train that is leaving
this section of track must again change the state of the semaphore to allow
another train to enter. In the computer version, a semaphore appears to be a
simple integer. A process (or a thread) waits for permission to proceed by
waiting for the integer to become 0. The signal if it proceeds signals that
this by performing incrementing the integer by 1. When it is finished, the
process changes the semaphore's value by subtracting one from it.
Message Queue: These are structured list of memory segments
where process can store or retrieve data.
IPCS command in linux allows to list the information about the
inter process communication.
localhost:root-~ $ ipcs
------ Shared Memory Segments --------
key shmid owner
perms bytes nattch
status
0x00000000 4227074 root
644 80
2
0x00000000 4259844 root
644 16384
2
0x00000000 4292613 root
644 280 2
------ Semaphore Arrays --------
key semid owner
perms nsems
0x01050001 65538 root
666 7
------ Message Queues --------
key msqid owner
perms used-bytes messages
0x41050020 0 root
666
0
0
0x000004d2 131075 root
666
0
0
The basic ‘ipcs’ command gives the details about the Shared memory,
Semaphore and Message Queue Details.
Every Inter process communication is associated with a key to it
(0x01050001).
When a process runs, it grabs some shared memory. There could be
one or more shared memory segments for every process.
Shared Memory
localhost:root-~ $ ipcs -m
------ Shared Memory Segments --------
key shmid owner
perms bytes nattch status
0x00000000 4227074 root
644 80 2
0x00000000 4259844 root
644 16384
2
0x00000000 4292613 root
644 280
2
In the above output, the each shared memory segment has a key
associated with it and also uniquely identified by a shared memory ID (
4227074) under the shmid column.
Here, you see a mix of private and public shared memory objects.
Private objects have a key of zero, although every object has a unique shmid.
The nattch column tells you how many processes currently are attached to the
shared memory object.
If nattch is zero, and neither process listed by ipcs is
running, it may be safe to delete the object with the icprm command
Process ids that accessed IPC facility
If we need to find out the last accessed or very recently
accessed process ID for this IPC facility we can use,
localhost:root-~ $ ipcs -m -p
------ Shared Memory Creator/Last-op --------
shmid owner cpid
lpid
4227074 root 4965 4974
4259844 root 4965 4974
4292613 root 4965 4974
The Output Shows you the last recently accessed and creator.
The creator’s PID is listed as cpid and the last PID to attach
or detach is listed as lpid. You may think that as long as nattch is 2, these
are the only processes. Don’t forget that there is no guarantee that the object
creator is still attached.
Who has this memory mapped now?
As we know that ipcs command gives you information about the
shared memory segments, it never gives you the details of who has this memory
mapped now.In oder to find this we can use
localhost:root-~ $ ipcs -m
------ Shared Memory Segments --------
key shmid owner
perms bytes nattch
status
0x00000000 4227074 root
644
80
2
0x00000000 4259844 root
644
16384
2
0x00000000 4292613 root
644 280
2
localhost:root-~ $ ipcs -m -p
------ Shared Memory Creator/Last-op --------
shmid owner cpid
lpid
4227074 root 4965 4974
4259844 root 4965 4974
4292613 root 4965 4974
Then
localhost:root-~ $ lsof | head -1 ; lsof | grep 4227074
COMMAND PID USER
FD TYPE DEVICE SIZE/OFF NODE NAME
We grep for the shmid to see which processes are still using
this object. Notice that lsof indicates
the key in the NAME column in hexadecimal.I could not get any
values here.
Message Queues
localhost:root-~ $ ipcs -q
------ Message Queues --------
key msqid owner
perms used-bytes messages
0x41050020 0 root
666 0
0
0x000004d2 131075 root
666 0
0
The above output lists all the Message Queues in the System. The
key is the application defined key and msqid is the system defined one. The 2
important fields are used-bytes and message. If there is any data residing
under these 2 fields for any key then there is a problem, because most applications don’t let messages sit in queues for
very long.
localhost:root-~ $ ipcs -q -i 131075
Message Queue msqid=131075
uid=0 gid=0 cuid=0
cgid=0 mode=0666
cbytes=0
qbytes=65536 qnum=0 lspid=6814 lrpid=6925
send_time=Mon Sep 17 02:26:40 2012
rcv_time=Mon Sep 17 02:26:43 2012
change_time=Mon Sep 3
02:30:25 2012
The lspid is the last sender pid and irpid is the last received
pid.
Semaphores
localhost:root-~ $ ipcs -s
------ Semaphore Arrays --------
key semid
owner perms nsems
0x01050001 65538
root 666
7
Semaphores are declared as arrays. The length of the array is
shown in the nsems column.If we get more details about the semaphore
localhost:root-~ $ ipcs -s -i 65538
Semaphore Array semid=65538
uid=0 gid=0 cuid=0
cgid=0
mode=0666, access_perms=0666
nsems = 7
otime = Fri Sep 21 20:04:42 2012
ctime = Fri Sep 7
20:01:16 2012
semnum value ncount
zcount pid
0 1 0 0 1403
1 1 0 0 1403
2 1 0 0 1403
3 1 0 0 2112
4 1 0 0 1403
5 1 0 0 1403
6 10 0 0 1403
The ncount is the number
of processes blocking on the semaphore, waiting for it to increment. The zcount
is the number of processes blocking on the semaphore, waiting for it to go to
zero. The pid column identifies the most recent process to complete a semaphore
operation.
The ps command can help
identify processes waiting on a semaphore. The wchan format option shows what
system function is blocking a process.
localhost:root-~ $ ps -o wchan -p 9953
WCHAN
pipe_wait
if you see a system call semtimedop , then it is sure that it is a semaphore .
localhost:root-~ $ ipcs -lm
------ Shared Memory Limits --------
max number of segments = 4096
max seg size (kbytes) = 67108864
max total shared memory (kbytes) = 17179869184
min seg size (bytes) = 1
To get more clear details about a IPC, we can use
localhost:root-~ $ ipcs -q -i 0x000004d2
Message Queue msqid=0
uid=0 gid=0 cuid=0
cgid=0 mode=0666
cbytes=0
qbytes=65536 qnum=0 lspid=15982 lrpid=5948
send_time=Sat Sep 22 23:55:24 2012
rcv_time=Sat Sep 22 23:55:24 2012
change_time=Mon Sep 3
02:30:14 2012
Option -i with -q provides information about a particular
message queue. Option -i with -s provides semaphore details. Option -i with -m
provides details about a shared memory.
Limits for IPC facility
We can also get the Limits of a Inter process facility using ,
localhost:root-~ $ ipcs -l
------ Shared Memory Limits --------
max number of segments = 4096
max seg size (kbytes) = 67108864
max total shared memory (kbytes) = 17179869184
min seg size (bytes) = 1
------ Semaphore Limits --------
max number of arrays = 512
max semaphores per array = 512
max semaphores system wide = 32000
max ops per semop call = 32
semaphore max value = 32767
------ Messages: Limits --------
max queues system wide = 4096
max size of message (bytes) = 65536
default max size of queue (bytes) = 65536
The above command gives the limits for shared memory. -l can be
combined with -q and -s to view the limits for message queue and semaphores
respectively.
Creator and Owner Details
ipcs -c option lists creator userid and groupid and owner userid
and group id. This option can be combined with -m, -s and -q to view the
creator details for specific IPC facility.
localhost:root-~ $ ipcs -m -c
------ Shared Memory Segment Creators/Owners --------
shmid perms cuid
cgid uid gid
4227074 644 root root root root
4259844 644 root root root root
4292613 644 root root root root
Current Usage
In Order to find the current usage we can use
localhost:root-~ $ ipcs -u
------ Shared Memory Status --------
segments allocated 7
pages allocated 10
pages resident 10
pages swapped 0
Swap performance: 0 attempts
0 successes
------ Semaphore Status --------
used arrays = 3
allocated semaphores = 9
------ Messages: Status --------
allocated queues = 4
used headers = 0
used space = 0 bytes
More to Come , Happy Learning