From 4ee1497ade4f9d0fb58d2e5fed0417919a13c158 Mon Sep 17 00:00:00 2001 From: Bandi Yugandhar Date: Wed, 5 Sep 2018 22:22:22 +0530 Subject: [PATCH] =?UTF-8?q?Update=20=E8=AE=A1=E7=AE=97=E6=9C=BA=E6=93=8D?= =?UTF-8?q?=E4=BD=9C=E7=B3=BB=E7=BB=9F.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...215\344\275\234\347\263\273\347\273\237.md" | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) 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 8751d4f8..a25daa6f 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" @@ -464,10 +464,10 @@ The first case may result Writer to starve. This case favous Writers i.e no writ ```c int readcount, writecount; //(initial value = 0) -semaphore rmutex, wmutex, readTry, resource; //(initial value = 1) +semaphore rmutex, wmutex, readLock, resource; //(initial value = 1) //READER -reader() { +void reader() { down(&readLock); // reader is trying to enter down(&rmutex); // lock to increase readcount @@ -482,7 +482,7 @@ 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 @@ -490,8 +490,8 @@ reader() { //WRITER -writer() { - +void writer() { + down(&wmutex); //reserve entry section for writers - avoids race conditions writecount++; //report yourself as a writer entering if (writecount == 1) //checks if you're first writer @@ -510,12 +510,14 @@ writer() { up(&readLock); //if you're last writer, you must unlock the readers. Allows them to try enter CS for reading up(&wmutex); //release exit section } +``` -We can observe that every reader is forced to acquire ReadTry lock. On the otherhand, writers doesn’t need to lock individually. Once the first writer locks the ReadTry lock, it will be released only when there is writer left in the queue. +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 int readCount; // init to 0; number of readers currently accessing resource // all semaphores initialised to 1 @@ -524,7 +526,7 @@ Semaphore readCountAccess; // for syncing changes to shared variable readCo Semaphore serviceQueue; // FAIRNESS: preserves ordering of requests (signaling must be FIFO) void writer() -{ +{ down(&serviceQueue); // wait in line to be servicexs // down(&resourceAccess); // request exclusive access to resource @@ -542,7 +544,7 @@ void writer() void reader() -{ +{ down(&serviceQueue); // wait in line to be serviced down(&readCountAccess); // request exclusive access to readCount // -- GitLab