202k views
2 votes
Give an example where binary semaphores would be preferred over

general semaphores and vice versa.

User Wpearse
by
7.6k points

1 Answer

2 votes

Final answer:

Binary semaphores are preferred for mutual exclusion while general semaphores are preferred for counting and concurrent resource access.

Step-by-step explanation:

Binary semaphores and general semaphores are synchronization mechanisms used in parallel programming to control access to shared resources. Binary semaphores can only take two values: 0 and 1. They are often used for mutual exclusion, where only one thread can access a resource at a time. General semaphores can have any non-negative value and are used for counting, where multiple threads can access a resource at the same time.

An example where binary semaphores would be preferred is in a scenario where there is a critical section of code that can only be accessed by one thread at a time. For instance, in a multi-threaded application, a binary semaphore can be used to ensure that only one thread can access a file for writing at a time.

An example where general semaphores would be preferred is in a scenario where a fixed number of resources are available and multiple threads can access them concurrently. For example, in a server application that can handle a limited number of incoming connections, a general semaphore can be used to track the number of available connections and allow multiple threads to use them as they become available.

User Zapotec
by
8.5k points