Define a simple scenario (in the context of processes and threads in Linux) in which a counting
Question:
Define a simple scenario (in the context of processes and threads in Linux) in which a counting semaphore could be used. Your counting semaphore must NOT be implemented as a binary semaphore and must have a count greater than one. Implement your scenario in a C program.
For the threads used in both the mutex and the semaphore, you are also not allowed to use the increment operator (++) or decrement operator (--) on the shared resource because these operators are most often thread safe to begin with.
Your screencast must without a doubt prove that a mutex and semaphore are working properly and fixed an actual problem in the code. This should be done by showing the code in a failed state during your screencast.
Example scenarios could include, but are not be limited to the following:
Semaphore
- Semaphores are typically used to coordinate access to resources, with the semaphore count initialized to the number of free resources.
- A semaphore with a count of 1 behaves much like a mutex and is also known as a binary semaphore.
- When a semaphore is used to control access to a pool of resources, a semaphore tracks only how many resources are free; it does not keep track of which of the resources are free.
- wait: Decrements the value of semaphore variable by 1. If the new value of the semaphore variable is negative, the process executing wait is blocked (i.e., added to the semaphore's queue). Otherwise, the process continues execution, having used a unit of the resource.
- signal: Increments the value of semaphore variable by 1. After the increment, if the pre-increment value was negative (meaning there are processes waiting for a resource), it transfers a blocked process from the semaphore's waiting queue to the ready queue.
- Practical Scenarios:
- A system that can only support ten users
- The producer and consumer problem
- A bar or movie theater or rest room that lets only so many people in
- A fixed thread pool that will perform a bunch of work in parallel
- A fixed database connection pool
- A fixed web server request pool
- A parking lot that holds only so many cars
Mutex
- A mutual exclusion object (Mutex) is a program object that allows multiple program threads to share the same resource, such as file access, but not simultaneously.
- A thread is: each person
- The mutex is: the door handle
- The lock is: the person's hand
- The resource is: the phone
- Practical Scenarios:
- A bank transaction
- An eCommerce application tracking inventory of available items (tickets, anything in Amazon)
- Anything that shares a counter
- Preventing concurrent access/reading from a file or database data (i.e., prevent dirty reads)
- A phone booth, a shared mobile phone across family members
- Inserting data into a common data structure or linked list
- A one lane bridge that counts cars
- Detailed description of the scenario emphasizing the need for a synchronization mechanism.
- A clear, justified recommendation for the use of mutexes or semaphores in this particular scenario.
- Analysis of the pros and cons comparing the use of mutexes vs. the use of semaphores.
- Coding results:
- All the source code files. Comment your code, describing the solution and identifying the programmer.
- Screencast URL showing successful and correct execution of the code, and also demonstrating the application in a failed condition.
Smith and Roberson Business Law
ISBN: 978-0538473637
15th Edition
Authors: Richard A. Mann, Barry S. Roberts