2.7 Operating System (Reader-Writer Problem Philosopher Dining Problem Manager)

content

1. The reader-writer problem

2. The Philosophers Dining Problem 

accomplish

3. Monitor 

1. Why introduce a monitor?

2. Definition and basic characteristics of monitor

3. Extension 1: Use monitor to solve the problem of producer and consumer 

4. Extension 2: a mechanism similar to a monitor in Java 


  Personal Homepage: Personal Homepage
 Series Column: Operating System

1. The reader-writer problem

There are two concurrent processes of readers and writers, sharing a file. When two or more reading processes access the shared data at the same time, there will be no side effects, but if a writing process and other processes (reading process or writing process) Simultaneous access to shared data may result in data inconsistency errors. Hence the requirement:

①Allows multiple readers to perform read operations on the file at the same time;
②Only one writer is allowed to write information to the file;
③Any writer does not allow other readers or writers to work until the write operation is completed;
④The writer performs the write operation Before that, all existing readers and writers should quit.

insert image description here 

 1. Relationship analysis: find out the various processes described in the title, and analyze the synchronization and mutual exclusion relationship between them.
①, Two types of processes: writing process, reading process
②, mutual exclusion relationship: writing process - writing process, writing process - reading process. There is no mutual exclusion problem between the reading process and the reading process.
2. Arrange ideas : Determine the approximate sequence of P and V operations according to the operation flow of each process.
3. Set semaphore: Set the required semaphore, and determine the initial value of the semaphore according to the problem conditions. (The initial value of the mutual exclusion semaphore is generally 1, and the initial value of the synchronization semaphore depends on the initial value of the corresponding resource)
 

 

insert image description here 

 insert image description here

  • The reader-writer problem provides a reference idea for us to solve the complex mutual exclusion problem.
  • The core idea is to set a counter count  to record the number of reading processes currently accessing the shared file. We can use the value of count to determine whether the currently entered process is the first/last read process, so as to make different processing.
  • In addition, the check and assignment of the count variable cannot be done in one go, which leads to some errors. If you need to achieve "one go", you should naturally think of using a mutex semaphore .
  • Finally, we must also seriously understand how we solve the problem of "write process starvation"

2. The Philosophers Dining Problem 

There are five philosophers sitting on a round table, a chopstick is placed on the table between each two philosophers, and a bowl of rice is in the middle of the table. Philosophers devote their whole lives to thinking and eating. Philosophers think without affecting others. Only when the philosopher is hungry does he try to pick up the left and right chopsticks (one by one). If the chopsticks are already in someone else's hands, wait. A hungry philosopher can start eating only by picking up two chopsticks at the same time. When the meal is finished, he puts down the chopsticks and continues to think.

insert image description here 

 

1. Relationship analysis . There are 5 philosopher processes in the system, and the access of the 5 philosophers and the left and right neighbors to the middle chopsticks is mutually exclusive.

2. Organize your thoughts . There is only mutual exclusion in this problem, but unlike the problems encountered before, each philosopher process needs to hold two critical resources at the same time to start eating. How to avoid the deadlock phenomenon caused by improper allocation of critical resources is the essence of the philosopher's problem.

3. Semaphore settings. An array of mutually exclusive semaphores is defined chopstick[5]={1,1,1,1,1} to implement mutually exclusive access to 5 chopsticks.  And number the philosophers , the chopsticks on the left of the  philosopher are 0~4numbered , and  the chopsticks on the right are numbered  .ii(i+1)%5

 

 

insert image description here 

 The above situation is unreasonable and will cause deadlock problems , so how should we prevent deadlocks from happening?

accomplish

 Option One

insert image description here

 ① Some restrictions can be imposed on the philosopher process, such as allowing up to four philosophers to eat at the same time. This ensures that at least one philosopher can get the left and right chopsticks.

 

 Option II

② The odd-numbered philosophers were asked to take the chopsticks on the left first, and then the chopsticks on the right, while the even-numbered philosophers were just the opposite. In this way, it is guaranteed that if two adjacent parity philosophers both want to eat, only one of them can pick up the first chopstick, and the other will directly block. This avoids the situation of having one and then waiting for the other.

 third solution

