Você está na página 1de 6

Assignment 1 Cynthea Rajam Godwin 1) Int S=1; P(S) { While(S<=0); S --; } V(S) { S++ } When P(S) is called then

it will be allowed to enter the critical section if S is greater than zero, and then it will decrement S to note its presence in the critical section. If the operation is not atomic, if the test of S and the decrement of S are not atomic, then it is possible that one process executes the test in the while loop, but is preempted just before decrementing S. Then a second process can also succeed in passing the while loop test, then decrement S and enter its critical section. Later, when the first process runs again, it also enters its critical section, thus violating mutual exclusion. This is why the atomic Test-and-Set is so often implemented in hardware, because that is the best way to make it atomic.

2) Reader/Writer with Writers priority using semaphores Algorithm: Reader: A reader can write only when there are no active or waiting readers and has to wait to give the writer its priority , when the last reader finishes the waiting writer is started. Writer: The writer wait till the active reader r writer gets over , if there are no active reader or writer the writer continues to write . Once the writer finishes , if there are waiting writers then put them in the writers queue (writers priority) otherwise check for waiting readers and put them in the queue Pseudo code Procedure reader P(reader_mutex) If( wbusy==false) { If( reader==0) { reader=reader+1

P(sr) // this is to lock the writer V(reader_mutex) } else { reader=reader+1 V(reader_mutex) } else { V(reader_mutex) reader=reader+1 P(new) P(sr) } <read_file> P(reader_mutex) Reader=reader-1 If((reader==0)||(wbusy==true)) V(sr) V(reader_mutex) Procedure writer P(writer_mutex) P(new) Wbusy==true P(sr) P(reader_mutex) <write_file> V(sr) Wbusy==false V(writer_mutex) V(new) V(reader_mutex)

3) Readers/writers problem using monitors to execute the requests in FCFS manner Algorithm : Each arriving Reader or writer is put in the circular buffer , the reader is assigned a value 0 and the writer is assigned a value 1 . There is also a head and a tail assigned in the circular buffer , the head is the current slot and the tail is the next slot that has to be filled. Depending on the way in which they arrive the readers and writers are addressed in a first come first serve basis

Circular_priority : monitor begin priority : array 0 to N-1 of buffer int : tail, head Procedure Startread begin priority[tail] = 0 tail = tail + 1 mod N if busy then OKtoread.wait readcount = readcount + 1 OKtoread.signal end Startread Procedure Endread begin readcount = readcount 1 if priority[head+1] = 1 { head = head + 1 mod N OKtowrite.signal } else { head = head +1 mod N OKtoread.signal } end Endread Procedure startwrite begin priority[tail] = 1 tail = tail + 1 mod N if busy OR readcount ! = 0 then OKtowrite.wait busy = true end startwrite Procedure endwrite begin busy = false if priority [head + 1] = 1 { head = head + 1 mod N OKtowrite.signal } else { head = head + 1 mod N OKtoread.signal } Endwrite

4) Semaphore based solution to readers/writers problem that alternates between the readers and writers

In this case it mean that when there is an active writer, and there is a reader and a write both waiting then the reader is let to access the critical section , and similarly when there is an acive reader and there is a waiting reader ad writer then the writer is given access to the critical section 1 st . This case can be solved like the 2 nd problem in which the writer is given priority and o matter what the writer gets access to the critical section first, this can be rectified by in thewriter section by removing the reader mutex is released first . Procedure reader P(reader_mutex) If( wbusy==false) { If( reader==0) { reader=reader+1 P(sr) // this is to lock the writer V(reader_mutex) } else { reader=reader+1 V(reader_mutex) } else { V(reader_mutex) reader=reader+1 P(new) P(sr) } <read_file> P(reader_mutex) Reader=reader-1 If((reader==0)||(wbusy==true)) V(sr) V(reader_mutex) Procedure writer P(writer_mutex) P(new) Wbusy==true P(sr) P(reader_mutex) <write_file> V(sr) Wbusy==false

V(reader_mutex) V(new) V(writer_mutex)

5) Monitor bases solution for alternating control between readers/writers Procedure startread Begin If ((busy==true) || (oktowrite.queue!=0)) //check if no writers are waiting Oktoread.wait Readcount = redadcount + 1 Oktoread.signal end startread Procedure endread Begin Readcount = readcount - 1 If(oktowrite.queue!=0) Oktowrite.signal else Oktoread.signal end endread Procedure startwrite begin if((busy==true)||(readcount!=0)) oktowrite.wait busy = true end endstartwrite Procedure endwrite begin busy = false if(oktoread.queue) oktoread.signal else oktowrite.signal end endwrite

6) The monitor code is

Print-allocator-2

{ Cond print_wait Int wait_printers =3 Release_printer(int my proc) { Wait_printers ++ Print_wait.signal(); } Request_printer (int myproc) { If(wait_printers>0) { Wait_printers --; Return j } Wait (myproc) ; Wait printers }

Você também pode gostar