Reputation: 572
In UNIX-like systems we have both unnamed semaphores and named semaphores. For basically everything I've done, since I'd have to pass the pointer to the actual data to access, I always managed to pass the semaphore too, to whatever thread had to access the resource itself. Simple technique: creating a struct with both the object and the semaphore.
What are some cases in which it's best to use a named semaphore?
Upvotes: 0
Views: 426
Reputation: 462
Exactly as jspcl said. Named semaphores are used for synchronization between process. Between two process same address cannot be shared (as there are some exceptions in some embedded os such as integrity,qnx etc.. ).Because each process have different address spaces where address pages are different .
So named semaphores are used to access the shared memory between process.
For example , shmget() posix call in Linux is used for named semaphores. In this call u have to the semaphore name which is of char* along with read and write permissions of the memory for the process. It will return a file descriptor for that semaphore associated with the shared memory .After that u have to call mmap to get the shared memory address . By which u can read and write in to the shared memory.
Real world example is aeronautics where multiple processors are used. One process will interacts with other electronic parts and will send information to other processor . Hence in other processor there has to be process which can read data from serial port and then send to other process . So here for multiprocess communication named semaphore is used
Upvotes: 1