Semaphore mechanism for the "Operating system summary" process synchronization

Source: Internet
Author: User
Tags semaphore

The semaphore mechanism was presented by Dijkstra 1965 and is an important tool for resolving process synchronization

The following methods apply to access the same critical section as several processes

Shaping Signal Volume

Defines an shaping semaphore s that represents the number of resources, can only synchronize P, v operation changes. The original value is S=1, and each process comes in the execution:

    1. The wait () operation is performed first, and if the semaphore s<=0 is always detected and blocked
    2. If not less than 0 will enter the critical section and set S to 0, to ensure that other processes can not enter the critical section
    3. After the critical section is executed, the semaphore is set back.
wait(S) {    while(S <= 0);//资源如果小余0就会阻塞。一直在这边检测    S--;//进入临界区就会将}signal(S) {    S ++;//在临界区执行完了,就会把信号量+1}
Recorded signal volume

Plastic signal volume does not follow the "right to wait principle", process one but not to go to the critical section in that has been detected, do not release the processor, so there is a record-type signal volume

    1. First a process requests the class resource, performs a wait operation, discovers Value<0, enters the block, inserts the list, releases the processor
    2. When another process no longer uses the resource, it performs a signal operation and discovers that value is less than 0, which wakes up the blocking queue in the linked list

Semaphore S is a structure

typedefstruct {    int value;//表示资源数目的整形变量    struct process_control_block *list;//进程链表,阻塞的进程都会放到这里}
Wait (semaphore*s) {s -Value--;//Perform a wait operation first, then Value-1,    if(S -Value< 0) Bolck (S -List);//If value is found to be less than 0, it will block and put the process into the list}signal (semaphore*s) {s -Value++;//Let value+1    if(S -Value<= 0) Wakeup (S -List);//If the discovery is still less than 0, it proves that the process is blocking, waking the first process in the linked list}

The following is access to multiple critical resources for multiple concurrent processes

and type signal quantity

And the idea of signal volume, is the process of the entire operation of the necessary resources, a one-time allocation to him, and then the process is finished, and then released once. If one is unassigned, other resources are not assigned to the process, which prevents deadlock problems

    1. The process performs the swait operation first, and if all resources can be allocated, allocate all resources to the process
    2. When the process finishes executing, the Ssignal action frees the resource and then wakes up the waiting queue for another process
Swait (S1, S2,.........Sn) { while(true) {if(S1>=1&&....&&Sn>=1) {//detects that all resources can be assignedfor (i=I I<N I++) {for (i=I I<N I++) Si--;//Total number of resources-1Break }        }Else{//Enter the blocking queue, waiting for resources}}}ssignal (S1, S2,......Sn) { while(true) {for (i=1; I<N;i++) {//Release all resourcesSi++;//Wake up process in waiting queue}    }}
Semaphore set

The above can only perform +1 or 1 operations on the semaphore, each time only one unit of resources can be requested or released, when the need for n units, it is necessary to execute n this wait operation, which is very inefficient

The expansion of the signal volume of the and type signal set

    1. The minimum allocation limit for this resource is Ti
    2. The Si > ti can be assigned when the resource Si is above the lower limit of his allocation;
    3. Di is the process's demand for the resource, and the resource is allocated later to process Si = Si-di
    4. When the process does not require resources, release will be carried out si = si + di;
Swait (S1, T1, D1,.........Sn, TN, DN) { while(true) {if(S1>=T1&&....&&Sn>=TN) {//detects that all resources can be assignedfor (i=I I<N I++) {for (i=I I<N I++) Si=Si-Di//All resources D1, the amount of resources requiredBreak }        }Else{//Enter the blocking queue, waiting for resources}}}ssignal (S1, D1,......Sn, DN) { while(true) {for (i=1; I<N;i++) {//Release all resourcesSi=Si+Di//Wake up process in waiting queue}    }}
Special cases
    1. Swait (S, D, D) has only one semaphore, which is the only resource needed, and the existing resources cannot be less than D
    2. Swait (S, 1, 1) is the general semaphore mechanism (shaping the semaphore mechanism)
    3. Swait (S, 1, 0) when S >= 1 o'clock, allows multiple processes to enter the critical section, or any process will be organized into the critical section

Semaphore mechanism for the "Operating system summary" process synchronization

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.