③ A philosopher is only allowed to pick up chopsticks when both left and right chopsticks are available.

Case 1: Scientist 0 eats; other scientists block before the 0th scientist finishes eating. Only after the 0th scientist has finished eating, other scientists can eat.

insert image description here

Scenario 2: The 0th scientist eats; before the 0th scientist finishes eating, other scientists' processes are blocked. Only after the 0th scientist has finished eating, other scientists can eat. However, if the 1st scientist to the right of the 0th scientist eats, it will be blocked, and even if the 2nd scientist has chopsticks on the left and right sides, it is temporarily unavailable.
insert image description here 

Scenario 3: The 0th scientist eats; before the 0th scientist finishes eating, other scientists' processes are blocked. Only after the 0th scientist has finished eating, other scientists can eat. But if the 4th scientist to the left of the 0th scientist eats, the blockage will occur. At this time, the chopsticks on the right of the 4th scientist are unavailable, but the 4th will still pick up the left chopsticks first.

insert image description here 

 

  • The crux of the Philosopher's meal problem is to resolve process deadlocks.
  • There is only a mutual exclusion relationship between these processes, but different from the mutual exclusion relationship that has been touched before, each process needs to hold two critical resources at the same time, so there is a potential "deadlock" problem.
  • If you encounter a situation in which a process needs to hold multiple critical resources at the same time, you should refer to the idea of ​​​​the philosopher's problem and analyze whether there will be circular waiting between the processes given in the question and whether deadlock will occur. You can refer to the three ideas of the philosopher's dining problem to solve the deadlock.

 

3. Monitor 

1. Why introduce a monitor?

Definition and basic characteristics of monitor

insert image description here

2. Definition and basic characteristics of monitor

  • A monitor is a special software module, which consists of these parts:
    1. The description of the shared data structure local to the monitor; 2. A set of procedures
    for operating the data structure ; 3. The shared data local to the monitor The statement to set the initial value; ④, the monitor has a name.

  • The basic features of the monitor:
    1. The data local to the monitor can only be accessed by the process local to the monitor;
    2. A process can access the shared data in the monitor only by calling the procedure in the monitor;
    3. Each time Only one process is allowed to execute an internal procedure within the monitor.

3. Extension 1: Use monitor to solve the problem of producer and consumer 

 

  •  The purpose of introducing the monitor is nothing more than to achieve mutual exclusion and synchronization of processes more conveniently.
  • 1. Shared data needs to be defined in the monitor (such as the buffer for producer-consumer problems)
    2. The "entry" for accessing these shared data needs to be defined in the monitor - in fact, some functions (such as producer consumption) In other problems, you can define a function to put the product into the buffer, and define a function to take the product from the buffer)

    ③. Only through these specific "entry points" can the shared data be accessed

    4. There are many "entries" in the monitor, but only one of them can be opened at a time , and only one process or thread can enter (for example, in the producer-consumer problem, each process needs to access the shared buffer mutually exclusively This feature of the monitor ensures that at most one process will access the buffer within a period of time.  Note: This mutual exclusion feature is implemented by the compiler, and the programmer does not need to care  )

    ⑤. Condition variables and wait/wake operations can be set in the monitor process to solve synchronization problems. A process or thread can be made to wait on a condition variable (  at this time, the process should release the right to use the monitor first, that is, to give up the "entry"  ); the process or thread waiting on the condition variable can be woken up by a wake-up operation .

  • Programmers can define a monitor with a special syntax (for example: monitor ProducerConsumer ... end monitor;), and then other programmers can use the specific "entry" provided by this monitor to easily achieve process synchronization/interaction reprimanded. The idea of ​​"encapsulation"

4. Extension 2: a mechanism similar to a monitor in Java 

In Java, if the keyword  synchronized  is used to describe a function, then this function can only be called by one thread in the same time period

insert image description here 

 insert image description here

 

Guess you like

Origin blog.csdn.net/Javascript_tsj/article/details/124368957
Recommended