[Operating system] Computer operating system - application of semaphore, PV operation classic exercises and code representation

Topic 1:

An automatic counting system is set up at a traffic intersection, which consists of an "observer" process and a "reporter" process. The observer process can identify trucks and count the passing trucks; the reporter process will print out the count value of the observer at regular intervals (can be set to every 1 hour, on time), and clear the count value to "0" after each printing . The concurrent execution of the two processes can complete the hourly traffic statistics of trucks.

code show as below:

struct semaphore S ;
int count = 0 ;
S.value = 1 ;

process observer
{  
    while (condition)
    {
       observe a lorry;
       wait(S);
      count = count + 1; 
      signal(S);
    } 
}

process reporter
{
    while (condition)
    {
       wait(S);
      print(count);
      count = 0;
      signal(S);
    } 
}

Topic 2:

Using semaphores, according to the predecessor relationship of the statement, write a program that can be concurrent

The figure shows:

There is statement S1 in process P1;

There is a statement S2 in the process P2;

......

Statements S2 and S3 can only be executed after statement S1 is executed;

Statements S4 and S5 can only be executed after statement S2 is executed;

·After statements S3, S4 and S5 are executed, statement S6 can be executed.

code show as below:

struct semaphore a,b,c,d,e,f,g ;
a.value=b.value=c.value=0;
d.value=e.value=g.value=0;

parbegin  /*parbegin Indicates the start of concurrent execution*/
process P1: { S1;signal(a);signal(b); }
process P2: 
   { wait(a);S2;signal(c);signal(d); }
process P3: { wait(b);S3;signal(e); }
process P4: { wait(c);S4;signal(f); }
process P5: { wait(d);S5;signal(g); }
process P6: 
     { wait(e);wait(f);wait(g);S6; }
parend    /*Use parend to indicate the end of concurrent execution */

Write a program that can be executed concurrently. Write a program that can be executed concurrently. Write a program that can be executed concurrently.

Topic 3:

In a box, equal numbers of black and white Go pieces are mixed. Now use the automatic sorting system to separate the sunspots from the whites. The sorting system has two processes P1 and P2, wherein P1 sorts the whites, and P2 picks the blacks. It is stipulated that when a process picks a child, another process must be allowed to pick it. Use semaphore and PV operations to coordinate the activities of the two processes.

code show as below:

Struct semaphore S1, S2;
S1.value=1; S2.value=0;

cobegin
process P1(){             
   while(true){                
       P(S1);                         
       pick white                        
       V(S2);
   }
}                      

process P2(){ 
    while(true){
        P(S2);
        pick sunspots
        V(S1);
    }
}

coend

Topic 4:

The library stipulates that every reader who enters the library must be registered on the registration form, and must be canceled on the registration form when exiting.

(1) Use semaphores to realize mutual exclusion registration and cancellation between readers.

(2) The library has a total of 100 seats. When there are no empty seats in the library, readers who arrive later will have to wait (block) in the library

code show as below:

Struct semaphore MUTEX; 
MUTEX.value=1;
Struct semaphore  S; 
S.value=100;  

Readeri(){
    P(S);
    P(MUTEX);
    registration;
    V(MUTEX);
    read;
    P(MUTEX);
    cancel;
    V(MUTEX);
    V(S);
}


Topic 5:

A family of four, father, mother, son, and daughter sit around a table; there is a fruit plate on the table; when the fruit plate is empty, the father can put bananas or the mother can put apples, but when there is fruit in the plate, they cannot put it, parents wait. When there are bananas on the plate, the daughter can eat bananas, otherwise, the daughter waits; when there are apples on the plate, the son can eat them, otherwise, the son waits.

code show as below:

struct semaphore S1, S2, S3;
S1.value = 1;
S2.value = 0;
S3.value = 0;

father(){
    wait(S1);
    put bananas;
    signal(S2);
}

mather(){
    wait(S1);
    put apples;
    signal(S3);
}

son(){
    wait(S3);
    eat apples;
    signal(S1);
}

daughter(){
    wait(S2);
    eat bananas;
    signal(S1);
}

Topic 6:

On the bus, the activities of the driver and the conductor are:

Driver's activity: Start the vehicle

         normal operation

Stop at the station

Activities of the conductor: close the door

Ticket sales

Open the door

When the car is constantly arriving at the station, parking, and driving, the driver and the conductor

What synchronization relationship does the activity have? Use semaphore and P, V operation to achieve.

code show as below:

struct semaphore S1, S2;
S1.value = 0;
S2.value = 0;

driver(){
    wait(S1);
    start the vehicle;
    vehicle driving;
    stop driving;
    signal(S2);
}

casher(){
    close the car door;
    signal(S1);
    ticket sales;
    wait(S2);
    open the door;
}

Topic 7:

There is a supermarket that can accommodate up to N people for shopping. When the N customers are full, the customers who arrive later will wait outside the supermarket; there is only one cashier in the supermarket. Customers and cashiers can be regarded as two types of processes, and there is a synchronization relationship between the two types of processes. Write the algorithm of two kinds of processes realized by P;V operation

code show as below:

struct semaphore S, S1, S2, S3;
S.value = N;
S1.value = 0;
S2.value = 0;
S3.value = 1;

customer(){
    wait(S);
    Enter;
    Shopping;
}

wait(S3){
    to pay;
    signal(S1);
    wait(S2);
    leave;
    signal(S);
}

casher(){
    wait(S1);
    collect money;
    signal(S3);
    signal(S2);
}

Topic 8:

There is a single-plank bridge in the east-west direction; use P,V operations to achieve:

(1) Only one person is allowed to cross the bridge at a time;

(2) When there are pedestrians on the single-plank bridge, pedestrians in the same direction can cross the bridge at the same time, and those in the opposite direction must wait.

(3) When there are pedestrians on the single-plank bridge from east to west, pedestrians in the same direction can cross the bridge at the same time, and only one person is allowed to cross the bridge alone from west to east.

code show as below:

semaphore wait, mutex1, mutex2;

mutex1.value = mutex2.value = 1; wait = 0;

int count1, count2;

count1 = count2 = 0;



begin_together

process P East(){

​	P(mutex1);

​	count1++;

​	if(count1 == 1)

​		P(wait);

​	V(mutex1);

​	pass the bridge;

​	P(mutex1);

​	count1--;

​	if(count1 == 0)

​		V(wait);

​	V(mutex1);

}

process P West(){

​	P(mutex2);

​	count2++;

​	if(count2 == 1)

​		P(wait);

​	V(mutex2);

​	pass the bridge;

​	P(mutex2);

​	count2--;

​	if(count2 == 0)

​		V(wait);

​	V(mutex2);

}

end_together

Tags: Linux Algorithm C++ programming language

Posted by ctjansen on Thu, 12 Jan 2023 19:17:42 +0530