Docsity

Using Test and Set for Mutual Exclusions | CMSC 412, Study notes of Operating Systems

Toggle sidebar
bg1
1
1
1
CMSC 412
Using Test and Set for Mutual Exclusion
repeat
while test-and-set(lock);
// critical section
lock = false;
// non-critical section
until false;
lbounded waiting time version
repeat
waiting[i] = true;
key = true;
while waiting[i] and key
key = test-and-set(lock);
waiting[i] = false;
// critical section
j = (i + 1) % n
while (j != i) and (!waiting[j])
j = (j + 1) % n;
if (j == i)
lock = false;
else
waiting[j] = false;
// non-critical section
until false;
Note: no priority based on wait time
no process waiting
release process j
look for a waiting process
wait until released or no one busy
2
CMSC 412
Semaphores
lgetting critical section problem correct is difficult
โ€“harder to generalize to other synchronization problems
โ€“Alternative is semaphores
lsemaphores
โ€“integer variable
โ€“only access is through atomic operations
lP (or wait)
while s <= 0;
s = s - 1;
lV (or signal)
s = s + 1
lTwo types of Semaphores
โ€“Counting (values range from 0 to n)
โ€“Binary (values range from 0 to 1)
pf3
pf4
pf5

Partial preview of the text

Download Using Test and Set for Mutual Exclusions | CMSC 412 and more Study notes Operating Systems in PDF only on Docsity!

CMSC 412^1

Using Test and Set for Mutual Exclusion

repeat while test-and-set(lock); // critical section lock = false; // non-critical section until false; l bounded waiting time version repeat waiting[i] = true; key = true; while waiting[i] and key key = test-and-set(lock); waiting[i] = false; // critical section j = (i + 1) % n while (j != i) and (!waiting[j]) j = (j + 1) % n; if (j == i) lock = false; else waiting[j] = false; // non-critical section until false; Note: no priority based on wait time no process waiting release process j look for a waiting process wait until released or no one busy CMSC 412^2

Semaphores

l getting critical section problem correct is difficult

  • harder to generalize to other synchronization problems
  • Alternative is semaphores l semaphores
  • integer variable
  • only access is through atomic operations l P (or wait) while s <= 0; s = s - 1; l V (or signal) s = s + 1 l Two types of Semaphores
  • Counting (values range from 0 to n)
  • Binary (values range from 0 to 1)

CMSC 412^3

Using Semaphores

l critical section repeat P(mutex); // critical section V(mutex); // non-critical section until false; l Require that Process 2 begin statement S2 after Process 1 has completed statement S1: Process 2 S V(synch) Process 1 P(synch) S CMSC 412^4

Implementing semaphores

l Busy waiting implementations

l Instead of busy waiting, process can block itself

  • place process into queue associated with semaphore
  • state of process switched to waiting state
  • transfer control to CPU scheduler
  • process gets restarted when some other process executes a signal operations

CMSC 412^7

Readers have Priority

reader() { repeat P(x); readcount = readcount + 1; if readcount = 1 then P (wsem); V(x); READUNIT; P(x); readcount = readcount - 1; if readcount = 0 V(wsem); V(x); forever }; writer() { repeat P(wsem); WRITEUNIT; V(wsem) forever } CMSC 412^8

Comments on Reader Priority

l semaphores x,wsem are initialized to 1

l note that readers have priority - a writer can gain

access to the data only if there are no readers (i.e.

when readcount is zero, signal(wsem) executes)

l possibility of starvation - writers may never gain

access to data

CMSC 412^9

Writers Have Priority

reader

repeat P(z); P(rsem); P(x); readcount++; if (readcount == 1) then P(wsem); V(x); V(rsem); V(z); readunit; P(x); readcount- -; if readcount == 0 then V (wsem) V(x) forever

writer

repeat P(y); writecount++: if writecount == 1 then P(rsem); V(y); P(wsem); writeunit V(wsem); P(y); writecount--; if (writecount == 0) then V(rsem); V(y); forever; CMSC 412^10

Notes on readers/writers with writers

getting priority

P(z); P(rsem); P(x); readcount++; if (readcount==1) then P(wsem); V(x); V(rsem); V(z);

readers queue up on semaphore

z; this way only a single reader

queues on rsem. When a writer

signals rsem, only a single

reader is allowed through

Semaphores x,y,z,wsem,rsem are initialized to 1