Synchronization, Operating System

Assignment Help:

As we already know, threadsmust ensure consistency; otherwise, race conditions (non-deterministic results) might happen. Now consider the "too much milk problem": two people share the same fridge and must guaran tee that there's always milk, but not too much milk. How can we solve it? First, we consider some important concepts and their de?nitions:

 Mutex: prevents things from operating on the same data at the same time;

 Critical section: a piece of code that only one thread can execute at a time;

 Lock: a mechanism for mutual exclusion; the program locks on entering a critical section, accesses the shared data, and then unlocks. Also, a program waits if it tries to enter a locked section.

 Invariant: something that must always be true when not holding the lock. For the above mentioned problem, we want to ensure some correctness properties. First, we want to guarantee that only one person buys milk when it is need (this is the safety property, aka "noth-ing bad happens"). Also, wewant to ensure that someone does buymilkwhen needed (the progress property, aka "something good eventually happens"). Nowconsider thatwe can use the following atomic operations when writing the code for the problem:

 "leave a note" (equivalent to a lock)

 "remove a note" (equivalent to an unlock)


"don't buy milk if there's a note" (equivalent to a wait)

An atomic operation is an unbreakable operation. Once it has started, no other thread or process can interrupt it until it has ?nished. Our ?rst try could be to use the following code on both threads:

if (no milk && no note) {
leave note;
buy milk;
remove note;
}
Unfortunately, this doesn't work because both threads could simultaneously verify that there's no note and no milk, and then both would simultaneously leave a note, and buy more milk. The problem in this case is that we end up with too much milk (safety property not met).

Now consider our solution #2:

Thread A:
leave note "A";
if (no note "B")
if (no milk)
buy milk;
remove note "A";
Thread B:
leave note "B";
if (no note "A");
if (no milk)
buy milk;
remove note "B";

The problemnowis that if both threads leave notes at the same time, neitherwill ever do anything. Then, we end up with no milk at all, which means that the progress property not met. Now, let's consider an approach that does work:

Thread A
leave note A
while (note B)
do nothing
if (no milk)
buy milk
remove note A
Thread B
leave note B;
if (no note A)
if (no milk)
buy milk;
remove note B;

This approach, unlike the two examples considered on the previous class, does work. However, it is complicated: it is not quick-and-easy to convince yourself that these two sections of code always produce the desired behavior.


Related Discussions:- Synchronization

What happens when you execute a program in unix, When you run a program on ...

When you run a program on your UNIX system, the system prepares a special environment for that program. This environment owns everything needed for the system to execute the progra

Data transfer between two processes, Q. Data transfer between two processes...

Q. Data transfer between two processes? Communication: Data transfer between two processes is essential for some time. Both processes are on the one computer or on different

What is long-term, What is long-term, medium-term, short-term? In a bat...

What is long-term, medium-term, short-term? In a batch system many processes are submitted than are able to be executed immediately. These processes are reel to a mass storage

What is an operating system, What is an operating system? An operating ...

What is an operating system? An operating system is a program that manages the computer hardware. It acts as an intermediate among users of a computer and the computer hardware

Determine the syntax of the assembler directive equ, Determine the syntax o...

Determine the syntax of the assembler directive EQU The following is syntax of the assembler directive EQU: EQU

What kind of protection structure do we have, Q. Consider a calculating en...

Q. Consider a calculating environment where a unique number is associated with each process and each object in the system. Suppose that we permit a process with number n to acce

Activities of operating system-secondary-storage management, Q. What are th...

Q. What are the three main activities of an operating system in regard to secondary-storage management? Answer: 1) Free-space management 2) Storage allocation 3) Disk

Producer-consumer using condition variables, Now let us present an implemen...

Now let us present an implementation of a producer-consumer system using condition variables. This implementation works. dequeue() lock(A) while (queue empty) { wait(A, C)

What is external fragmentation?, What is external fragmentation? As pro...

What is external fragmentation? As process are removed from and loaded to the memory free memory space is bracken into pieces .external fragmentation take place when enough mem

Define the conditions of deadlock prevention, What are the four necessary c...

What are the four necessary conditions of deadlock prevention?       Ans:  Four essential conditions for deadlock prevention: a. The meaning of removing the mutual exclusio

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