Quote:
Originally Posted by
maverix
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.