semaphore

Document Sample
semaphore Powered By Docstoc
					Introduction

Introduction

Semaphore:Defination

Usage of Semaphores

Condition Variable

Applications

Process Synchronization
Arvind W. Kiwelekar Assistant Professor, Department of Computer Engineering, DBTU-Lonere, 402103 Raigad awk@netone.in

February 26, 2009

Arvind W. Kiwelekar Assistant Professor, Department of Computer Engineering, DBTU-Lonere, 402103 Raigad awk@netone.in Process Synchronization

Introduction

Introduction

Semaphore:Defination

Usage of Semaphores

Condition Variable

Applications

What is synchronization?

◮

Serialization For example File should be displayed on the screen after reading it from the disk. Event B should happer after event A. Mutual Exclusion For example, Two passengers should not book the same seat, case of automated Railway Reservation system. Events A and B should not happen at the same time.

◮

Arvind W. Kiwelekar Assistant Professor, Department of Computer Engineering, DBTU-Lonere, 402103 Raigad awk@netone.in Process Synchronization

Introduction

Introduction

Semaphore:Defination

Usage of Semaphores

Condition Variable

Applications

What is Concurrency?
Execution Models
◮ ◮ ◮

Sequential Multi-Processor Multi-Process or MultiThreading
Process DisplayData q1. display(data)

Process File-Reader p1. get(filename) p2. open(filename) p3. read(filename,data)

Two event are concurrent if we can tell which event happens first by looking at the program.

Arvind W. Kiwelekar Assistant Professor, Department of Computer Engineering, DBTU-Lonere, 402103 Raigad awk@netone.in Process Synchronization

Introduction

Introduction

Semaphore:Defination

Usage of Semaphores

Condition Variable

Applications

Concurrency and Non-Determinism

Process PositiveThinker p1. print (’Yes’)

Process NegativeThinker q1. print(’no’)

What is the output? Yes followed by no no followed by yes

Arvind W. Kiwelekar Assistant Professor, Department of Computer Engineering, DBTU-Lonere, 402103 Raigad awk@netone.in Process Synchronization

Introduction

Introduction

Semaphore:Defination

Usage of Semaphores

Condition Variable

Applications

Shared Varaible Example 1

x=0 Process A p1. x=5 p2. print x Process B q1. x =7

Arvind W. Kiwelekar Assistant Professor, Department of Computer Engineering, DBTU-Lonere, 402103 Raigad awk@netone.in Process Synchronization

Introduction

Introduction

Semaphore:Defination

Usage of Semaphores

Condition Variable

Applications

Shared Varaible Example 2

count=0 Process A p1. temp=count p2. count = temp+ 1 x Process B q1. temp=count q2. count = temp+ 1 x

Mutual Exclusion Problem Shared variable such as x and count should not be updated by two processes at the same time.

Arvind W. Kiwelekar Assistant Professor, Department of Computer Engineering, DBTU-Lonere, 402103 Raigad awk@netone.in Process Synchronization

Introduction

Introduction

Semaphore:Defination

Usage of Semaphores

Condition Variable

Applications

Mutual Exclusion Problem
Requirements for the Solution
◮

The solution must work even if context switches occur while the process is in its CS. Solution should not impose any specific order. The solution must be free from deadlock. The solution must be free from starvation. The overhead of associated with providing solution should be minimal.

◮ ◮ ◮ ◮

Arvind W. Kiwelekar Assistant Professor, Department of Computer Engineering, DBTU-Lonere, 402103 Raigad awk@netone.in Process Synchronization

Introduction

Introduction

Semaphore:Defination

Usage of Semaphores

Condition Variable

Applications

Semaphores: Definition
◮

◮ ◮

◮ ◮

◮

Semaphores are the mechanisms provided by operating system to solve the problem of synchronization i.e. serialization and mutual exclusion. Semaphores are integer variables. Semaphores are initilized when they are created after that only two kinds of operations are allowed. These are increment and decrement. We can not read the current value of semaphore. When the semaphore value is decremented, and if the result is negative, thread decrementing the semaphore gets blocked. The thread resumes operation only when the semaphore value is incremented by some other thread. When a thread is incremented and if other threads are waiting one of the thread gets unblocked.

Arvind W. Kiwelekar Assistant Professor, Department of Computer Engineering, DBTU-Lonere, 402103 Raigad awk@netone.in Process Synchronization

