Semaphore application

Use semaphores to achieve mutual exclusion

In order to process a plurality of mutually exclusive access to a critical resource shall be a mutex semaphore for mutex resource settings, and set the initial value of 1, and then processes the critical region of each access resource CS placed the wait (mutex) signal and between (mutex).

For example, a record semaphore achieve two mutual exclusion applies a printer:

semaphore  mutex =1;        //表示打印机
        begin
        parbegin
            p1: begin
                       repeat
                           … …
                           wait(mutex);
                           使用打印机
                           signal(mutex);
                           … …
                        until false;
                    end
            p2: begin
                       repeat
                          … …
                          wait(mutex);
                          使用打印机
                          signal(mutex);
                          … …
                      until false; 
                 end
        parend 
        end      

Use semaphores to achieve predecessor relationship

 Suppose there are two processes concurrently executing P1, P2, P1 has statements S1, P2 there are statements S2, S2 after S1 want to perform execution. The solution is: the process P1, P2 share a common semaphore S, and S assigned to 0.

进程P1:   S1;
           signal(S);

进程P2:   wait(S);
           S2;

Since the initial value of S is 1, wait operation process P2 is not adopted, so the statement is not executed S2. P1 and the process does not wait operation may be performed directly S1 statement, the statement and then execute signal, S ++, process time P2 may execute entirely finished.

According to a simple example, the code can be introduced when the precursor complex relationship:

 The first step: Suppose shared semaphore S1 to S2 is a

semaphore a = 0;
    begin
        parbegin
            begin S1; signal(a); end;
            begin wait(a); S2; end;
        parend
    end

Step Two: Suppose shared semaphores S1 to S3 is b

semaphore a, b = 0;
    begin
        parbegin
            begin S1; signal(a); signal(b); end;
            begin wait(a); S2; end;
            begin wait(b); S3; end;
        parend
    end

The third step: Suppose shared semaphore S2 to S4 is c 

semaphore a, b, c = 0;
    begin
        parbegin
            begin S1; signal(a); signal(b); end;
            begin wait(a); S2; signal(c); end;
            begin wait(b); S3; end;
            begin wait(c); S4; end;
        parend
    end

The fourth step: Suppose shared semaphore S2 to S5 is d

semaphore a, b, c, d = 0;
    begin
        parbegin
            begin S1; signal(a); signal(b); end;
            begin wait(a); S2; signal(c); signal(d); end;
            begin wait(b); S3; end;
            begin wait(c); S4; end;
            begin wait(d); S5; end;
        parend
    end

 Fifth step: Suppose S3 to S6, S5 to S6, the amount of shared signal S4 to S6, respectively, e, f, g

semaphore a, b, c, d, e, f, g = 0;
    begin
        parbegin
            begin S1; signal(a); signal(b); end;
            begin wait(a); S2; signal(c); signal(d); end;
            begin wait(b); S3; signal(e); end;
            begin wait(c); S4; signal(g); end;
            begin wait(d); S5; signal(f); end;
            begin wait(e); wait(f); wait(g); end;
        parend
    end

Using the synchronized recording semaphore

p1, p2 due to two processes cooperate to accomplish a task and a shared variable x. The result of the processing into the process p2 x, x is the result of a process p1 will print. How to achieve partnership?

semaphore empty=1;  //变量x可赋值使用,即P1的print(x)已完成
semaphore full=0;   //变量x已赋值,即P1可print(x)
    begin
        parbegin
            p1: begin
                       repeat
                          … …
                          wait(full);
                          print(x);
                          signal(empty);
                          … …
                      until false;
                end
            p2: begin
                       repeat
                         … …
                         wait(empty);
                         x:=处理结果;
                         signal(full);
                         … …
                       until false; 
                end

        parend
    end
        

Here the use of a semaphore mechanism to achieve a synchronous mode, and producer-consumer model similar to prevent the occurrence of deadlock. When a deadlock occurs in the recording semaphore and more resource sharing is handled with AND semaphore, here describes how to write semaphore resolve the deadlock problem.

 

 

Published 79 original articles · won praise 0 · Views 3039

Guess you like

Origin blog.csdn.net/weixin_43237362/article/details/104739573