readers-writers problems
来源:互联网 发布:php管理上传文件 编辑:程序博客网 时间:2024/05/21 14:55
In computer science, the first and second readers-writers problems are examples of a common computing problem inconcurrency. The two problems deal with situations in which many threads must access the same shared memory at one time, some reading and some writing, with the natural constraint that no process may access the share for reading or writing while another process is in the act of writing to it. (In particular, itis allowed for two or more readers to access the share at the same time.) Areaders-writer lock is a data structure that solves one or more of the readers-writers problems.
Contents
[hide]- 1The first readers-writers problem
- 2The second readers-writers problem
- 3The third readers-writers problem
- 4See also
- 5References
- 6External links
The first readers-writers problem[edit]
Suppose we have a shared memory area with the constraints detailed above. It is possible to protect the shared data behind a mutual exclusionmutex, in which case no two threads can access the data at the same time. However, this solution is suboptimal, because it is possible that a readerR1 might have the lock, and then another reader R2 requests access. It would be foolish forR2 to wait until R1 was done before starting its own read operation; instead,R2 should start right away. This is the motivation for the first readers-writers problem, in which the constraint is added that no reader shall be kept waiting if the share is currently opened for reading. This is also calledreaders-preference, with its solution below:
semaphore wrt=1, mutex=1;readcount=0; writer(){ wait(wrt); // Writing is done signal(wrt);} reader(){ wait(mutex); readcount++; if (readcount == 1) wait(wrt); signal(mutex); // Do the Reading // (Critical Section Area) wait(mutex); readcount--; if (readcount == 0) signal(wrt); signal(mutex);}
The second readers-writers problem[edit]
Suppose we have a shared memory area protected by a mutex, as above. This solution is suboptimal, because it is possible that a readerR1 might have the lock, a writer W be waiting for the lock, and then a readerR2 requests access. It would be foolish for R2 to jump in immediately, ahead ofW; if that happened often enough, W would starve. Instead, W should start as soon as possible. This is the motivation for thesecond readers-writers problem, in which the constraint is added thatno writer, once added to the queue, shall be kept waiting longer than absolutely necessary. Here, P() is for wait and V() is for signal. This is also calledwriters-preference.
A solution to the writers-preference scenario is presented below:[1]
int readcount, writecount; (initial value = 0)semaphore mutex_1, mutex_2, mutex_3, w, r ; (initial value = 1) READER P(mutex_3); P(r); P(mutex_1); readcount := readcount + 1; if readcount = 1 then P(w); V(mutex_1); V(r); V(mutex_3); reading is performed P(mutex_1); readcount := readcount - 1; if readcount = 0 then V(w); V(mutex_1); WRITER P(mutex_2); writecount := writecount + 1; if writecount = 1 then P(r); V(mutex_2); P(w); writing is performed V(w); P(mutex_2); writecount := writecount - 1; if writecount = 0 then V(r); V(mutex_2);
The third readers-writers problem[edit]
In fact, the solutions implied by both problem statements result in starvation — the first readers-writers problem may starve writers in the queue, and the second readers-writers problem may starve readers. Therefore, thethird readers-writers problem is sometimes proposed, which adds the constraint thatno thread shall be allowed to starve; that is, the operation of obtaining a lock on the shared data will always terminate in a bounded amount of time.
A solution with fairness for both readers and writers might be as follows:
semaphores: no_waiting, no_accessing, counter_mutex ( initial value is 1 ) shared variables: nreaders ( initial value is 0 ) local variables: prev, current WRITER: P( no_waiting ); P( no_accessing ); V( no_waiting ); ... write ... V( no_accessing ); READER: P( no_waiting ); P( counter_mutex ); prev := nreaders; nreaders := nreaders + 1; V( counter_mutex ); if prev = 0 then P( no_accessing ); V( no_waiting ); ... read ... P( counter_mutex ); nreaders := nreaders - 1; current := nreaders; V( counter_mutex ); if current = 0 then V( no_accessing );
Note that sections protected by counter_mutex could be replaced by a suitable fetch-and-add atomic instruction, saving two potential context switches in reader's code.
Note also that this solution can only satisfy the condition that "no thread shall be allowed to starve" if and only if semaphores preserve first-in first-out ordering when blocking and releasing threads. Otherwise, a blocked writer, for example, may remain blocked indefinitely with a cycle of other writers decrementing the semaphore before it can.
See also[edit]
- Producers-consumers problem
- Dining philosophers problem
- Cigarette smokers problem
- Sleeping barber problem
- Read/write lock pattern
References[edit]
[2][3][4]
- Jump up^ Communications of the ACM :Concurrent Control with "Readers" and "Writers" P.J. Courtois,* F. H, 1971[1]
- Jump up^ Morris JM (1979) A starvation-free solution to the mutual exclusion problem. Inf Process Lett 8:76–80
- Jump up^ Fair Solution to the Reader-Writer-Problem with Semaphores only. H. Ballhausen, 2003[2]
- Jump up^ Faster Fair Solution for the Reader-Writer Problem. V.Popov, O.Mazonka 2013[3]
- readers-writers problems
- Filter Readers and Writers
- Readers/Writers lock
- c2 Streams Readers and Writers
- InputStream Readers OutputStream Writers 不同
- readers and writers problem 读者写者问题 写者优先 C++实现
- Problems
- Problems
- Problems
- Writers and Leviathan
- Principles for Technical Writers
- JavaWorld Writers Guidelines
- What Blog Readers Want
- What Blog Readers Want
- What Blog Readers Want
- What Blog Readers Want
- Readers' Choice Awards 2014
- The JSR-133 Cookbook for Compiler Writers
- Linux内存机制
- 无法找到会话标识符。启用检查 PLUSTRACE 角色
- mysql的基本操作(初学者)
- 设计一个类,该类不能被继承,且只能实例化3次
- Oracle物化视图语法
- readers-writers problems
- usb 枚举过程中的状态机
- Linux下编译mongodb以及C++客户端驱动
- 初识Objecive-C 之 多态不得不说的故事
- 最新Lync客户端特性比较
- Mat—基础图像容器(OpenCV教程)
- android创建闪屏 splash
- Linux进程地址空间与虚拟内存
- Flex 导出AIR程序图标设置