Introduction

Introduction

Semaphore:Defination

Usage of Semaphores

Condition Variable

Applications

Semaphores: Definition

◮

The decrement operation wait(),P(), procure(). The real meaning of the opeartion is to decrement and block the invoking thread if the result is negative. The increment operation is known as signal() and V(), vacate(). The real meaning of the operation is increment and wake up a waiting process.

◮

Arvind W. Kiwelekar Assistant Professor, Department of Computer Engineering, DBTU-Lonere, 402103 Raigad awk@netone.in Process Synchronization

Introduction

Introduction

Semaphore:Defination

Usage of Semaphores

Condition Variable

Applications

Semaphores: Syntax
◮

Constructing Semaphore mySemaphore = Semaphore(1)

◮

Incrementing Semaphore mySemaphore.signal ()

◮

decrementing Semaphore mySemaphore.wait()

Arvind W. Kiwelekar Assistant Professor, Department of Computer Engineering, DBTU-Lonere, 402103 Raigad awk@netone.in Process Synchronization

Introduction

Introduction

Semaphore:Defination

Usage of Semaphores

Condition Variable

Applications

Usage of Semaphores
Signaling readingDone = Semaphore(1); // This semaphore is shared by FileReader and DiaplayData
Process File-Reader p1. get(filename) p2. open(filename) p3. read(filename,data) readingDone.signal(); Process DisplayData readingDone.wait(); q1. display(data)

Arvind W. Kiwelekar Assistant Professor, Department of Computer Engineering, DBTU-Lonere, 402103 Raigad awk@netone.in Process Synchronization

Introduction

Introduction

Semaphore:Defination

Usage of Semaphores

Condition Variable

Applications

Usage of Semaphores
Rendezous // These semaphores are shared by File-ReadAndDisplay and SortData readingDone = Semaphore(1); forSortedData = Semaphore(1);
Process File-ReadAndDisplay p1. get(filename) p2. open(filename) p3. read(filename,data) p4. readingDone.signal(); p5. forSortedData.wait() p6. display(data) Process SortData q1 readingDone.wait(); q2. sort(data) q3. forSortedData.signal();

Arvind W. Kiwelekar Assistant Professor, Department of Computer Engineering, DBTU-Lonere, 402103 Raigad awk@netone.in Process Synchronization

Introduction

Introduction

Semaphore:Defination

Usage of Semaphores

Condition Variable

Applications

Usage of Semaphores
Mutual Exclusion Problem // This variable is shared by Process A and B int count =0
Process Process A p1. count = count + 1 Process Process B q1. count = count + 1

Semaphore based Solution // This variable is shared by Process A and B mutex = Semaphore(1); int count =0;
Process Process A mutex.wait(); p1. count = count + 1 Process Process B q1. count = count + 1 mutex.signal();

Arvind W. Kiwelekar Assistant Professor, Department of Computer Engineering, DBTU-Lonere, 402103 Raigad awk@netone.in Process Synchronization

Introduction

Introduction

Semaphore:Defination

Usage of Semaphores

Condition Variable

Applications

Usage of Semaphores:Multiplex
class Table(){ int maxSeater; Table(maxSeater); sit(); vacate(); }

doubleSeater = Table(2); duplex = Semaphore(2);
Process Visitor1 duplex.wait() doubleSeater.sit() doubleSeater.vacate() duplex.signal();
Process Synchronization

Process Visistor2 duplex.wait() doubleSeater.sit() doubleSeater.vacate() duplex.signal();

Process Visistor3 duplex.wait() doubleSeater.sit() doubleSeater.vacate() duplex.signal();

Arvind W. Kiwelekar Assistant Professor, Department of Computer Engineering, DBTU-Lonere, 402103 Raigad awk@netone.in

Introduction

Introduction

Semaphore:Defination

Usage of Semaphores

Condition Variable

Applications

