ALL > Computer and Education > courses > university courses > undergraduate courses > Operating System > ZSTU-(2020-2021)-1 > student homework > >
2017329621131_王小滔_homework5 Version 0
👤 Author: by 2031115154qqcom 2021-01-03 08:02:58
Assume that there is an existing ticket booking system that can satisfy multiple people querying the remaining ticket information at the same time, but when a user submits an order and enters the booking process, if other users query the remaining ticket data at this time, the readout may be dirty data, and If other users book tickets at the same time, it may cause the problem of incorrect data coverage. For example, there are currently 16 tickets left, a certain ticket booking process A enters the query first, the query result is 16, and the result is stored in a variable; another process B has a faster operation efficiency, and the ticket booking task is completed before the time slice is completed , Changed the number of remaining votes to 15; now switch back to process A, because the result of the previous query is 16, so after the operation, the number of remaining votes is written as 15, and the correct number of remaining votes should be 14, which leads to Inconsistent data. To this end, we need to improve the booking system to satisfy: 1. Multiple reading processes can access shared data at the same time; 2. The access to shared data by the writing process should be completed/abandoned by the reading process and other writing processes After the access; 3. When a writing process accesses shared data, other writing processes and reading processes cannot access the shared data until the current writing process ends access to the shared data.
Solution:
The relationship between the writing process and the writing process, and between the writing process and the reading process is clear. We can consider setting a mutually exclusive semaphore mutex, and perform P operation on it before any writing process writes data to the file , But the reading process cannot perform P operation on mutex before reading data, otherwise, other reading processes will be stuck and cannot enter if they want to read data. It is conceivable that there is an alliance between the reading processes. We can consider setting a semaphore for it to count the number of reading processes that are reading data from the shared file, and let the first read process that enters and the last read that leaves The process does some judgment and guarantee work.
semaphore mutex=1;//Used to implement mutually exclusive access to files
int num=0;//Record that several read processes are currently accessing files
Write process ()
{
while(1)
{
P(mutex);
Write data;
V(mutex);
}
}
Read process()
{
while(1)
{
if(num==0)
P(mutex);
num++;
Read data;
num--;
if(num==0)
V(mutex);
}
}
It looks simple, but it has big flaws. Suppose that the reading process p1 judges if (num==0) and passes. It knows that it is the first reading process to access the shared file, but it has not yet performed P operation on mutex; at this time, the reading process P2 is switched to the processor , Suppose P2 is executed to P(mutex) and passed; now it is switched to P1, P1 cannot pass the detection, and is hung in the waiting queue, which does not implement multiple read processes to access the shared file. The reason is that the operation of num is interrupted in the middle, and it is not done in one go. We can set a mutually exclusive semaphore for operations on the variable num:
semaphore mutex=1;//Used to implement mutually exclusive access to files
int num=0;//Record that several read processes are currently accessing files
semaphore mutex2=1;//Used to ensure exclusive access to the num variable
Write process ()
{
while(1)
{
P(mutex);
Write data;
V(mutex);
}
}
Read process()
{
while(1)
{
P(mutex2);
if(num==0)
P(mutex);
num++;
V(mutex2);
Read data;
P(mutex2);
num--;
if(num==0)
V(mutex);
V(mutex2);
}
}
In the above method, the reading process is the priority, which may cause the writing process to "starve to death".
Improvement (literacy fairness law):
semaphore mutex=1;//Used to implement mutually exclusive access to files
int num=0;//Record that several read processes are currently accessing files
semaphore mutex2=1;//Used to ensure exclusive access to the num variable
semaphore w=1;//Used to achieve "write priority"
Write process ()
{
while(1)
{
P(w);
P(mutex);
Write data;
V(mutex);
V(w);
}
}
Read process()
{
while(1)
{
P(w);

P(mutex2);
if(num==0)
P(mutex);
num++;
V(mutex2);

V(w);

Read data;
P(mutex2);
num--;
if(num==0)
V(mutex);
V(mutex2);
}
}

Please login to reply. Login

Reversion History

Loading...
No reversions found.