Shared Memory & Semaphore: ipcs & ipcrm

Posted: November 4, 2011 in Unix Commands
Tags: , , ,

Shared memory and Semaphores are types of IPC. IPC stands for Inter Process Communication. There are couple more IPCs, namely Pipes & Message Queue. I am not going to go deep into the details of these IPCs because its not in the context here. Lets directly go to the commands under discussion:

ipcs: To display the shared memory and semaphores.

Example: ipcs command with -a option lists all the IPC facilities which can be read by the current process. It provides details about message queue, semaphore and shared memory.

# ipcs -a

------ Shared Memory Segments --------
key        shmid      owner      perms      bytes      nattch     status
0xc616cc44 1056800768 oracle    660        4096       0
0x0103f577 323158020  root      664        966        1
0x0000270f 325713925  root      666        1          2

------ Semaphore Arrays --------
key        semid      owner      perms      nsems
0x0103eefd 0          root      664        1
0x0103eefe 32769      root      664        1
0x4b0d4514 1094844418 oracle    660        204

------ Message Queues --------
key        msqid      owner      perms      used-bytes   messages
0x000005a4 32768      root       644        0            0

Command to list all Message Queues, -q.

$ ipcs -q

------ Message Queues --------
key        msqid      owner      perms      used-bytes   messages
0x000005a4 32768      root       644        0            0

List all the Semaphores, -s.

# ipcs -s

------ Semaphore Arrays --------
key        semid      owner      perms      nsems
0x0103eefd 0          root      664        1
0x0103eefe 32769      root      664        1
0x4b0d4514 1094844418 oracle    660        204

List all Shared Memories, -m.

# ipcs -m

------ Shared Memory Segments --------
key        shmid      owner      perms      bytes      nattch     status
0xc616cc44 1056800768 oracle    660        4096       0
0x0103f577 323158020  root      664        966        1
0x0000270f 325713925  root      666        1          2

ipcrm: To remove the shared memory and semaphores present in the memory.

The following options shall be supported:

-q  msgid
Remove the message queue identifier msgid from the system and destroy the message queue and data structure associated with it.
-m  shmid
Remove the shared memory identifier shmid from the system. The shared memory segment and data structure associated with it shall be destroyed after the last detach.
-s  semid
Remove the semaphore identifier semid from the system and destroy the set of semaphores and data structure associated with it.
-Q  msgkey
Remove the message queue identifier, created with key msgkey, from the system and destroy the message queue and data structure associated with it.
-M  shmkey
Remove the shared memory identifier, created with key shmkey, from the system. The shared memory segment and data structure associated with it shall be destroyed after the last detach.
-S  semkey
Remove the semaphore identifier, created with key semkey, from the system and destroy the set of semaphores and data structure associated with it.

Note: Please refer manual pages for more information on these commands.

Background: In any application using large database, there is a tendency to use shared memory concept to speed up the response time. In such applications, after updating the database, if the new change didn’t reflect in the list or GUI, try clearing the shared memory/semaphores by using the above mentioned commands.

I faced such an issue, that’s why I have posted this. 🙂

Advertisements
Comments
  1. maes99 says:

    Its very helpful , Thanks

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s