大神作家用什么软件码字_什么是读者作家问题?

大神作家用什么软件码字

Readers writer problem is another example of a classic synchronization problem. There are many variants of this problem, one of which is examined below.

读者作家问题是经典同步问题的另一个示例。 此问题有很多变体,下面将介绍其中一种。

问题陈述 (The Problem Statement)

There is a shared resource which should be accessed by multiple processes. There are two types of processes in this context. They are reader and writer. Any number of readers can read from the shared resource simultaneously, but only one writer can write to the shared resource. When a writer is writing data to the resource, no other process can access the resource. A writer cannot write to the resource if there are non zero number of readers accessing the resource at that time.

有一个共享资源,应由多个进程访问。 在这种情况下,有两种类型的过程。 他们是读者作家 。 可以同时从共享资源读取任何数量的读取器 ,但是只有一个写入器可以写入共享资源。 当一个作家写数据到资源,没有其他进程可以访问该资源。 如果当时访问资源的读者人数不为零,则编写者无法写入资源。

解决方案 (The Solution)

From the above problem statement, it is evident that readers have higher priority than writer. If a writer wants to write to the resource, it must wait until there are no readers currently accessing that resource.

从以上问题陈述中可以明显看出,读者的优先级高于作家。 如果写者想写资源,它必须等到当前没有读者访问该资源。

Here, we use one mutex m and a semaphore w. An integer variable read_count is used to maintain the number of readers currently accessing the resource. The variable read_count is initialized to 0. A value of 1 is given initially to m and w.

在这里,我们使用一个互斥锁 m和一个信号量 w 。 整数变量read_count用于维护当前正在访问资源的读取器的数量。 变量read_count初始化为0 。 最初给mw赋值为1

Instead of having the process to acquire lock on the shared resource, we use the mutex m to make the process to acquire and release lock whenever it is updating the read_count variable.

我们不用互斥锁m来使进程在更新read_count变量时获取并释放锁,而不是让进程获取对共享资源的锁。

The code for the writer process looks like this:

作家来说,过程的代码如下所示:

while(TRUE) 
{
    wait(w);
    
   /* perform the write operation */
   
   signal(w);
}

And, the code for the reader process looks like this:

并且,用于读取器过程的代码如下所示:

while(TRUE) 
{
    //acquire lock
    wait(m);
    read_count++;
    if(read_count == 1)
        wait(w);
    
    //release lock  
    signal(m);  
    
    /* perform the reading operation */
    
    // acquire lock
    wait(m);   
    read_count--;
    if(read_count == 0)
        signal(w);
        
    // release lock
    signal(m);  
}

这是未编码的代码(解释) (Here is the Code uncoded(explained))

  • As seen above in the code for the writer, the writer just waits on the w semaphore until it gets a chance to write to the resource.

    如上面在编写者代码中所见,编写者只是等待w信号量,直到它有机会写入资源为止。

  • After performing the write operation, it increments w so that the next writer can access the resource.

    执行写操作后,它将增加w,以便下一个写程序可以访问该资源。

  • On the other hand, in the code for the reader, the lock is acquired whenever the read_count is updated by a process.

    另一方面,在用于读取器的代码中,只要通过进程更新了read_count ,便会获取锁定。

  • When a reader wants to access the resource, first it increments the read_count value, then accesses the resource and then decrements the read_count value.

    当读者想要访问资源时,首先它会增加read_count值,然后访问资源,然后再减小read_count值。

  • The semaphore w is used by the first reader which enters the critical section and the last reader which exits the critical section.

    信号量w由进入临界区的第一个阅读器和离开临界区的最后一个阅读器使用。

  • The reason for this is, when the first readers enters the critical section, the writer is blocked from the resource. Only new readers can access the resource now.

    这样做的原因是,当第一个读者进入关键部分时,作者被阻止访问资源。 现在只有新读者才能访问该资源。

  • Similarly, when the last reader exits the critical section, it signals the writer using the w semaphore because there are zero readers now and a writer can have the chance to access the resource.

    同样,当最后一个读取器退出关键部分时,它会使用w信号通知写入器,因为现在的读取器为零,写入器可以访问资源。

翻译自: https://www.studytonight.com/operating-system/readers-writer-problem

大神作家用什么软件码字

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值