1 / 8

Week 5: Critical Section and Mutual Exclusion

Learn about the critical section problem and its solution through mutual exclusion methods like lock variables, Peterson's solution, and TSL instruction.

cstamant
Download Presentation

Week 5: Critical Section and Mutual Exclusion

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Week 5: Critical Section and Mutual Exclusion Dr. I. H. Shah. / CSCS 301 Fall 2009

  2. Critical Section Problem and its Solution Critical Section • If a process is accessing Shared Data it is said to be in Critical Section. • The part of a program (consisting of a few instructions) which accesses the shared data is called its Critical Section. • When a process is in its critical section a special status is granted to the process; it has exclusive access to the shared data and all other processes desiring to do so at the same time are kept waiting. • A critical section must be executed very quickly and a process must not block inside its critical section (thus critical section must be coded carefully to avoid the possibility of infinite loops). Mutual Exclusion • It is a solution to the critical section problem. • It is some way of making sure that if one process is accessing a shared data, all other processes desiring to do so at the same time are kept waiting. When this process has finished accessing the shared data, one of the processes waiting to do so is allowed to proceed. In this way, a process accessing the shared data excludes all other processes from doing so.

  3. Mutual Exclusion Mutual Exclusion (ME) • It is a solution to the critical section problem; it is some way of making sure that if one process is accessing the shared data, all other processes desiring to do so at the same time are kept waiting. When this process has finished accessing the shared data, one of the processes waiting to do so is allowed to proceed. In this way, a process accessing the shared data excludes all other processes from doing so. • Mutual Exclusion Primitives • To achieve mutual exclusion the following four conditions must be satisfied. • No two processes may simultaneously be inside their critical sections. • No assumptions are made about the process speeds and the number of CPUs. • No process stopped outside critical sections should block other processes. • No process should wait arbitrarily long to enter its critical section. • Here we shall study different proposals for achieving mutual exclusion so that when one process accesses the shared data, no other process can access that data and cause trouble.

  4. Mutual Exclusion Using Lock Variables • It is a software solution. • Consider a single shared variable lock initially set to zero. • lock=0  no process is its critical section • lock = 1  a process is in its critical section. • Working of this scheme If a process wants to enter its critical section, it tests the lock. If lock = 1, it waits. If lock = 0 the process sets it to 1 and enters its critical section. After finishing its critical section the process sets the Lock back to zero. • This method can also cause trouble! • Suppose a process tests the lock and finds it to be zero. Before it sets the lock to 1, its time quantum expires and another process gains control and set the lock to 1. If its time slice expires when it is in the critical section, the first process gains control, sets the lock to 1 and enters the critical section. In this way both the process are in the Critical Section!

  5. Mutual Exclusion Using Peterson’s Solution In 1981 Peterson proposed a much simpler solution to achieve mutual exclusion. It is illustrated below: const int N =2; int turn; int interested [N]; enter_region (int process) { int other; other = 1 – process; interested [process] =TRUE; turn = process; while(turn = = process && interested [other] = = True ); } leave_region (int process) { interested [process] = FALSE }

  6. Peterson’s Solution (Cont) • Before entering its critical section a process calls enter_region. This call will cause it to wait if there is a need to do so. After finishing the critical section the process calls leave_region to indicate its departure from the critical section. • Working of the scheme • Assume that initially no process is in critical section and process 0 desires to enter critical section; it calls enter_region. • This sets turn = 0 & interested [0] = TRUE. • Now turn = = process && interested [other] becomes FALSE, therefore loop terminates immediately and the process 0 enters its critical section. • Now if process 1 wants to enter critical section it calls enter_region. This time it will hang there until interested [0] becomes FALSE which will happen only when process 0 calls leave_region. • If both processes call enter_region simultaneously then the last value stored in turn will be effective. • EX. Suppose process 1 stores the value last so that turn = 1. Which process will be able to enter the CS first?

  7. Mutual Exclusion Using TSL instruction • This is hardware instruction and works as follows: • TSLcopies the contents of a variable (memory word) into a register and stores a non-zero value in that variable. • The operation of copying the variable and modifying its contents is guaranteed to be atomic; i.e. these instructions are executed as one uninterruptible unit. • To use TSL instruction, we shall use a shared variable flag to coordinate access to the shared memory. • When flag is 0 a process can set it to 1 using the TSL and then it can enter its critical section. • After finishing its critical section the process sets the flag back to 0 by calling leave_region. • The code (in a pseudo assembly language) and usage of this instruction to achieve the mutual exclusion is as follows:

  8. TSL instruction (Cont) enter_region: TSL register, flag CMP register, 0 JNZ enter-region RET leave_region: MOV flag, 0 RET The solution to the critical section problem is straightforward: • Before entering critical section, a process calls enter_region and does busy waiting until the flag is 0; it acquires flag, sets it to 1 & enters critical section. • On finishing critical section it calls leave_regionwhich sets the flag to 0. • The processes must call enter_region and leave_region at appropriate times for the method to work.

More Related