The meaning of the Sem_flag setting value in the Sembu structure of the semop operation of the semaphore under Linux

Source: Internet
Author: User
Tags semaphore signal handler

The main function of the SEMOP function is to perform p/v operation on the semaphore.

P operation is responsible for converting the current process from a running state to a blocking state until another process wakes it up. The operation is: apply for an idle resource (reduce the signal volume by 1), if successful, then exit; if it fails, the process is blocked;

The V operation is responsible for waking up a blocked process, which has a parameter table that holds the process information waiting to be woken up. The action is to release an occupied resource (add a semaphore of 1) and select a wake-up if a blocked process is found.

The SEMOP function is prototyped as follows:

int semop (int semid, struct sembuf *sops, unsigned nsops);

The Semid identifies the set of semaphores for which it operates.

Where SOPs points to an array of the following structures:

struct sembuf{

unsigned short sem_num; /* Semaphore number:0, 1, ....., Nsems-1 */

Short Sem_op; /* Semaphore operation: <0, 0, >0 */

Short Sem_flag; /* Semaphore flags:0, Ipc_nowaite, Sem_undo */

};

The Nops parameter indicates the number of elements in the array of SEMBUF structures that are directed by SOPs. Each element in the array specifies an operation for a specific semaphore within the target semaphore set. This particular semaphore is specified by Sem_num, 0 represents the first element, 1 represents the second element, and so on, until Nsems-1, where Nsems is the number of member semaphores in the target semaphore set.

There are three types of Sem_flag:

1). 0 represents a blocking call

2). Ipc_nowait represents a non-blocking call

3). If the Sem_undo flag is set, then at the end of the process, the corresponding operation will be canceled, which is a more important flag bit. If the flag bit is set, the kernel will release on behalf of the process if it exits without releasing the shared resource. If the flag is set for a semaphore, the kernel allocates a Sem_undo structure to record it, in order to ensure that resources are released safely later. In fact, if the process exits, then its resources are freed, but the semaphore value has not changed, at this time, the semaphore value is not reflected in the actual situation of the resource occupancy, in this case, the problem is solved by the kernel to complete. This is a bit like a zombie process, although the process exits and the resource is freed, but the kernel process table still has its records, which requires the parent process to call Waitpid to resolve the problem.

Semop the operation of the signal is determined by the value of the SEM_OP, the following is an analysis of the value of the SEM_OP:

  • when the sem_op is positive, The value of the Sem_op is added to the signal value of the operating semaphore. If the SEM_FLG is set to Ipc_undo, the signal value is reset to the value before the call to the SEMOP function, regardless of whether the program ends properly or not. This corresponds to the number of resources consumed by the process release.

  • when the sem_op is negative , if the semaphore to be manipulated is greater than or equal to the absolute value of SEM_OP, the value of Sen_op is added from the semaphore value.

  • If the semaphore is less than the absolute value of sem_op, it is as follows:

    • If the value of SEM_FLG is ipc_nowait, then Semop error, return Eagain.

    • If the SEM_FLG is not set to Ipc_nowait, the value of the semncnt of the semaphore is added 1, and then the process hangs until the value of this semaphore is greater than the absolute values of sem_op, or the semaphore is removed from the system, and SEMOP is returned SEMOP , or the pending process catches the signal and returns from the signal handler, SEMOP error returns EINTR.

    • if the value of Sem_op is 0 , the calling process wants to wait until the semaphore value becomes 0.

      If one of the processes is killed by the kill command, the process has not yet performed a V operation to release resources. With the Sem_undo flag, the operating system automatically frees the semaphore held by the process, allowing another process to continue working. Without this flag, another process will block P operations forever.

      Therefore, it is generally recommended to use the Sem_undo flag.


This article is from the "fringe" blog, so be sure to keep this source http://ab6107.blog.51cto.com/10538332/1764751

The meaning of the Sem_flag setting value in the Sembu structure of the semop operation of the semaphore under Linux

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.