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

Define busy waiting and spinlock, Define busy waiting and spinlock. Whe...

Define busy waiting and spinlock. When a process is in its significant section, any other process that tries to enter its critical section must loop continuously in the entry c

Semispace, Semispace works by maintaining two disjoint areas from which mem...

Semispace works by maintaining two disjoint areas from which memory can be allocated. These areas are called the from-space and the to-space. At ?rst, the algorithm allocates memor

Program of calculator and controller - fork, Program of Calculator and Con...

Program of Calculator and Controller with the fork, exec, and wait system calls The objective of this assignment is to get you to be comfortable with the fork(), exec(), and wai

How is process management done in multiprocessing systems, How is process m...

How is process management done in multiprocessing systems ? There are two accost for the process management in multiprocessor systems. Master-slave configuration Sy

Define the properties of interactive operating system, Define the propertie...

Define the properties of Interactive operating system Interactive:- This system is composed of many short transactions where the results of the next transaction may be unpred

How is the NTFS namespace planned, Q. How is the NTFS namespace planned? Ex...

Q. How is the NTFS namespace planned? Explain. Answer: The NTFS namespace is prearranged as a hierarchy of directories where every directory uses a B+ tree data structure to

OS is a resource allocator and control program, why we say OS is a resource...

why we say OS is a resource allocator and control program

Explain peterson''s solution for the critical section problem, Explain the ...

Explain the Peterson's solution for the critical section problem? In Peterson's solution two variables a) flag and b) turn are used as shared variables. If the both shared vari

Operating systems concepts, The code should be nicely formatted with plenty...

The code should be nicely formatted with plenty of comments. It should be easy to read, properly The summary section should include three parts: (1) Record the running t

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