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
Define process? What is meant by state of a process? Process is a program in implementation. A process in general includes the process stack, which consists temporary data and

LRU page replacement In least lately used page replacement algorithm we will replace that page that hasn't been used for the longest period of time. This is similar as an opti

Determine a processing that is not a part of Synthesis phase  Perform LC processing is not a part of Synthesis phase

What are the disadvantages of swapping technique used in pre-3BSD UNIX systems? If there is excessively much memory contention, processes are swapped out until sufficient

Q. Define the difference among pre-emptive and non pre-emptive scheduling. Answer: Pre-emptive scheduling permits a process to be interrupted in the midst of its execution ta

Entry-sequenced file organization The records in an entry-sequenced file are store up in the order they are written into the file. New records are for all time appended to

Difference between cache and associate memory

Question: (a) The following table (table 1.0) gives the length of the CPU burst time in milliseconds of processes P 1 to P 4 . Assume the processes arrived in the order P 1 ,

Utility Programs: those programs which are not the essential part of operating system. But they provide different utilities / features to the users e.g. MS-DOS : Fdisk, FORMAT, AT

Explain the Fork-join This is primitives in a higher level programming language for implementing interacting processes. The syntax is like this: fork ; join ; where