6

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?

HopelessN00b
  • 53,385
  • 32
  • 133
  • 208
Lazer
  • 415
  • 3
  • 7
  • 9

2 Answers2

6

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.

Shawn Chin
  • 1,804
  • 11
  • 12
  • What's the difference between a "semaphore array" and a regular semaphore created using `sem_init`? –  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
  • This doesn't answer the question of *what* a "semaphore array" actually *is*... – code_dredd Nov 03 '20 at 21:51
2

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

papples
  • 121
  • 1