📜  读者作家问题

📅  最后修改于: 2020-12-15 09:33:17             🧑  作者: Mango

读者写作问题

读者-作家问题是过程同步的经典问题,它涉及一次在多个过程之间共享的数据集,例如文件。在这些各种过程中,有一些是读取器-只能读取数据集。它们不执行任何更新,有些是Writer-可以读取和写入数据集。

读写器问题用于管理各种读写器过程之间的同步,因此数据集没有问题,即不会产生不一致。

让我们以一个例子来理解-如果两个或两个以上的读者想要在同一时间访问文件,那将没有问题。但是,在其他情况下,例如当两个作者或一个读者和一个作者想要在同一时间访问文件时,可能会出现一些问题,因此任务是设计代码,如果一个读者读取,则不允许在同一时间更新任何写入器,类似地,如果一个写入器正在写入,则不允许任何读取器在该时间点读取文件,并且如果一个写入器正在更新文件,则不应允许其他写入器进行更新。在同一时间更新文件。但是,多个阅读器可以同时访问该对象。

让我们用下面的表格了解读写的可能性:

表格1

Case Process 1 Process 2 Allowed / Not Allowed
Case 1 Writing Writing Not Allowed
Case 2 Reading Writing Not Allowed
Case 3 Writing Reading Not Allowed
Case 4 Reading Reading Allowed

读者和作家的解决方案可以使用二进制信号量来实现。

我们使用两个二进制信号量“ write”和“ mutex”,其中二进制信号量可以定义为:

信号量:信号量是S中的一个整数变量,除初始化外,信号量仅可通过两个标准原子操作-wait和signal访问,其定义如下:

1. wait( S )
{
while( S <= 0) ;
S--;
}

2. signal( S )
{
S++;
}

从以上wait的定义中可以明显看出,如果S <= 0,则它将进入无限循环(由于分号;在while循环之后)。信号的工作是增加S的值。

下面的代码将提供读写器问题的解决方案,读写器过程代码如下:

读者流程代码

阅读器过程的代码如下:

static int readcount = 0;
wait (mutex);
readcount ++; // on each entry of reader increment readcount
if (readcount == 1)
{
  wait (write);
}
signal(mutex);

--READ THE FILE?

wait(mutex);
readcount --; // on every exit of reader decrement readcount
if (readcount == 0)
{
  signal (write);
}
signal(mutex);

在上面的阅读器代码中,互斥量和是具有初始值1的信号量,而readcount变量的初始值是0。互斥体在读取器和编写器过程代码中都是常见的,信号量互斥量可确保相互排斥和信号量写处理写机制。

readcount变量表示同时访问文件的读取器的数量。当变量readcount变为1时,将使用等待操作来写入信号量,从而将值减小1。这意味着不再允许编写者如何访问文件。读操作完成后, readcount递减一。当readcount变为0时,用于操作的信号操作允许写程序访问文件。

编写程序代码

下面给出了定义编写器过程的代码:

wait(write);
WRITE INTO THE FILE
signal(wrt);

如果一个作家希望访问该文件,等待操作是在信号,其减为0,没有其他作家可以访问该文件执行。访问文件的作者完成写作业后,对write进行信号操作。

让我们看看表1中提到的每种情况的证明

情况1:写作-写作→不允许。那就是当两个或两个以上的进程愿意编写时,则不允许这样做。让我们看看我们的代码是否在相应地工作?

Explanation :
The initial value of semaphore write = 1
Suppose two processes P0 and P1 wants to write, let P0 enter first the writer code, The moment P0 enters
 Wait( write ); will decrease semaphore write by one, now write = 0
And continue WRITE INTO THE FILE
Now suppose P1 wants to write at the same time (will it be allowed?) let's see.
P1 does Wait( write ), since the write value is already 0, therefore from the definition of wait, it will go into an infinite loop (i.e. Trap), hence P1 can never write anything, till P0 is writing.
Now suppose P0 has finished the task, it will
signal( write); will increase semaphore write by 1, now write = 1
if now P1 wants to write it since semaphore write > 0
This proofs that, if one process is writing, no other process is allowed to write.

情况2:阅读-写作→不允许。那就是当一个或多个进程正在读取文件时,则不允许另一进程进行写入。让我们看看我们的代码是否在相应地工作?

Explanation:
Initial value of semaphore mutex = 1 and variable readcount = 0
Suppose two processes P0 and P1 are in a system, P0 wants to read while P1 wants to write, P0 enter first into the reader code, the moment P0 enters 
Wait( mutex ); will decrease semaphore mutex by 1, now mutex = 0
Increment readcount by 1, now readcount = 1, next
if (readcount == 1)// evaluates to TRUE
{
  wait (write); // decrement write by 1, i.e. write = 0(which   
                   clearly proves that if one or more than one 
                   reader is reading then no writer will be  
                   allowed.
}

signal(mutex); // will increase semaphore mutex by 1, now mutex = 1 i.e. other readers are allowed to enter.

And reader continues to --READ THE FILE?

Suppose now any writer wants to enter into its code then:

As the first reader has executed wait (write); because of which write value is 0, therefore wait(writer); of the writer, code will go into an infinite loop and no writer will be allowed.
This proofs that, if one process is reading, no other process is allowed to write.
Now suppose P0 wants to stop the reading and wanted to exit then
Following sequence of instructions will take place:
wait(mutex); // decrease mutex by 1, i.e. mutex = 0
readcount --; // readcount = 0, i.e. no one is currently reading
if (readcount == 0) // evaluates TRUE
{
  signal (write); // increase write by one, i.e. write = 1 
}
signal(mutex);// increase mutex by one, i.e. mutex = 1

