Semget ()
You can use the system to call semget () to create a new semaphore set or access an existing semaphore set:
System Call: semget ();
Prototype: intsemget (key_t key, int nsems, int semflg );
Return Value: If successful, the IPC identifier of the semaphore set is returned. -1: errno = eaccess (no permission) is returned if the request fails)
Eexist (the semaphore set already exists and cannot be created)
Eidrm (semaphore set deleted)
Enoent (the semaphore set does not exist and ipc_creat is not used)
Enomem (not enough memory to create a new semaphore set)
The first parameter of the enospc system calling semget () is the keyword value (generally returned by the system calling ftok ). The system kernel compares this value with the keyword value of other semaphore sets in the system. The open and access operations are related to the content in the semflg parameter. Ipc_creat: If the semaphore set does not exist in the system kernel, a semaphore set is created. When ipc_excl is used together with ipc_creat, if the semaphore set already exists, the call fails. If ipc_creat is used separately, semget () either returns the identifier of the newly created semaphore set or the identifier of the semaphore that already exists with the same keyword value in the system. If ipc_excl and ipc_creat are used together, either the ID of the newly created semaphore set is returned.
Or return-1. It is meaningless to use ipc_excl independently. The nsems parameter indicates the number of semaphores that should be created in a new semaphores set. The maximum number of semaphores in the semaphores set is defined in Linux/SEM. h: # definesemmsl32/* <= 512maxnumofsemaphoresperid */
The following is a program for opening and creating a semaphore set:
Export pen_semaphore_set (key_t keyval, int numsems)
{
Intsid;
If (! Numsems)
Return (-1 );
If (SID = semget (mykey, numsems, ipc_creat | 0660) =-1)
{
Return (-1 );
}
Return (SID );
}
}; ========================================================== ======================================
Semop ()
System Call: semop ();
Call prototype: int semop (INT Semid, struct sembuf * SOPs, unsign ednsops );
Return Value: 0. If yes. -1. If it fails: errno = e2big (nsops is greater than the maximum number of OPS)
Eaccess (insufficient permissions)
Eagain (ipc_nowait is used, but the operation cannot continue)
Efault (the address to which SOPs points is invalid)
Eidrm (semaphore set deleted)
Eintr (receives other signals during sleep)
Einval (the semaphore set does not exist or the Semid is invalid)
Enomem (sem_undo is used, but there is not enough memory to create the required data structure)
Erange (signal value out of range)
The first parameter is the keyword value. The second parameter is the pointer to the array to be operated. The third parameter is the number of operations in the array. The SOPs parameter points to an array composed of sembuf. This array is defined in Linux/SEM. h:/* semop systemcall takes an array of these */
Structsembuf {
Ushortsem_num;/* semaphore index in array */
Eclipsem_op;/* semaphore operation */
Eclipsem_flg;/* operation flags */
The number of semaphores to be processed by sem_num.
The operation to be performed by sem_op.
Sem_flg operation flag. If sem_op is a negative number, the semaphore will subtract its value. This is related to the resources controlled by semaphores. If ipc_nowait is not used, the calling process goes to sleep until the resources controlled by the signal volume can be used. If sem_op is a positive number, the semaphore is added with its value. This means that the process releases resources controlled by semaphores. Finally, if sem_op is 0, the calling process calls sleep () until the semaphore value is 0. This is used when a process is waiting for completely idle resources. ========================================================== ======================================
Semctl ()
System Call: semctl ();
Prototype: int semctl (INT Semid, int semnum, int cmd, Union semunarg );
Return Value: If successful, it is a positive number.
-1: errno = eaccess (insufficient permissions)
Efault (the address to which Arg points is invalid)
Eidrm (semaphore set deleted)
Einval (the semaphore set does not exist or the Semid is invalid)
Eperm (EUID does not have the CMD permission)
Erange (signal value out of range)
The system calls semctl to perform control operations on the semaphore set. This is very similar to the system call of msgctl in the message queue. However, the two System Call parameters are slightly different. Because the signal volume is generally used as a semaphore set, rather than a separate semaphore. Therefore, you must know not only the IPC keyword value, but also the specific semaphores in the semaphores set. Both system calls use the CMD parameter, which indicates the specific command to be operated. The last parameter in the two system calls is different. In msgctl, the last parameter is a pointer to the data structure used in the kernel. We use this data structure to obtain information about message queues and set or change the access permissions and users of queues. However, the semaphores support additional optional values.
This requires a more complex data structure.
The first parameter that the system calls semctl () is the keyword value. The second parameter is the number of semaphores. The command that can be used in the CMD parameter is as follows:
· IPC _ stat reads the data structure semid_ds of a semaphore set and stores it in the Buf parameter of semun.
· Ipc_perm, an element in semid_ds, sets the data structure of the semaphore set. Its value is taken from the Buf parameter in semun.
· IPC _ rmid: removes the semaphore set from the memory.
· Getall is used to read the values of all semaphores in the semaphores set.
· Getncnt returns the number of processes waiting for resources.
· Getpid returns the PID of the last process that executes the semop operation.
· Getval returns the value of a single semaphore In the semaphore set.
· Getzcnt returns the number of processes that are waiting for completely idle resources.
· Setall: Set the values of all semaphores in the semaphores set.
· Setval: set the value of a separate semaphore In the semaphore set. The ARG parameter represents a semun instance. Semun is defined in Linux/SEM. h:
/* Arg for semctl systemcils .*/
Unionsemun {
Intval;/* value for setval */
Structsemid_ds * Buf;/* buffer for ipc_stat & ipc_set */
Ushort * array;/* array for getall & setall */
Structseminfo * _ Buf;/* buffer for ipc_info */
Void * _ pad; Val is used when the setval command is executed. Buf is used in the ipc_stat/ipc_set command. Represents the data structure of semaphores used in the kernel. The pointer used by array when the getall/setall command is used.
The following program returns the semaphore value. When the getval command is used, the last parameter in the call is ignored: intget_sem_val (intsid, intsemnum)
{
Return (semctl (SID, semnum, getval, 0 ));
} The following is an example of a practical application: # definemax_printers5
Printer_usage ()
{
Int X;
For (x = 0; x <max_printers; X ++)
Printf ("Printer % d: % d \ n \ r", X, get_sem_val (SID, x ));
} The following program can be used to initialize a new signal value: void init_semaphore (INT Sid, int semnum, int initval)
{
Union semunsemopts;
Semopts. Val = initval;
Semctl (SID, semnum, setval, semopts );
} Note that the last parameter in the System Call semctl is a replica of the Union type, rather than a pointer to the Union type.