C language programming in Linux 6-semaphore function semget () semop () semctl ()

Source: Internet
Author: User
Tags semaphore

Semget ()


Create a new semaphore set or use an existing semaphore set.
System Call: semget ();
Prototype: int semget (key_t key, int nsems, int semflg );
Parameter: the first parameter is the keyword value (generally returned by the System Call ftok ).
The second parameter nsems indicates the number of semaphores to be created in a new semaphores set. The semaphore set can be understood as an array with a length of nsems. The maximum number is defined in Linux/SEM. H. In ubuntu8, the maximum number is 250.
Return Value: If successful, the IPC identifier of the semaphore set is returned. If it fails,-1 is returned.
Errno is not described.

Semop ()


Semaphore operation
Call prototype: int semop (INT Semid, struct sembuf * SOPs, unsigned nsops );
Parameter: the first parameter is the keyword value returned by semget. The second parameter is the pointer to the array to be operated. The third parameter is the number of operations in the array. Nsops is the size of the array SOPs.
The second parameter structure is as follows:
Struct sembuf {
Unsigned short sem_num;/* use the sem_num semaphore */
Short sem_op;/* semaphore operation */
Short sem_flg;/* operation flag */
};
Sem_num corresponds to the signal light in the signal set, and 0 corresponds to the first signal light. Sem_flg can be ipc_nowait and sem_undo. If the sem_undo flag is set, the corresponding operation will be canceled at the end of the process, which is an important flag. If this flag is set, the kernel is released when the process does not release shared resources. The sem_op value is greater than 0, equal to 0, and less than 0. Three operations are performed on the signal lamp specified by sem_num.
Sem_op> 0: The process needs to release the shared resources of the number of sem_op;
Sem_op = 0: used to test whether shared resources are used up;
Sem_op <0: the process must apply for-sem_op shared resources.
Personal Understanding: when a process wants to use a certain resource, first judge whether sem_op is 0, then set sem_op> 0, and then sem_op <0.
Return Value: 0. Success. -1, failed.
Errno is not described.

Semctl ()


Various control operations on traffic signals are implemented.
System Call: semctl ();
Prototype: int semctl (INT Semid, int semnum, int cmd, Union semunarg );
Parameters:
The Semid parameter specifies the traffic signal set, and the semnum parameter specifies the traffic signal to which the traffic signal is operated. The cmd parameter specifies the specific operation type and only makes sense for several special cmd operations. semunarg is used to set or return traffic signal information, represents a semun instance. This parameter can be ignored.
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.
Return Value: If successful, it is a positive number.

Related Article

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.