This section describes how to use the semget system call. The accompanying program illustrates its use.
The synopsis found on the semget(2) manual page is as follows:
#include <sys/types.h> #include <sys/ipc.h> #include <sys/sem.h>
int semget (key, nsems, semflag) key_t key; int nsems, semflag;
The following line in the synopsis:
int semget (key, nsems, semflg)informs you that semget is a function with three formal arguments that returns an integer-type value. The next two lines:
key_t key; int nsems, semflg;declare the types of the formal arguments. key_t is defined by a typedef in the sys/types.h header file to be an integer.
The integer returned from this system call upon successful completion is the semaphore set identifier that was discussed above.
The process calling the semget system call must supply three actual arguments to be passed to the formal key, nsems, and semflg arguments.
A new semid with an associated semaphore set and data structure is created if either
The value passed to the semflg argument must be an integer that will specify the following:
``Operation permissions codes'' reflects the numeric values (expressed in octal notation) for the valid operation permissions codes.
Operation permissions codes
|Operation permissions||Octal value|
|Read by user||00400|
|Alter by user||00200|
|Read by group||00040|
|Alter by group||00020|
|Read by others||00004|
|Alter by others||00002|
A specific value is derived by adding or bitwise ORing the values for the operation permissions wanted. That is, if read by user and read/alter by others is desired, the code value would be 00406 (00400 plus 00006). There are constants #define'd in the sys/sem.h header file which can be used for the user (OWNER). They are as follows:
SEM_A 0200 /* alter permission by owner */ SEM_R 0400 /* read permission by owner */Control flags are predefined constants (represented by all upper-case letters). The flags that apply to the semget system call are IPC_CREAT and IPC_EXCL and are defined in the sys/ipc.h header file.
The value for semflg is, therefore, a combination of operation permissions and control commands. After determining the value for the operation permissions as previously described, the desired flag(s) can be specified. This specification is accomplished by adding or bitwise ORing (|) them with the operation permissions; the bit positions and values for the control commands in relation to those of the operation permissions make this possible.
The semflg value can easily be set by using the flag names in conjunction with the octal operation permissions value:
semid = semget (key, nsems, (IPC_CREAT | 0400));
semid = semget (key, nsems, (IPC_CREAT | IPC_EXCL | 0400));
As specified on the semget(2) manual page, success or failure of this system call depends upon the actual argument values for key, nsems, and semflg, and system-tunable parameters. The system call will attempt to return a new semaphore set identifier if one of the following conditions is true:
The key argument can be set to IPC_PRIVATE like this:
semid = semget(IPC_PRIVATE, nsems, semflg);
Exceeding the SEMMNI, SEMMNS, or SEMMSL system-tunable parameters will always cause a failure. The SEMMNI system-tunable parameter determines the maximum number of unique semaphore sets (semid's) that may be in use at any given time. The SEMMNS system-tunable parameter determines the maximum number of semaphores in all semaphore sets system wide. The SEMMSL system-tunable parameter determines the maximum number of semaphores in each semaphore set.
IPC_EXCL is another control command used in conjunction with IPC_CREAT. It will cause the system call to return an error if a semaphore set identifier already exists for the specified key provided. This is necessary to prevent the process from thinking that it has received a new (unique) identifier when it has not. In other words, when both IPC_CREAT and IPC_EXCL are specified, a new semaphore set identifier is returned if the system call is successful. Any value for semflg returns a new identifier if the key equals zero (IPC_PRIVATE) and no system-tunable parameters are exceeded.
Refer to the semget(2) manual page for specific associated data structure initialization for successful completion. The specific failure conditions and their error names are contained there also.