Semaphore function semget () semop () semctl ()

Source: Internet
Author: User

Semaphores are a mechanism for controlling the access to shared resources by multiple processes. In fact, they are introduced to solve the synchronization problem of mutually exclusive shared resources.
A semaphore cannot be defined separately, but only one semaphore set can be defined, including a set of semaphores. It is agreed that the semaphores in the semaphores set use the same
A reference ID. This setting is required for multiple resources or synchronization operations.
System Call functions related to semaphores:
1. semget ()
Prototype: int semget (key_t key, int nsems, int semflg );
The parameter key indicates the key for creating or opening the semaphore set.
The nsems parameter indicates the number of semaphores in the created semaphores set. This parameter is valid only when the semaphores set is created.
The flag parameter indicates the operation type of the called function. It can also be used to set the access permission of the semaphore set. The two parameters are expressed by or.
When semget is called to create a semaphore, its corresponding semid_ds structure is initialized. Each volume in ipc_perm is set to the corresponding
Value. sem_nsems is set to the value shown in nsems, sem_otime is set to 0, and sem_ctime is set to the current time.
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)
Enospc (exceeds the limit)
The first parameter of the System Call semget () is the keyword value (generally returned by the System Call ftok ). The system kernel
Compare the keyword values of other semaphore Sets
Relatively. The open and access operations are related to the content in the semflg parameter. Ipc_creat if
If the semaphore set does not exist in the system kernel, a semaphore set is created. Ipc_excl when and
When ipc_creat is used together, if the semaphore set has
If yes, the call fails. If ipc_creat is used separately, semget () either returns the ID of the newly created semaphore set
Or return
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
ID of the semaphore set created
Or return-1. It is meaningless to use ipc_excl independently. The nsems parameter indicates a new semaphore set.
The number of semaphores to be created.
2. semop ()
Call prototype: int semop (INT Semid, struct sembuf * SOPs, unsign ednsops );
Semid is the reference ID of the semaphore set.
Semoparray is an array of sembuff structures. The sembuff structure is used to specify the operations performed by calling the semop function.
The number of semoparray elements is indicated by the NOPs parameter.
Semoparray is an array, where each element table is an operation, because this function is an atomic operation, once executed
All operations in the array will be executed.
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)
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,
Then the calling process enters the sleep state until the signal
The amount of resources can be used. If sem_op is a positive number, the semaphore is added
Its value. This means that the process releases resources controlled by semaphores. Finally, if sem_op is 0, the process is called.
Will call sleep (),
Until the semaphore value is 0. This is used when a process is waiting for completely idle resources.
3. semctl ()
Prototype: int semctl (INT Semid, int semnum, int cmd, Union semun Arg );
Semid is the reference identifier of the semaphore set.
Semnum is used to specify a specific semaphore.
CMD indicates the operation to call the function. The value and corresponding operation are 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.
Arg is a copy of the Union type, rather than a pointer to the Union type. Usage of various volumes in the Union
It depends on the CMD parameter settings.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.