Created
September 28, 2016 02:40
-
-
Save fcpauldiaz/e161a720d4ffa551046d2d2ea7d8f976 to your computer and use it in GitHub Desktop.
Writter/Read Nachos pseudo code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ConditionVariable OkToRead | |
ConditionVariable OkToWrite | |
Read () { | |
//check if there are writers active or waiting | |
Lock acquire() | |
while (WW + AW > 0) { | |
WR++ //contamos como un lector | |
OkToRead.wait() //sleep() | |
WR-- | |
} | |
AR ++; //active reader | |
Lock Release(); | |
//al salir del bloque se lee de la database; | |
DB Read(); | |
Lock Aquire(); //avisar que se va a modificar las variables de estado | |
AR --; | |
//cuando no hay active writers | |
while (AC == 0 && WW > 0) { | |
//no deben de haber AW; | |
//sacar el wating writer; | |
OkToWrite.Notify(); //despierta al siguiente thread, solo puede haber uno. | |
} | |
Lock Release; | |
} | |
} | |
Write() { | |
Lock Aquire; | |
while (AR + AW > 0) { | |
WW++ //somos un writer que esta esperando | |
OkToWrite wait() | |
WW -- //ya no estamos esperando | |
} | |
AW++ | |
Lock Release //context switching | |
DB Write() | |
Lock Aquire() | |
AW-- | |
if (WW > 0) { | |
OkToWrite Notify() | |
} | |
else if (WR > 0) { | |
OkToRead NotifyAll() | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment