Question

In: Computer Science

Can the following code be modified to use a mutex lock rather than a semaphore? Why...

Can the following code be modified to use a mutex lock rather than a semaphore? Why or why not? (sem_post() is the POSIX form of signal())

sem_t sem;

sem_init(&sem, 0, 1);

sem_wait(&sem);

// critical section

sem_post(&sem);

sem_destroy(&sem);

Solutions

Expert Solution

Critical Section - It is simply defined as an area in the code where shared resources can be accessed. Two or more processes can access the same resource. This creates a problem called the critical section problem when two processes want to access the resource simultaneously. To solve this problem, two mechanisms are used one is a semaphore and the other is a mutex.

Semaphore - A Semaphore is a signaling mechanism that is used in synchronization operations in computer systems, A Semaphore is used to solve critical section problem by using two atomic operations signal and wait.

Mutex - Mutex is a locking mechanism that uses a mutex lock on a resource. The mutex is used to ensure that only one thread is allowed in the critical section and use the resource.

Answer:

The following code cannot be modified to use a mutex lock because the following piece of code uses binary semaphore and a mutex can never act as a semaphore.

Lets break the code to understand better;

sem_t sem; - Here we declare a semaphore where sem_t is the typedef.

sem_init( &sem , 0 , 1 ) - Intialization of the binary semaphore *sem. A binary semaphore only uses 1 and 0 to signal. If 1 then wait operation is implemented and 0 when the signal operation is successful

sem_wait(&sem) - This implements the wait function on the semaphore *sem

critical section - The area of code where shared resources can be accessed.

sem_post(&sem) -   This implements the post function on *sem. On success, it return's 0, and on failure, the return value is -1.

sem_destroy(&sem) - Semaphore *sem is destroyed and memoy is deallocated.


Related Solutions

Explain why it is necessary to use a mutex lock in conjunction with a condition variable.
Explain why it is necessary to use a mutex lock in conjunction with a condition variable.
Why is it advantageous to use comparative financial statements to analyze a company's performance rather than...
Why is it advantageous to use comparative financial statements to analyze a company's performance rather than a single date? Select five ratios, describe how each is calculated and what information it provides. Please provide one main post . Your main post must be a minimum of 200 words (no quotes) that indicates an understanding of the concepts and material
Why might it be better to use these types of designs rather than the independent-samples design?
Why might it be better to use these types of designs rather than the independent-samples design?
Why is AX4 tedreahedral rather than square planar? Use pictures and bond angeles in explanation
Why is AX4 tedreahedral rather than square planar? Use pictures and bond angeles in explanation
Why would you use methanol rather than ethanol as a recrystalizing solvent for methyl m-nitrobenzoate? (not...
Why would you use methanol rather than ethanol as a recrystalizing solvent for methyl m-nitrobenzoate? (not because of boiling point)
why we use absorption costing rather than variable costing? if absorbing costung method will allow for...
why we use absorption costing rather than variable costing? if absorbing costung method will allow for over producing then why does CAAP allow this methid? kindly please write at least longer than 3 paragraphs explaning everything carefully. please only do this if you can write longer than 3 paragraphs and explain it well. thank you ! please kindly also explain why absorption is GAAP and variable is not, and the advantages and disadventages of the two . i spelled GAAP...
Why are violations of stochastic dominance considered irrational behaviour rather than taste of risk? How can...
Why are violations of stochastic dominance considered irrational behaviour rather than taste of risk? How can violations of stochastic dominance be observed in subjective probability weighting curves?
Why are plasma membranes arranged as a bilayer rather than a monolayer?
Why are plasma membranes arranged as a bilayer rather than a monolayer?
Find the following Taylor Series with given centers. Use the algebraic methods rather than the derivatives....
Find the following Taylor Series with given centers. Use the algebraic methods rather than the derivatives. 1. f(x) = x^3 + x^2 -2x +3 centered at a= -1 2. sin(x) centered at a= pi/2 3. f(x) = (e^x - e^-x)/2 centered at a= 0
Discuss why one would use inferential statistics rather than correlational statistics. Be sure to clearly identify...
Discuss why one would use inferential statistics rather than correlational statistics. Be sure to clearly identify these reasons and back your statements with examples
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT