AT/T Bell Labs, the early process of UNIX communication was improved and expanded, forming a "system V IPC", the communication process is mainly confined to a single computer. The IPC object refers to shared memory (share memories), message queue, and semaphore set (semaphore).
Semaphore (semaphore), also called Semaphore. It is a mechanism for synchronizing between different threads in different processes or within a given process. The System v Semaphore is a collection of one or more semaphores. Each of these is a separate counting beacon. System V beacons are maintained by the kernel. The main function is semget,semop,semctl.
This article focuses on the SEMOP function. The main function of this function is the p/v operation of 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);
Semop operation: The SEM_FLG member of the SEMBUF structure can be 0, ipc_nowait, Sem_undo. When Sem_undo, it will allow the operating system to track the current process's modification of this semaphore, and if the process terminates without releasing the semaphore, the operating system will automatically release the process.
When the SEM_FLG member of the SEMBUF structure is Sem_undo, it will allow the operating system to track the current process's modification of the semaphore, and if the process terminates without releasing the semaphore, the operating system will automatically release the semaphore held by the process. Prevents other processes from having a "deadlock phenomenon" because of no semaphore.
Test code:
Sem.h
650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M02/84/82/wKiom1eSQAyzfLjgAACW87o0K8o305.png "title=" Sem.h.png "alt=" Wkiom1esqayzfljgaacw87o0k8o305.png "/>
Sem.c
650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M01/84/82/wKiom1eSQCrxvSQNAACpPlL0Fqc475.png "style=" float: none; "title=" Sem.c1.png "alt=" Wkiom1esqcrxvsqnaacppll0fqc475.png "/>
650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M02/84/82/wKiom1eSQCuxgmCvAACnZlYHHcA604.png "style=" float: none; "title=" Sem.c2.png "alt=" Wkiom1esqcuxgmcvaacnzlyhhca604.png "/>
TEST.c
650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M02/84/82/wKioL1eSQEixljffAAB53ctfQqw638.png "style=" float: none; "title=" Code_1.png "alt=" Wkiol1esqeixljffaab53ctfqqw638.png "/>
650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M00/84/82/wKiom1eSQEjB4iBkAAAdrF5EQmU215.png "style=" float: none; "title=" Code_2.png "alt=" Wkiom1esqejb4ibkaaadrf5eqmu215.png "/>
Run:
650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M02/84/82/wKiom1eSQIfzTsjAAABdTNyUpA8175.png "title=" Sem.sem_flg.png "alt=" Wkiom1esqifztsjaaabdtnyupa8175.png "/>
Run 1 set SEM.SEM_FLG to 0 "End child process deadlock phenomenon"
650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M00/84/82/wKioL1eSQNOiEDEQAAEcDMT7IFE644.png "title=" sem_ Undo.png "alt=" Wkiol1esqnoiedeqaaecdmt7ife644.png "/> Run 2 set SEM.SEM_FLG to Sem_undo "Terminate subprocess System Automatic v operation does not deadlock phenomenon"
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/84/82/wKioL1eSQRuTy8XqAAEXDn9V9xw083.png "title=" Use_ Sem_undo.png "alt=" Wkiol1esqruty8xqaaexdn9v9xw083.png "/>
Conclusion:
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 "City Hunter" blog, please be sure to keep this source http://alick.blog.51cto.com/10786574/1828983
Sem_undo Flag for Linux semaphores (to prevent deadlocks)