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
scheduling

In this project, you will implement the Chandy and Misra's (CM) algorithm using POSIX Threads (Pthreads).   The algorithm  is a distributed algorithm to solve a generalized dining

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

Write a short note on disk structure Modern disk drives are initiate as large one dimensional array of logical blocks where the logical block is the smallest unit of transfer.

What are the drawbacks of contiguous allocation of disk space? The disadvantages are a. Suffers from internal fragmentation b. Suffers from external fragmentation c. F

Can you please explain in easy words how input output protection works

Define Maintaining Data on a Per Client Basis Perhaps the most dramatic syntactical programming change for the application developer of client – server applications is maintain

You have been requested by your Senior Engineer to design an Engine Control Unit (ECU) and prepare a report detailing your proposed solution. The client requires a design for a mic


Define an assembly language  Assembly language stands for a Low level programming language