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 Real-time system? A Real-time system is used when inflexible time requirements have placed on the operation of processor or the flow of data so it is often used as a

What is a path name? A pathname is the path from the root by all subdirectories to a specified file. In a two-level directory structure a user name and a file name explain a pa

How can the deadlock be prevented Bankers algorithm : The Resource allocation graph algorithm isn't applicable to a resource allocation system with multiple instances of every

scattered resposibi;ity and authority -necessity of operation research

how to differentiate scheduling algorithms?

Multiprocessing - An Operating System proficient of utilizing and supporting more than one computer processor. Below are some instances of multiprocessing Operating Systems. Li

Fault tolerance : Machine failures, Communication faults, storage device crashes, and decays of storage media must be tolerated by the system to some extent. A fault tolerant sy

What is a file path name? A list of the subdirectories, directories, and files we must traverse to reach a file from the root directory.

LINUX OPERATING SYSTEM 1. Write a brief explanation about the Linux distribution. 2. Explain the features that Bash shell provides? 3. Who is responsible for managing use

Page fault is a trap to the software gained by the hardware when a process accesses a page that is goes in the virtual address space, but not added in physical memory. In the har