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

Briefly explain the process management in unix., Briefly explain the proces...

Briefly explain the process management in UNIX. UNIX makes use of the easy but powerful process facility that is visible to the user. UNIX follows the model in which the majori

Kernel, 1. What must a kernel provide for an effective user-level thread im...

1. What must a kernel provide for an effective user-level thread implementation? 2. With respect to the quantum q in a scheduling algorithm, explain and discuss the impact of the

Explain the steps in deadlock recovery, Explain the steps in deadlock recov...

Explain the steps in deadlock recovery. a)      Exterminate off all processes involved - drastic. b)      Exterminate off one at a time till deadlock gone - select a victim.

Is computers protect the operating system, Q. Some untimely computers prot...

Q. Some untimely computers protected the operating system by placing it in a memory partition that couldn't be modified by either the user job or the operating system itself. Expl

Explain the reasons why an organisation fail to plan, Problem 1: (a) Wh...

Problem 1: (a) What are the likely benefits which an organisation may derive from the planning process? (b) Explain the main reasons why an organisation may fail to plan.

What are files and explain the access methods for files, What are files and...

What are files and explain the access methods for files? File definition Attributes, operations and types Direct access Sequential access with diagram Other access

Write a linux script to add name in class list, For this exercise I wanted ...

For this exercise I wanted to give you some real-world examples of useful bash scripts. Therefore, you will write a couple of scripts that I actually use in my teaching to automate

#title. operation research, What are the features of Operation Research app...

What are the features of Operation Research approach?

Fundamental of operating system, An operating system provides the environme...

An operating system provides the environment within that programs are executed. Internally operating systems differ greatly in their makeup because they are organized along many di

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