Smt.Chandaben Mohanbhai Patel Institute of Computer ...€¦  · Web viewSmt.Chandaben Mohanbhai...

21
Smt.Chandaben Mohanbhai Patel Institute of Computer Applications, CHARUSAT, Changa Subject: MS103 OSC Unit-II Key Terms related to Concurrency (1) Critical Section: - Section of code within a process that requires access to shared resources and must not be executed while another process is in a corresponding section of code. (2) Deadlock: - It is a situation in which two or more processes are unable to proceed because each is waiting for one of the others to do something. (3)Race Condition: - It is a situation in which multiple threads or processes read and write a shared data item and the final result depends on the relative timing of their execution. (4)Starvation: - It is a situation in which a runnable process is overlooked indefinitely by the scheduler. (5)Mutual Exclusion:- It is a requirement according to which only one process may remain in critical section at a time. Mutual Exclusion using Hardware Support [1] Interrupt Disabling [2] Special Machine Instructions Prepared By : Dharmendra Patel Page 1 while (true) { /* disable interrupts */; /* critical section */; /* enable interrupts */;

Transcript of Smt.Chandaben Mohanbhai Patel Institute of Computer ...€¦  · Web viewSmt.Chandaben Mohanbhai...

Page 1: Smt.Chandaben Mohanbhai Patel Institute of Computer ...€¦  · Web viewSmt.Chandaben Mohanbhai Patel Institute of Computer Applications, CHARUSAT, Changa Subject: MS103 OSC Unit-II

Smt.Chandaben Mohanbhai Patel Institute of Computer Applications, CHARUSAT, Changa Subject: MS103 OSC Unit-II

Key Terms related to Concurrency

(1) Critical Section: - Section of code within a process that requires access to shared resources and must not be executed while another process is in a corresponding section of code.

(2) Deadlock: - It is a situation in which two or more processes are unable to proceed because each is waiting for one of the others to do something.

(3)Race Condition: - It is a situation in which multiple threads or processes read and write a shared data item and the final result depends on the relative timing of their execution.

(4)Starvation: - It is a situation in which a runnable process is overlooked indefinitely by the scheduler.

(5)Mutual Exclusion:- It is a requirement according to which only one process may remain in critical section at a time.

Mutual Exclusion using Hardware Support

[1] Interrupt Disabling

[2] Special Machine Instructions

(a) Compare and Swap: - The function of compare and swap is as follows:

Prepared By : Dharmendra Patel Page 1

while (true) {/* disable interrupts */;

/* critical section */;

/* enable interrupts */;

/* remainder */;}

Page 2: Smt.Chandaben Mohanbhai Patel Institute of Computer ...€¦  · Web viewSmt.Chandaben Mohanbhai Patel Institute of Computer Applications, CHARUSAT, Changa Subject: MS103 OSC Unit-II

Smt.Chandaben Mohanbhai Patel Institute of Computer Applications, CHARUSAT, Changa Subject: MS103 OSC Unit-II

(b) Exchange Function: - The exchange function is as follows:

Prepared By : Dharmendra Patel Page 2

int compare_and_swap (int *word, int testval, int newval)

{int oldval;oldval = *word;if (oldval == testval) *word = newval;

return oldval;}

Page 3: Smt.Chandaben Mohanbhai Patel Institute of Computer ...€¦  · Web viewSmt.Chandaben Mohanbhai Patel Institute of Computer Applications, CHARUSAT, Changa Subject: MS103 OSC Unit-II

Smt.Chandaben Mohanbhai Patel Institute of Computer Applications, CHARUSAT, Changa Subject: MS103 OSC Unit-II

Prepared By : Dharmendra Patel Page 3

void exchange (int register, int memory)

{int temp;temp = memory;memory = register;register = temp;

}

Page 4: Smt.Chandaben Mohanbhai Patel Institute of Computer ...€¦  · Web viewSmt.Chandaben Mohanbhai Patel Institute of Computer Applications, CHARUSAT, Changa Subject: MS103 OSC Unit-II

Smt.Chandaben Mohanbhai Patel Institute of Computer Applications, CHARUSAT, Changa Subject: MS103 OSC Unit-II

Characteristics of Special Machine Instructions:

(1) It is applicable to any number of processes and in uniprocessor as well as multiprocessor environment.

(2) It is simple so it is easy to verify.

(3) It may be extended to multiple critical sections.

Limitations

(1)Busy waiting is employed: - Even though process is waiting for access to a critical section, it continues to consume processor time.

(2) Starvation is possible: - When process leaves a critical section, number of processes are ready for critical section but the process selection is done arbitrary so it might be possibility that one process is overlooked.

(3) Deadlock may possible: - In uniprocessor environment, assume that process P1 is in execution mode and it requires resource R1 so it is assigned to P1. After sometimes one higher priority process P2 comes so P1 is preempted by scheduler and P2 is put for execution. After some execution P2 also requires same resource R1 that is currently with P1 , in this case neither P1 nor P2 is able to proceeds and it results in deadlock.

Mutual Exclusion using Semaphore

Semaphore: - It is a special variable that is used to provide coordination among processes by means of signaling.

Operations of Semaphore

(1) Semaphore variable is initialized with any non negative value.

(2) The semWait() operation decrements the semaphore value and if value becomes negative the process executing semWait() is blocked otherwise it continues execution.

(3) The semSignal() operation increments the semaphore value and if value becomes less than or equal to zero, the process blocked by a semWait , if any, is unblocked.

Prepared By : Dharmendra Patel Page 4

Page 5: Smt.Chandaben Mohanbhai Patel Institute of Computer ...€¦  · Web viewSmt.Chandaben Mohanbhai Patel Institute of Computer Applications, CHARUSAT, Changa Subject: MS103 OSC Unit-II

Smt.Chandaben Mohanbhai Patel Institute of Computer Applications, CHARUSAT, Changa Subject: MS103 OSC Unit-II

Mutual Exclusion Using Semaphore

Prepared By : Dharmendra Patel Page 5

Page 6: Smt.Chandaben Mohanbhai Patel Institute of Computer ...€¦  · Web viewSmt.Chandaben Mohanbhai Patel Institute of Computer Applications, CHARUSAT, Changa Subject: MS103 OSC Unit-II

Smt.Chandaben Mohanbhai Patel Institute of Computer Applications, CHARUSAT, Changa Subject: MS103 OSC Unit-II

Solution of producer/consumer using semaphore

Prepared By : Dharmendra Patel Page 6

Page 7: Smt.Chandaben Mohanbhai Patel Institute of Computer ...€¦  · Web viewSmt.Chandaben Mohanbhai Patel Institute of Computer Applications, CHARUSAT, Changa Subject: MS103 OSC Unit-II

Smt.Chandaben Mohanbhai Patel Institute of Computer Applications, CHARUSAT, Changa Subject: MS103 OSC Unit-II

Solution of readers/writers in which readers have a priority

Prepared By : Dharmendra Patel Page 7

Page 8: Smt.Chandaben Mohanbhai Patel Institute of Computer ...€¦  · Web viewSmt.Chandaben Mohanbhai Patel Institute of Computer Applications, CHARUSAT, Changa Subject: MS103 OSC Unit-II

Smt.Chandaben Mohanbhai Patel Institute of Computer Applications, CHARUSAT, Changa Subject: MS103 OSC Unit-II

Solution of readers/writers in which writers have a priority

Mutual Exclusion using Monitor

Monitor is a software module that consists of one or more procedures, local data and initialization sequence.

Monitor provides equivalent functionality to that of semaphores and is easier to control. Synchronization using Monitor

Achieved by the use of condition variables that are contained within the monitor and accessible only within the monitor

Condition variables are operated on by two functions:

(a)cwait(c): suspend execution of the calling process on condition c. The monitor is now available for other processes.

(b)csignal(c): resume execution of some process blocked after a cwait on the same condition. If there are numbers of such processes select any one otherwise do nothing.

Prepared By : Dharmendra Patel Page 8

Page 9: Smt.Chandaben Mohanbhai Patel Institute of Computer ...€¦  · Web viewSmt.Chandaben Mohanbhai Patel Institute of Computer Applications, CHARUSAT, Changa Subject: MS103 OSC Unit-II

Smt.Chandaben Mohanbhai Patel Institute of Computer Applications, CHARUSAT, Changa Subject: MS103 OSC Unit-II

Conditions of deadlock

(1)Mutual exclusion

Only one process may use a shared resource at a time.

(2)Hold-and-wait

A process may hold allocated resources while awaiting assignment of others.

Prepared By : Dharmendra Patel Page 9

Page 10: Smt.Chandaben Mohanbhai Patel Institute of Computer ...€¦  · Web viewSmt.Chandaben Mohanbhai Patel Institute of Computer Applications, CHARUSAT, Changa Subject: MS103 OSC Unit-II

Smt.Chandaben Mohanbhai Patel Institute of Computer Applications, CHARUSAT, Changa Subject: MS103 OSC Unit-II

(3)No preemption

No resource can be forcibly removed from a process holding it.

(4)Circular wait

A closed chain of processes exists, such that each process holds at least one resource needed by the next process in the chain.

Deadlock Prevention

(1)Mutual exclusion

Mutual exclusion condition is not disallowed from the system but for certain resources like file allow multiple accesses for read and exclusive access for write.

(2)Hold-and-wait

Hold and wait condition is prevented by requiring that process request all its required resources at once and block itself until all requests are granted simultaneously. This solution has two main drawbacks:

(a) Process may hold up for long period of time.

(b) Process may not know in advance all resources that is required.

(3)No preemption

No preemption condition can be prevented my many ways:

(a) The process holding certain resources is denied for further request; it must release all its original resources and if necessary, request them again together with additional resource.

(b) If a process request a resource and that is currently held by any other process, the OS may preempt the second process and release its resource for first process.

(4)Circular wait

Circular wait condition is prevented by defining a linear ordering of resource types. If a process has been allocated resources of type R, then it may subsequently request only those resources of types following R in ordering.

Prepared By : Dharmendra Patel Page 10

Page 11: Smt.Chandaben Mohanbhai Patel Institute of Computer ...€¦  · Web viewSmt.Chandaben Mohanbhai Patel Institute of Computer Applications, CHARUSAT, Changa Subject: MS103 OSC Unit-II

Smt.Chandaben Mohanbhai Patel Institute of Computer Applications, CHARUSAT, Changa Subject: MS103 OSC Unit-II

Deadlock Avoidance

State of the system is the current allocation of resources to process. It involves two vectors and two metrices.Vectors- Resources, AvailableMetrics- Claim, Allocation

Safe state is where there is at least one sequence that does not result in deadlock Banker’s Algorithm is used to determine safe state. The equation is

C- A <=V Where C= Claim, A=Allocation, V=Available Example of sate state

Prepared By : Dharmendra Patel Page 11

Page 12: Smt.Chandaben Mohanbhai Patel Institute of Computer ...€¦  · Web viewSmt.Chandaben Mohanbhai Patel Institute of Computer Applications, CHARUSAT, Changa Subject: MS103 OSC Unit-II

Smt.Chandaben Mohanbhai Patel Institute of Computer Applications, CHARUSAT, Changa Subject: MS103 OSC Unit-II

Example of Unsafe state

Prepared By : Dharmendra Patel Page 12

Page 13: Smt.Chandaben Mohanbhai Patel Institute of Computer ...€¦  · Web viewSmt.Chandaben Mohanbhai Patel Institute of Computer Applications, CHARUSAT, Changa Subject: MS103 OSC Unit-II

Smt.Chandaben Mohanbhai Patel Institute of Computer Applications, CHARUSAT, Changa Subject: MS103 OSC Unit-II

Deadlock Detection Algorithm

(a)Mark each process that has a row in the allocation matrix of all zeros

(b)Initialize a temporary vector W to equal to available vector

(c) Find an index i such that process i is currently unmarked and the ith row of request matrix is less than or equal to W. If no such row is found, terminate the algorithm and all unmarked processes are those involved in the deadlock.

(d)If such a row is found, mark process i and add the corresponding row of the allocation matrix to W.

Prepared By : Dharmendra Patel Page 13

Page 14: Smt.Chandaben Mohanbhai Patel Institute of Computer ...€¦  · Web viewSmt.Chandaben Mohanbhai Patel Institute of Computer Applications, CHARUSAT, Changa Subject: MS103 OSC Unit-II

Smt.Chandaben Mohanbhai Patel Institute of Computer Applications, CHARUSAT, Changa Subject: MS103 OSC Unit-II

Recovery Mechanism

• Abort all deadlocked processes

• Back up each deadlocked process to some previously defined checkpoint, and restart all processes

• Successively abort deadlocked processes until deadlock no longer exists

Dining Philosopher SolutionUsing Semaphore that leads to deadlock

Prepared By : Dharmendra Patel Page 14

Page 15: Smt.Chandaben Mohanbhai Patel Institute of Computer ...€¦  · Web viewSmt.Chandaben Mohanbhai Patel Institute of Computer Applications, CHARUSAT, Changa Subject: MS103 OSC Unit-II

Smt.Chandaben Mohanbhai Patel Institute of Computer Applications, CHARUSAT, Changa Subject: MS103 OSC Unit-II

Using Semaphore that does not lead to deadlock

Prepared By : Dharmendra Patel Page 15

Page 16: Smt.Chandaben Mohanbhai Patel Institute of Computer ...€¦  · Web viewSmt.Chandaben Mohanbhai Patel Institute of Computer Applications, CHARUSAT, Changa Subject: MS103 OSC Unit-II

Smt.Chandaben Mohanbhai Patel Institute of Computer Applications, CHARUSAT, Changa Subject: MS103 OSC Unit-II

Prepared By : Dharmendra Patel Page 16