What are the semaphore arrays on Linux?

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.


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