GNU/Linux provides the ipcs command to explore semaphores from the command line. The ipcs utility provides information on a variety of resources; we ll explore its use below for investigating semaphores.
The general form of the ipcs utility for semaphores is:
# ipcs -s
This presents all the semaphores that are visible to the calling process. Let s now look at an example where we create a semaphore (as was done in Listing 16.1):
# ./semcreate semcreate: Created a semaphore 1769475 # ipcs -s Semaphore Arrays key semid owner perms nsems 0x0000006f 1769475 mtj 666 1 #
Here, we see our newly created semaphore ( key 0x6f ). We can get extended information about the semaphore using the -i option. This allows us to specify a specific semaphore ID, for example:
# ipcs -s -i 1769475 Semaphore Array semid=1769475 uid=500 gid=500 cuid=500 cgid=500 mode=0666, access_perms=0666 nsems = 1 otime = Not set ctime = Fri Apr 9 17:50:01 2004 semnum value ncount zcount pid 0 0 0 0 0 #
Here we see our semaphore in greater detail. We see the owner and creator process and group IDs, permissions, number of semaphores ( nsems ), last semop time, last change time, and the details of the semaphore itself ( semnum through pid ). The value represents the actual value of the semaphore (zero after creation). If we were to perform the release operation (see Listing 15.3) and then perform this command again, we d see:
# ./semrel semrel: Releasing semaphore 1769475 semrel: Semaphore released 1769475 # ipcs -s -i 1769475 Semaphore Array semid=1769475 uid=500 gid=500 cuid=500 cgid=500 mode=0666, access_perms=0666 nsems = 1 otime = Fri Apr 9 17:54:44 2004 ctime = Fri Apr 9 17:50:01 2004 semnum value ncount zcount pid 0 1 0 0 20494 #
Note here that our value has increased (based upon the semaphore release), and other information (such as otime and pid ) has been updated given a semaphore operation having been performed.
We can also delete semaphores from the command line using the ipcrm command. To delete our previously created semaphore, we d simply use the ipcrm command as follows :
# ipcrm -s 1769475 [mtj@camus ch16]$ ipcs -s Semaphore Arrays key semid owner perms nsems #
As with the semop and semctl API functions, the ipcrm command uses the semaphore identifier to specify the semaphore to be removed.