Mutual Exclusion
Semaphore Implementation
p u b l i c c l a s s BinarySemaphore { integer value ; BinarySemaphore ( boolean i n i t V a l u e ){ value = initValue ; } public synchronized void wait () throws I n t e r r u p t e d E x c e p t i o n { w h i l e ( v a l u e == 0 ) wait ( ) ; v a l u e= v a l u e −1 } public synchronized void s i g n a l () throws I n t e r r u p t e d E x c e p t i o n { v a l u e = v a l u e ++; notify (); Arvind W. Kiwelekar Assistant Professor, Department of Computer Engineering, DBTU-Lonere, 402103 Raigad awk@netone.in }
Process Synchronization

Introduction

Introduction

Semaphore:Defination

Usage of Semaphores

Condition Variable

Applications

Mutual Exclusion
General Structure of a cooperating program
// B e g i n n i n g s e c t i o n

// E n t r y P r o t o c o l f o r C r i t i c a l S e c t i o n // C r i t i c a l S e c t i o n // E x i t P r o t o c o l f o r C r i t i c a l S e c t i o n // R emainder S e c t i o n

Arvind W. Kiwelekar Assistant Professor, Department of Computer Engineering, DBTU-Lonere, 402103 Raigad awk@netone.in Process Synchronization

Introduction

Introduction

Semaphore:Defination

Usage of Semaphores

Condition Variable

Applications

Other Approaches to Mutual Exclusion Problem
Solution 1
p u b l i c c l a s s Attempt1 i m p l e m e n t s Lock { b o o l e a n openDoor = t r u e ; p u b l i c vo i d requestCS ( i n t i ){ w h i l e ( ! openDoor ) ; // b u s y w a i t openDoor = f a l s e ; }// r e q u e s t C S p u b l i c vo i d r e l e a s e C S ( i n t i ){ openDoor = t r u e ; }// r e l e a s e C S }

The solution does not guarantee the mutual exclusion.
Arvind W. Kiwelekar Assistant Professor, Department of Computer Engineering, DBTU-Lonere, 402103 Raigad awk@netone.in Process Synchronization

Introduction

Introduction

Semaphore:Defination

Usage of Semaphores

Condition Variable

Applications

Other Approaches to Mutual Exclusion Problem
Solution 2
p u b l i c c l a s s Attempt2 i m p l e m e n t s Lock { b o o l e a n wantCS [ ] = { f a l s e , f a l s e } ; p u b l i c vo i d requestCS ( i n t i ){ wantCS [ i ] = t r u e ; w h i l e ( wantCS [1 − i ] ) ; // b u s y w a i t }// r e q u e s t C S p u b l i c vo i d r e l e a s e C S ( i n t i ){ wantCS [ i ]= f a l s e ; }// r e l e a s e C S }

The solution does not guarantee the freedom from deadlock.
Arvind W. Kiwelekar Assistant Professor, Department of Computer Engineering, DBTU-Lonere, 402103 Raigad awk@netone.in Process Synchronization

Introduction

Introduction

Semaphore:Defination

Usage of Semaphores

Condition Variable

Applications

Other Approaches to Mutual Exclusion
Solution 3
p u b l i c c l a s s Attemp3 i m p l e m e n t s Lock { i n t turn = 0; p u b l i c vo i d requestCS ( i n t i ){ w h i l e ( t u r n == 1− i ) ; // b u s y w a i t }// r e q u e s t C S p u b l i c vo i d r e l e a s e C S ( i n t i ){ t u r n = 1− i ; }// r e l e a s e C S }

The solution imposes strict ordering.
Arvind W. Kiwelekar Assistant Professor, Department of Computer Engineering, DBTU-Lonere, 402103 Raigad awk@netone.in Process Synchronization

Introduction

Introduction

Semaphore:Defination

Usage of Semaphores

Condition Variable

Applications

Other Approaches to Mutual Exclusion
Solution 4
p u b l i c c l a s s Attempt4 i m p l e m e n t s Lock { b o o l e a n wantCS [ ] = { f a l s e , f a l s e } ; i n t t u r n =1; p u b l i c vo i d requestCS ( i n t i ){ i n t j = 1− i ; wantCS [ i ] = t r u e ; turn = j ; w h i l e ( wantCS [ j ] && t u r n == j ) ; // b u s y w a i t }// r e q u e s t C S p u b l i c vo i d r e l e a s e C S ( i n t i ){ wantCS [ i ]= f a l s e ; }// r e l e a s e C S }

The solution solves the mutual exclusion problem for only two processes. Process Synchronization

Arvind W. Kiwelekar Assistant Professor, Department of Computer Engineering, DBTU-Lonere, 402103 Raigad awk@netone.in

Introduction

Introduction

Semaphore:Defination

Usage of Semaphores

Condition Variable

Applications

Applying Semaphore to Solve Dining Philosopher Problem
Philosopher Implementation
p u b l i c c l a s s P h i l o s o p h e r i m p l e m e n t s Runnabl e { i n t i d =0; Resource r = n u l l ; p u b l i c P h i l o s o p h e r ( i n t i n i t I d , R e s o u r c e i n i t r ){ id = i nit Id ; r = initr ; new Thre ad ( t h i s ) . s t a r t ( ) ; } p u b l i c void run (){ f o r ( i n t i =0; i< 1 0 ; i ++){ try{ Syste m . o u t . p r i n t l n ( ” Run ” + i + ” P h i l ” + i d + ” t h i n k i n g ” ) ; Thre ad . s l e e p ( 3 0 ) ; Syste m . o u t . p r i n t l n ( ” Run ” + i + ” P h i l ” + i d + ” Hungry ” ) ; r . acquire ( id ); Syste m . o u t . p r i n t l n ( ” Run ” + i + ” P h i l ” + i d + ” e a t i n g ” ) ; Thre ad . s l e e p ( 4 0 ) ; r . release ( id ); } c a t c h ( I n t e r r u p t e d E x c e p t i o n e ){ return ; } } } } Arvind W. Kiwelekar Assistant Professor, Department of Computer Engineering, DBTU-Lonere, 402103 Raigad awk@netone.in Process Synchronization

Introduction

Introduction

Semaphore:Defination

Usage of Semaphores

Condition Variable

Applications

Applying Semaphore to Solve Dining Philosopher Problem
Philosopher Implementation
p u b l i c c l a s s D i n i n g P h i l o s o p h e r implements Resource { i n t n= 0 ; BinarySemaphore [ ] f o r k = n u l l ; p u b l i c D i n i n g P h i l o s o p h e r ( i n t i n i t N ){ n = initN ; f o r k = new B i n a r y S e m a p h o r e [ n ] ; f o r ( i n t i = 0 ; i <n ; i ++){ f o r k [ i ] = new B i n a r y S e m a p h o r e ( t r u e ) ; } } p u b l i c v o i d a c q u i r e ( i n t i ){ try{ fork [ i ] . wait ( ) ; f o r k [ ( i +1) %n ] . w a i t ( ) ; } c a t c h ( I n t e r r u p t e d E x c e p t i o n e ){} } p u b l i c v o i d r e l e a s e ( i n t i ){ try{ fork [ i ] . signal ( ); f o r k [ ( i +1) %n ] . s i g n a l ( ) ; } c a t c h ( I n t e r r u p t e d E x c e p t i o n e ){} } p u b l i c s t a t i c v o i d main ( S t r i n g a r g s ){ D i n i n g P h i l o s o p h e r dp = new D i n i n g P h i l o s o p h e r ( 5 ) ; f o r ( i n t i =0; i< 5 ; ) { new P h i l o s o p h e r ( i , dp ) ; } Arvind } Kiwelekar Assistant Professor, Department of Computer Engineering, DBTU-Lonere, 402103 Raigad awk@netone.in W. Process} Synchronization

Introduction

Introduction

Semaphore:Defination

Usage of Semaphores

Condition Variable

Applications

Application of Semaphores

◮ ◮ ◮

Semaphores for Mutual Exclusion Semaphores for Serialization. Semaphores for Resource Managment.

Arvind W. Kiwelekar Assistant Professor, Department of Computer Engineering, DBTU-Lonere, 402103 Raigad awk@netone.in Process Synchronization

Introduction

Introduction

Semaphore:Defination

Usage of Semaphores

Condition Variable

Applications

Limitations Semaphores

◮ ◮ ◮ ◮ ◮ ◮

Their use is not enforced. Incorrect use can cause deadlock. Priority Problem. A semaphore doesnot allow a test for busy. They are not used to pass data. Blocking is indefinite. No timeout.

Arvind W. Kiwelekar Assistant Professor, Department of Computer Engineering, DBTU-Lonere, 402103 Raigad awk@netone.in Process Synchronization


				
DOCUMENT INFO
Shared By:
Categories:
Tags:
Stats:
views:1017
posted:2/26/2009
language:English
pages:24