Locks - mutexes, Operating System

Assignment Help:

Locks (also known as mutexes, short for mutual exclusion locks) provide mutual exclusion to shared data inside a critical session. They are implemented by means of two atomic routines:

acquire, which waits for a lock, and takes it when possible; and release, which unlocks the lock and wakes up the waiting threads. The rules for using locks/mutexes are the following:

1. only one person can have the lock;

2. locks must be acquired before accessing shared data;

3. locks must release after the critical section is done;

4. locks are initially released.

The syntax for using locks in C/C++ is the following:

pthread_mutex_init(&l);
...
pthread_mutex_lock(&l);
update data // this is the critical section
pthread_mutex_unlock(&l);

Let us now try to rewrite the "Too Much Milk" problem in a simpler, cleaner, more symmetric, and portable way, using locks. In order to do so, the code for Thread A (and also for Thread B) would be the following:

pthread_mutex_lock(&l)
if (no milk)
buy milk
pthread_mutex_unlock(&l)


Related Discussions:- Locks - mutexes

Relationship among the capabilities of domain level j and i, Q. In a ring-...

Q. In a ring-protection system the level 0 has the greatest access to objects and level n (greater than zero) has fewer access rights. The access rights of a program at a partic

Which algorithm makes the most efficient use of memory, Question : (a) ...

Question : (a) The dynamic allocation problem is how to satisfy a request of size n from a list of free holes into memory. The best-fit, first-fit and worst-fit strategies are

Explain doscreatethread functions used in the os/2, Explain DosCreateThread...

Explain DosCreateThread Functions Used in the OS/2  DosCreateThread(&threadID,MyThreadFunction,arg,flags,stackSize) DosCreateThread creates a thread within the context of th

What is preemptive and nonpreemptive scheduling, What is preemptive and non...

What is preemptive and nonpreemptive scheduling? Under nonpreemptive scheduling once the CPU has been allocated to a process, the process keeps the CPU unless it releases the C

Explanation of modern cpu architectures, Your CTO liked your proposal from ...

Your CTO liked your proposal from Unit 2 so much he only trusts you to explain complex concepts to his managers. He now wants you to write him a four page paper explaining the CPU,

Define i/o and cpu bursts, Define I/O and CPU Bursts   Applications typ...

Define I/O and CPU Bursts   Applications typically to through cycle of CPU bursts and Input/output bursts. Since the CPU sits idle during I/O operations, efficiency is improved

What is the information associated with an open file, What is the informati...

What is the information associated with an open file? Various pieces of information are associated with  an open file which may be: File pointer Disk location of th

What is preallocation, What is preallocation? Why do it? Allocating spa...

What is preallocation? Why do it? Allocating space for a file before making the file to allow for expansion. This reserves space for a certain file so that other files can't gr

Write Your Message!

Captcha
Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd