Locks - mutexes, Operating System

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)

Posted Date: 3/12/2013 5:14:02 AM | Location : United States







Related Discussions:- Locks - mutexes, Assignment Help, Ask Question on Locks - mutexes, Get Answer, Expert's Help, Locks - mutexes Discussions

Write discussion on Locks - mutexes
Your posts are moderated
Related Questions
Q. Consider a distributed system with two sites A and B. Consider whether site A can distinguish among the following: a. B goes down. b. The link between A and B goes down.

Define the Non Monolithic Coding First off, client - server developers must adopt a new programming mindset. Much as with the shift to object - oriented design, developers sho

Medium term scheduling is form of the swapping operation. This attaches to processes that are in a suspended or blocked state. They are swapped out of real-memory storage until t

Define the OS/2 threads code The sample program in Listing is very similar to the NetWare example. OS/2 commands are substituted for the pseudocode commands controlling thread

Define what is Dispatcher In the Worker Model, it is the task of the Dispatcher to spawn a thread for every client-to-server connection. The Dispatcher acts as the go-between o

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,

Q. Explain about Spooling? Acronym for simultaneous peripheral operations on line. Spooling denotes to putting jobs in a buffer, a special area in memory or on a disk where

Question: a) The following questions refers to Windows XP networking: i) Briefly, explain how a host joins a network using DHCP? ii) Which IP address could be assigned to a

Deadlock Prevention Prevention is the name given to method that guarantee that deadlocks can never happen for the reason that the way the system is structured. Since 4 conditio

Q. In what circumstances is a token-passing network more effectual than an Ethernet network? Answer: A token ring is extremely effective under high sustained load as no colli