Now if again any writer wants to write, it can do it now, since write > 0

案例3:写作-阅读→不允许。那就是当一个进程正在写入文件时,则不允许另一进程读取文件。让我们看看我们的代码是否在相应地工作?

Explanation:
The initial value of semaphore write = 1
Suppose two processes P0 and P1 are in a system, P0 wants to write while P1 wants to read, P0 enter first into the writer code, The moment P0 enters 
Wait( write ); will decrease semaphore write by 1, now write = 0
And continue WRITE INTO THE FILE
Now suppose P1 wants to read the same time (will it be allowed?) let's see.
P1 enters reader's code
Initial value of semaphore mutex = 1 and variable readcount = 0
Wait( mutex ); will decrease semaphore mutex by 1, now mutex = 0
Increment readcount by 1, now readcount = 1, next
if (readcount == 1)// evaluates to TRUE
{
  wait (write); // since value of write is already 0, hence it 
                   will enter into an infinite loop and will not be  
                   allowed to proceed further (which clearly 
                   proves that if one writer is writing then no  
                   reader will be allowed.
}

The moment writer stops writing and willing to exit then 
This proofs that, if one process is writing, no other process is allowed to read.

The moment writer stops writing and willing to exit then it will execute:
signal( write); will increase semaphore write by 1, now write = 1
if now P1 wants to read it can since semaphore write > 0 

情况4:阅读-阅读→允许。那就是当一个进程正在读取文件,而另一个进程或多个进程愿意读取时,则全部允许,即读取-读取不是互斥的。让我们看看我们的代码是否在相应地工作?

Explanation :
Initial value of semaphore mutex = 1 and variable readcount = 0
Suppose three processes P0, P1 and P2 are in a system, all the three processes P0, P1, and P2 want to read, let P0 enter first into the reader code, the moment P0 enters 
Wait( mutex ); will decrease semaphore mutex by 1, now mutex = 0
Increment readcount by 1, now readcount = 1, next
if (readcount == 1)// evaluates to TRUE
{
  wait (write); // decrement write by 1, i.e. write = 0(which   
                   clearly proves that if one or more than one 
                   reader is reading then no writer will be  
                   allowed.
}

signal(mutex); // will increase semaphore mutex by 1, now mutex = 1 i.e. other readers are allowed to enter.

And P0 continues to --READ THE FILE?

→Now P1 wants to enter the reader code
current value of semaphore mutex = 1 and variable readcount = 1
let P1 enter into the reader code, the moment P1 enters 
Wait( mutex ); will decrease semaphore mutex by 1, now mutex = 0
Increment readcount by 1, now readcount = 2, next
if (readcount == 1)// eval. to False, it will not enter if block

signal(mutex); // will increase semaphore mutex by 1, now mutex = 1 i.e. other readers are allowed to enter.

Now P0 and P1 continues to --READ THE FILE?

→Now P2 wants to enter the reader code
current value of semaphore mutex = 1 and variable readcount = 2
let P2 enter into the reader code, The moment P2 enters 
Wait( mutex ); will decrease semaphore mutex by 1, now mutex = 0
Increment readcount by 1, now readcount = 3, next
if (readcount == 1)// eval. to False, it will not enter if block

signal(mutex); // will increase semaphore mutex by 1, now mutex = 1 i.e. other readers are allowed to enter.

Now P0, P1, and P2 continues to --READ THE FILE?


Suppose now any writer wants to enter into its code then:

As the first reader P0 has executed wait (write); because of which write value is 0, therefore wait(writer); of the writer, code will go into an infinite loop and no writer will be allowed.  

Now suppose P0 wants to come out of system( stop reading) then
wait(mutex); //will decrease semaphore mutex by 1, now mutex = 0
readcount --; // on every exit of reader decrement readcount by 
                 one i.e. readcount = 2

if (readcount == 0)// eval. to FALSE it will not enter if block

signal(mutex); // will increase semaphore mutex by 1, now mutex = 1 i.e. other readers are allowed to exit


→ Now suppose P1 wants to come out of system (stop reading) then
wait(mutex); //will decrease semaphore mutex by 1, now mutex = 0
readcount --; // on every exit of reader decrement readcount by 
                 one i.e. readcount = 1

if (readcount == 0)// eval. to FALSE it will not enter if block

signal(mutex); // will increase semaphore mutex by 1, now mutex = 1 i.e. other readers are allowed to exit

→Now suppose P2 (last process) wants to come out of system (stop reading) then
wait(mutex); //will decrease semaphore mutex by 1, now mutex = 0
readcount --; // on every exit of reader decrement readcount by 
                 one i.e. readcount = 0

if (readcount == 0)// eval. to TRUE it will enter into if block
{
  signal (write); // will increment semaphore write by one, i.e. 
                     now write = 1, since P2 was the last process
                     which was reading, since now it is going out, 
                     so by making write = 1 it is allowing the writer    
                     to write now.
}

signal(mutex); // will increase semaphore mutex by 1, now mutex = 1 


The above explanation proves that if one or more than one processes are willing to read simultaneously then they are allowed.