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 an I/O subsystem? The control of devices connected to the computer is a main issue while designing operating systems. Because I/O deices differ widely in their function

Q. State three merits of placing functionality in a device controller rather than in the kernel and State three disadvantages. Answer: Three advantages: Bugs are less probabl

Explain about paging? Answer: Paging is a memory-management scheme that permits the physical-address space of process to be noncontiguous. Paging avoids the considerable proble

Transferring files from Windows to Unix Be very careful transferring files from Windows to Unix. If you do transfer a file from Windows to Unix open the file, in Unix, using vi

Q. Explain about Error detection method? Error detection: error can occur in CPU, in I/O devices or in memory hardware. Operating system constantly needs to be aware of possi

what common event leads to the creation of a process?

Managing the interaction of client and serverĀ  Among the differences between traditional application programs and client server ones is that developers must create a mechanism

Define memory management In multiprogramming system the user part of memory should be further subdivided to accommodate multiple processes. The task of subdivision is carried o

Remote IT Support Engineer Windows Systems Administrator Project Description: Need Windows base IT Support Engineer / Systems Network Administrator - perfectly MCSE qualified

SEGMENTATION In a memory management system that is properly a programmer views a program to be collection of program components called as segments . Every segment is a logical