The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Advanced & Expert Users
.
google unix.com




View Single Post in the UNIX and Linux Forums - Click on the Thread or Permalink to View Entire Thread -->
  #6 (permalink)  
Old 08-29-2007
porter porter is offline Forum Advisor  
Registered User
  
 

Join Date: Jan 2007
Posts: 2,965
Quote:
Originally Posted by maverix View Post
I have used lockf() and flock() system calls and it seems to work fine.

However, I want to use semaphores to get the same effect. FYI...I am doing these just to understand and learn the concepts. Its not a live/industry project.
One thing to be aware of is the type of thing you are guarding determines the type of semphore.

If it's a file, then file locking is preferred, this way even if the file is on a NFS volume, the locking and sharing will still work.

If the thing is in a single process and the sharing is between threads then using pthread_mutex/pthread_cond et al.

If the thing is shared memory or similar that is being shared by processes that are on the same machine then you can use the kernel provided semahores.

I suggest reading anything by W.Richard Stevens on the subject.