diff --git "a/notes/\350\256\241\347\256\227\346\234\272\346\223\215\344\275\234\347\263\273\347\273\237.md" "b/notes/\350\256\241\347\256\227\346\234\272\346\223\215\344\275\234\347\263\273\347\273\237.md" index 5eee94b4f69e632fc69d3c80c4c0fb7ffe96038d..e6c462a6d01aa460c81d26fd55c475b463608bb1 100644 --- "a/notes/\350\256\241\347\256\227\346\234\272\346\223\215\344\275\234\347\263\273\347\273\237.md" +++ "b/notes/\350\256\241\347\256\227\346\234\272\346\223\215\344\275\234\347\263\273\347\273\237.md" @@ -457,12 +457,12 @@ void writer() { } } ``` -The first case may result Writer to starve. This case favous Writers i.e no writer, once added to the queue, shall be kept waiting longer than absolutely necessary(only when there are readers that entered the queue before the writer). - +以下内容由 [@Bandi Yugandhar](https://github.com/yugandharbandi) 提供。 +The first case may result Writer to starve. This case favous Writers i.e no writer, once added to the queue, shall be kept waiting longer than absolutely necessary(only when there are readers that entered the queue before the writer). -```c +```source-c int readcount, writecount; //(initial value = 0) semaphore rmutex, wmutex, readLock, resource; //(initial value = 1) @@ -482,13 +482,12 @@ void reader() { down(&rmutex); //reserve exit section - avoids race condition with readers - readcount--; //indicate you're leaving + readcount--; //indicate you're leaving if (readcount == 0) //checks if you are last reader leaving up(&resource); //if last, you must release the locked resource up(&rmutex); //release exit section for other readers } - //WRITER void writer() { @@ -514,10 +513,9 @@ void writer() { We can observe that every reader is forced to acquire ReadLock. On the otherhand, writers doesn’t need to lock individually. Once the first writer locks the ReadLock, it will be released only when there is no writer left in the queue. - From the both cases we observed that either reader or writer has to starve. Below solutionadds the constraint that no 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. -```c +```source-c int readCount; // init to 0; number of readers currently accessing resource // all semaphores initialised to 1 @@ -532,17 +530,16 @@ void writer() down(&resourceAccess); // request exclusive access to resource // up(&serviceQueue); // let next in line be serviced - + // writeResource(); // writing is performed // - + // up(&resourceAccess); // release resource access for next reader/writer // } - void reader() { down(&serviceQueue); // wait in line to be serviced @@ -554,11 +551,11 @@ void reader() // up(&serviceQueue); // let next in line be serviced up(&readCountAccess); // release access to readCount - + // readResource(); // reading is performed // - + down(&readCountAccess); // request exclusive access to readCount // readCount--; // update count of active readers @@ -568,10 +565,9 @@ void reader() up(&readCountAccess); // release access to readCount } - - ``` + ### 2. 哲学家进餐问题