EXAMPLE FILE: semsWithIntr




//////////////////////////////////////////////////
HOW TO IMPLEMENT SEMAPHORES BY BLOCKING INTERRUPTS.
//////////////////////////////////////////////////


In this implementation, the "procedures" below are actually
implemented as system calls, executed by the kernel in
privileged mode.
 
      (* a variable shared by all n processes *)
var semaphore : record
                  count : integer ;
		  queue : queueType 
                end ;


(* #################################################### *)
procedure InitSem (var sem : semaphore; val : integer ) ;
begin
  sem.count := val ;
  CreateQ(queue) ;
end ;

(* #################################################### *)
procedure Wait (var sem : semaphore ; him : process) ;
begin

  ... block interrupts ....

  sem.count := sem.count - 1 ;
  if    sem.count < 0
  then  block(him) ;

  ... un-block interrupts ....

  
end ;


(* #################################################### *)
procedure Signal (var sem : semaphore ) ;
var P : ^process ;
begin

  ... block interrupts ....

  sem.count := sem.count + 1 ;
  if    sem.count <= 0
  then  begin
	  Deq(sem.queue, P) ;
          wakeup(P) 
	end ;

  ... un-block interrupts ....
  
end ;