# Application of semaphore

Posted by jaql on Sun, 08 Mar 2020 13:38:13 +0100

## Using semaphore to realize process mutual exclusion

In order to make multiple processes mutually exclusive to access a critical resource, a mutex semaphore must be set for the resource, and the initial value is 1. Then, the critical area CS of each process accessing the resource is placed between wait(mutex) and signal(mutex).

For example, using recording semaphores to realize mutual exclusion of two processes is applicable to a printer:

```semaphore  mutex =1;        //Represents a printer
begin
parbegin
p1: begin
repeat
... ...
wait(mutex);
//Using the printer
signal(mutex);
... ...
until false;
end
p2: begin
repeat
... ...
wait(mutex);
//Using the printer
signal(mutex);
... ...
until false;
end
parend
end
```

## Using semaphore to realize precursor relationship

Suppose there are two concurrent processes P1 and P2. P1 has statement S1 and P2 has statement S2. You want to execute S2 after S1 is executed. The solution is to make process P1 and P2 share a common semaphore s, and assign s to 0.

```process P1:    S1;
signal(S);

//Process P2: wait(S);
S2;
```

Because the initial value of S is 1, the wait operation of process P2 cannot be passed, so S2 statement cannot be executed. But process P1 has no wait operation, so it can directly execute S1 statement, and then execute signal statement, S + +. At this time, process P2 can be executed completely. According to the first simple example, we can deduce the code of complex precursor relationship:

Step 1: suppose the shared semaphore from S1 to S2 is a

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

Step 2: suppose the shared semaphore from 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```

Step 3: suppose the shared semaphore from 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```

Step 4: suppose the shared semaphore from 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```

Step 5: assume that the shared semaphores of S3 to S6, S5 to S6, and S4 to S6 are e, f, and g respectively

```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```

## Synchronization by using recorded semaphores

p1 and p2 share a variable x because they cooperate to complete a task. Process p2 sends the processing result to x, and process p1 prints the result of X. How to achieve partnership?

```semaphore empty=1;  //Variable x can be assigned, i.e. print(x) of P1 has been completed
semaphore full=0;   //Variable x is assigned, i.e. P1 is printable (x)
begin
parbegin
p1: begin
repeat
... ...
wait(full);
print(x)；
signal(empty);
... ...
until false;
end
p2: begin
repeat
... ...
wait(empty);
x:=Treatment results;
signal(full);
... ...
until false;
end

parend
end

```

In this paper, the semaphore mechanism is used to implement the synchronization mode, which is similar to the producer consumer model AND prevents deadlock. When multiple resources sharing of recording semaphore deadlock occurs, AND semaphore is used for processing. This paper describes the method of solving the deadlock problem of recording semaphore.  79 original articles published, praised 0, and 2955 visitors