I have the following exercise:
The given program snippet contains two functions mywrite() and myread(), which are in the same Write file or read from file. These two functions are used by a lot of threads called again and again. It should be ensured that at the same time either only read or can be written. Only one writing thread is permitted at a time will. Multiple reading threads are allowed at the same time. Complete the given program using only the given ones for your solution binary semaphores S and T, and the integer variable c. Your solution does not have to be fair be. For the initialization of a semaphore you can e.g. e.g. use S:=0, this means that the semaphore is busy. Use the functions introduced in the lecture: S.acquire() or S.release().
and the following solution:
Semaphor S := 1;
Semaphor T := 1;
int c = 0;
void mywrite()
{
T.acquire();
write_data_to_file();
T.release();
}
void myread()
{
S.acquire();
if (c == 0)
{
T.acquire()
}
c ;
S.release();
read_data_from_file();
S.acquire();
c--;
if (c == 0)
{
T.release()
}
S.release();
}
I do not know, why T.acquire() comes after S.acquire() in the myread() function. the counter increments, but why? the mywrite() function never checks the value of c.
CodePudding user response:
S
provides mutually exclusive access to c
.
Each writer acquires and releases T
.
T
is also acquired by a reader when there are no other active readers. It is released when there are no more active readers.
This ensures:
- Would-be writers wait when there's an active writer.
- Would-be readers wait when there's an active writer.
- Would-be writers wait when there's one or more active readers.
c
is the count of active readers. A reader uses c
to determine if there are other active readers. In other words, a reader uses c
to determine if it needs to acquire T
, and if it should release T
.
You can't have multiple threads accessing c
simultaneously, so some form of mutual exclusion is needed around access of c
. S
is used to provide that functionality. S
isn't acquired before T
is acquired so much as S
is acquired before c
is accessed.