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
What is a Relocatable program? The Relocatable programs can be loaded almost anywhere in memory.

Explain the Per Thread Scoping Thread-level programming introduces new twists for application-level variable scoping. Threads are commonly used in one of two ways. To ex

swapping technique

What are the objectives of operating system? Objectives of OS 1.      Convenience: An OS makes a computer more suitable to use. 2.      Efficiency : An OS allows t

. A paper mill produces two grades of paper viz., X and Y. Because of raw material restrictions, it cannot produce more than 400 tons of grade X paper and 300 tons of grade Y paper

Q. Does Windows XP offer any user-mode processes that enable it to run programs developed for other operating systems? Describe two of these subsystems. Answer: Environmental

Explain the OS/2 Operating System Like NetWare, OS/2 manage resources at three levels, as shown in Figure 4.5 Resources may be maintained at the session, process, and thread le

what is a batch system give me answer in simple words so that i can understand

Hierarchical paging Single way is to use a two-level paging algorithm, in which the page table itself is also paged. A logical address is separated into a page number and a pag

College life is tough.  Eating pizza for every meal is hitting you hard.  You are looking at working out to stay healthy.  You found a web site that tells you how many calories eac