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
Q. Why is rotational latency typically not considered in disk scheduling? How would you alter SSTF, SCAN and C-SCAN to comprise latency optimization? Answer: Most disks don't

What is an I/O subsystem? The control of devices connected to the computer is a main issue while designing operating systems. Since I/O devices differ widely in their function

Determine the reasons for Poor response time Process busy or High I/O rates or High paging rates

Partitions and mounting A disk can be sliced into many partitions, or a partition can span multiple disks. Every partition can be either "raw", having no file system, or "cooke

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

Q. Process migration in a heterogeneous network is typically impossible given the differences in architectures and operating systems. Explain a method for process migration across

I need help implementing malloc


Define the TlsFree(TLSIndex) Function This function should be called to free a TLSindex allocated by TlsAlloc. It would be executed when there are no more threads in a process

What is internal fragmentation? Consider holes of 20k assume the process requests 18 bites. If we allocate accurately the request block, we are left with a hole of 2k. The over