Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

I understand semaphores, but what are these semaphore arrays being used on my Linux box?

$ ipcs

------ Shared Memory Segments --------
key        shmid      owner      perms      bytes      nattch     status      
0x00000000 327681     root      644        80         2                       
0x00000000 360450     root      644        16384      2                       
0x00000000 393219     root      644        280        2                       
...

------ Semaphore Arrays --------
key        semid      owner      perms      nsems     
0x4172d4f4 290914305  lazer     660        104       
0x3b87b970 291045378  lazer     660        104       
0xa97eb380 293928963  lazer     660        104       
0x1fde2040 294191108  lazer     660        104       

------ Message Queues --------
key        msqid      owner      perms      used-bytes   messages    

$

Also, which OS resource are they guarding?

share|improve this question

migrated from stackoverflow.com Sep 19 '11 at 12:50

This question came from our site for professional and enthusiast programmers.

2 Answers 2

ipcs -i <SEMID> -s will give you more information on the specific sem array. E.g.

[me@home]$ ipcs -i 32769 -s

Semaphore Array semid=32769
uid=537  gid=85  cuid=537        cgid=85
mode=0600, access_perms=0600
nsems = 1
otime = Mon Sep 19 12:18:53 2011
ctime = Mon Sep 19 12:07:11 2011
semnum     value      ncount     zcount     pid
0          1          0          0          7548

Use the pid to figure out who's using it.

share|improve this answer
    
What's the difference between a "semaphore array" and a regular semaphore created using sem_init? –  Dan Cecile Sep 19 '11 at 11:25
    
I may be mistaken, but I believe the term "semaphore array" is used because using the SysV version of semaphores (semget()) you can create a semaphore set with multiple elements (nsems). For POSIX semaphoes (sem_init/sem_open), you're limited to a single value. –  Shawn Chin Sep 19 '11 at 12:24

Yeah I was confused by this.

Semaphore arrays are a SysV alternative to kernel semaphores for user processes.

They are a bit more complicated:

  • They use an array of values to protect several resources with one semaphore. So where linux kernel semaphores have the operations 'up'/'down' to inc/decrement the structure's value, sem_arrays have operations to edit any of the values in it's array.

  • They have undoable operations. A process can allow the kernel to roll back an operation if it happens to die unexpectedly.

Also, which OS resource are they guarding?

Since they are for user mode processes I wouldn't think that they are guarding any OS resources.

For more information: "Understanding the Linux kernel" - Chapter 19

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.