[Subject Prev][Subject Next][Thread Prev][Thread Next][Subject Index][Thread Index]

Re: semaphores



hi ! 


semaphores are location in memory that can be tested and set/reset
by more then one processes.Semaphores can be used to implement
critical reagons ( the areas of the code that can be executed by 
only one processes at a particuler time  ) 

now lets suppose u have many co-operating processes reading and
writing to a data file and u want the access to the file strictly 
cordinated in this case u will put 
  
   a semaphore with vale 1(or true)  attached with the file and 

//******> static struct semaphore cmd_semaphore = MUTEX;

 two functions 
 
  one to increase (make it true )the value of semaphore (say up )
  other to decrease the value ( make it false) of the semaphore  (say down)

********>>> down(&cmd_semaphore);
**********> up(&cmd_semaphore);

value 1 of semaphore will show that file is free while value 0 will 
show that its busy 

whenever a process wants to use the file it will call down 

 if this call get success the file will be given to process and 
    semaphore value will be made 0, after its use this process will call 
    up to set the value of semaphore back to 1  and free the resource
     (file in present case )

 if not the process will wait till the other process makes it free


hope this is what u wanted ....

others pls add if i have missed something 


shubh
REC-Surat
--------------------------
            @>
i love    /( )\  linux
           ^ ^ 
--------------------------