Locks - mutexes, Operating System

Assignment Help:

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)


Related Discussions:- Locks - mutexes

Explain next fit algorithm, NEXT FIT ALGORITHM Here scanning starts fro...

NEXT FIT ALGORITHM Here scanning starts from the first fit position and then it finds the next position which is large sufficient to hold the process. Thus the name next fit.

Design a android application, As mentioned earlier, the game engine will b...

As mentioned earlier, the game engine will be used as the base of developing the Go game for the Android. This includes improving logic of the game, design good graphics and optim

Show the services which provided by operating system, Q. Show the services ...

Q. Show the services which provided by operating system? A) Resource Allocation: If there are more than one user or jobs running at the same instance then resource

Effects of multiprogramming on resource utilization, Effects of multiprogra...

Effects of multiprogramming on resource utilization With the utilize of multiprogramming, batch processing is able to be quite efficient .Just as multiprogramming permits the

Define secondary memory, Define secondary memory. This memory holds tho...

Define secondary memory. This memory holds those pages that are not there in main memory. The secondary memory is usually a high speed disk. It is called as the swap device, an

What are race conditions?, What are race conditions? Race conditions ar...

What are race conditions? Race conditions are problems that take place due to the sharing of the same file by several processes. In such a case none of the processes is able to

Definition of operating system, Q. Definition of Operating System? Defi...

Q. Definition of Operating System? Definition of Operating System: • An Operating system is a program which controls execution of application programs and acts as an interfa

What is a process, What is a process? A process is a program in impleme...

What is a process? A process is a program in implementation. It is an active entity and it contains the process stack, having temporary data and the data section includes globa

Explain what isam, Explain what ISAM is. Indexed sequential access meth...

Explain what ISAM is. Indexed sequential access method. The file is stored in sorted order. ISAM has a master index file, indicating in what part of another index file the key

Write Your Message!

Captcha
Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd