From 4fa8a6b8d63306c6435401fc40a30b466f8c1d04 Mon Sep 17 00:00:00 2001 From: Bandi Yugandhar Date: Wed, 5 Sep 2018 21:50:08 +0530 Subject: [PATCH 1/2] =?UTF-8?q?Update=20and=20rename=20notes/=E8=AE=A1?= =?UTF-8?q?=E7=AE=97=E6=9C=BA=E6=93=8D=E4=BD=9C=E7=B3=BB=E7=BB=9F.md=20to?= =?UTF-8?q?=20=20notes/=E8=AE=A1=E7=AE=97=E6=9C=BA=E6=93=8D=E4=BD=9C?= =?UTF-8?q?=E7=B3=BB=E7=BB=9F.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- notes/计算机操作系统.md | 112 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 112 insertions(+) diff --git a/notes/计算机操作系统.md b/notes/计算机操作系统.md index 4125d86d..8751d4f8 100644 --- a/notes/计算机操作系统.md +++ b/notes/计算机操作系统.md @@ -456,6 +456,118 @@ void writer() { up(&data_mutex); } } +``` +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 +int readcount, writecount; //(initial value = 0) +semaphore rmutex, wmutex, readTry, resource; //(initial value = 1) + +//READER +reader() { + + down(&readLock); // reader is trying to enter + down(&rmutex); // lock to increase readcount + readcount++; + if (readcount == 1) + down(&resource); //if you are the first reader then lock the resource + up(&rmutex); //release for other readers + up(&readLock); //Done with trying to access the resource + + +//reading is performed + + + down(&rmutex); //reserve exit section - avoids race condition with readers + 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 +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 + down(&readLock); //if you're first, then you must lock the readers out. Prevent them from trying to enter CS + up(&wmutex); //release entry section + + + down(&resource); //reserve the resource for yourself - prevents other writers from simultaneously editing the shared resource + //writing is performed + up(&resource); //release file + + + down(&wmutex); //reserve exit section + writecount--; //indicate you're leaving + if (writecount == 0) //checks if you're the last 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. + + +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. + +int readCount; // init to 0; number of readers currently accessing resource + +// all semaphores initialised to 1 +Semaphore resourceAccess; // controls access (read/write) to the resource +Semaphore readCountAccess; // for syncing changes to shared variable readCount +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 + // + 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 + down(&readCountAccess); // request exclusive access to readCount + // + if (readCount == 0) // if there are no readers already reading: + down(&resourceAccess); // request resource access for readers (writers blocked) + readCount++; // update count of active readers + // + 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 + if (readCount == 0) // if there are no readers left: + up(&resourceAccess); // release resource access for all + // + up(&readCountAccess); // release access to readCount +} + + + ``` ### 2. 哲学家进餐问题 From 4ee1497ade4f9d0fb58d2e5fed0417919a13c158 Mon Sep 17 00:00:00 2001 From: Bandi Yugandhar Date: Wed, 5 Sep 2018 22:22:22 +0530 Subject: [PATCH 2/2] =?UTF-8?q?Update=20=E8=AE=A1=E7=AE=97=E6=9C=BA?= =?UTF-8?q?=E6=93=8D=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 --- notes/计算机操作系统.md | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/notes/计算机操作系统.md b/notes/计算机操作系统.md index 8751d4f8..a25daa6f 100644 --- a/notes/计算机操作系统.md +++ b/notes/计算机操作系统.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 //