next up previous
Next: Readers and Writers Up: No Title Previous: No Title

Semaphore Implementation

  figure22
Figure 9:

 
TYPE semaphore = RECORD
   head, tail : POINTER TO pcb 
       count  : integer ;
END

PROCEDURE P(s:semaphore) ; {wait}
 IF s.count > 0
 THEN s.count := s.count - 1 ;
 ELSE current^.state := blocked ;
      addtail(current,s) {insert at end of Queue}
 END
END

PROCEDURE V(s:semaphore)
 IF s.head = null {nothing waiting}
 THEN s.count := s.count + 1 ;
 ELSE s.head^.state := runnable ;
      remove pcb at head of Q ;
      schedule it {insert in ready Q} ;
 END
END

PROCEDURE Initsemaphore(s,n:integer) ;
 {An exercise for you}



Omer F Rana
Tue Feb 11 19:19:52 GMT 1997