毛雷溥浩 homework5 Version 0 |
|
👤 Author: by 2972850556qqcom 2021-12-15 02:17:34 |
A database could be shared by mutithreads. some threads are used for only reading while others are functioning upgrasion.The former user is calledreader nd the later iscalled the writer f witing and other users(neither writer nor reader) concurrently share the object,it might be confused. At thesetime, to ensure the such kind of hard cases happen, writers are required to exclude the other who access the shared databases.
typedef struct {
int value;
struct process *list;
} semaphore;
semaphore mutex, wrt;int readcount;
do {
wait(wrt);
// Writing is performedsignal(wrt);
} while(TRUE);
For reader:do {
wait(mutex);readcount++;if (readcount==1)wait(wrt);
signal(mutex);
l/ reading is performedwait(mutex);
readcount--;
if (readcount == 0)signal(wrt);
signal(mutex);} while(TRUE;