Process Synchronization – Outline Wh y d o p r o c e s s e s n e e d s y n c h r o n i z a t i o n ? Wh a t i s t h e c r i t i c a l - s e c t i o n p r o b l e m ? D e s c r i b e s o l u t i o n s t o t h e c r i t i c a l - s e c t i o n p r o b l e m P e t e r s o n ’ s s o l u t i o n u s i n g s y n c h r o n i z a t i o n h a r d w a r e s e m a p h o r e s m o n i t o r s C l a s s i c P r o b l e m s o f S y n c h r o n i z a t i o n Wh a t a r e a t o m i c t r a n s a c t i o n s ? 1
Why Process Synchronization ? P r o c e s s e s m a y c o o p e r a t e w i t h e a c h o t h e r p r o d u c e r - c o n s u m e r a n d s e r v i c e - o r i e n t e d s y s t e m m o d e l s e x p l o i t c o n c u r r e n t e x e c u t i o n o n m u l t i p r o c e s s o r s C o o p e r a t i n g p r o c e s s e s m a y s h a r e d a t a ( g l o b a l s , fj l e s , e t c ) i m p e r a t i v e t o m a i n t a i n d a t a c o r r e c t n e s s Wh y i s d a t a c o r r e c t n e s s i n d a n g e r ? p r o c e s s r u n a s y n c h r o n o u s l y , c o n t e x t s w i t c h e s c a n h a p p e n a t a n y t i m e p r o c e s s e s m a y r u n c o n c u r r e n t l y d i fg e r e n t o r d e r s o f u p d a t i n g s h a r e d d a t a m a y p r o d u c e d i fg e r e n t v a l u e s P r o c e s s s y n c h r o n i z a t i o n t o c o o r d i n a t e u p d a t e s t o s h a r e d d a t a o r d e r o f p r o c e s s e x e c u t i o n s h o u l d n o t a fg e c t s h a r e d d a t a O n l y n e e d e d w h e n p r o c e s s e s s h a r e d a t a ! 2
Producer-Consumer Data Sharing Producer Consumer while (true){ while (true){ /* wait if bufger full */ /* wait if bufger empty */ while (counter == 10) while (counter == 0) ; /* do nothing */ ; /* do nothing */ /* produce data */ /* consume data */ bufger[in] = sdata; sdata = bufger[out]; in = (in + 1) % 10; out = (out + 1) % 10; /* update number of /* update number of items in bufger */ items in bufger */ counter++; counter--; } } 3
Producer-Consumer Data Sharing Producer Consumer while (true){ while (true){ /* wait if bufger full */ /* wait if bufger empty */ while (counter == 10) while (counter == 0) ; /* do nothing */ ; /* do nothing */ /* produce data */ /* consume data */ bufger[in] = sdata; sdata = bufger[out]; in = (in + 1) % 10; out = (out + 1) % 10; /* update number of /* update number of items in bufger */ items in bufger */ R1 = load (counter); R2 = load (counter); R1 = R1 + 1; R2 = R2 – 1; counter = store (R1); counter = store (R2); } } 4
Race Condition S u p p o s e c o u n t e r = 5 Incorrect Sequence 1 Incorrect Sequence 2 R1 = load (counter); R1 = load (counter); R1 = R1 + 1; R1 = R1 + 1; R2 = load (counter); R2 = load (counter); R2 = R2 – 1; R2 = R2 – 1; counter = store (R1); counter = store (R2); counter = store (R2); counter = store (R1); Final Value in counter = 4! Final Value in counter = 6! R a c e c o n d i t i o n i s a s i t u a t i o n w h e r e s e v e r a l p r o c e s s e s c o n c u r r e n t l y m a n i p u l a t e s h a r e d d a t a , a n d s h a r e d d a t a v a l u e d e p e n d s o n t h e o r d e r o f e x e c u t i o n 5
Critical Section Problem R e g i o n o f c o d e i n a p r o c e s s u p d a t i n g s h a r e d d a t a i s c a l l e d a c r i t i c a l r e g i o n . C o n c u r r e n t u p d a t i n g o f s h a r e d d a t a b y m u l t i p l e p r o c e s s e s i s d a n g e r o u s . C r i t i c a l s e c t i o n p r o b l e m h o w t o e n s u r e s y n c h r o n i z a t i o n b e t w e e n c o o p e r a t i n g p r o c e s s e s ? S o l u t i o n t o t h e c r i t i c a l s e c t i o n p r o b l e m o n l y a l l o w a s i n g l e p r o c e s s t o e n t e r i t s c r i t i c a l s e c t i o n a t a t i m e P r o t o c o l f o r s o l v i n g t h e c r i t i c a l s e c t i o n p r o b l e m r e q u e s t p e r m i s s i o n t o e n t e r c r i t i c a l s e c t i o n i n d i c a t e a f t e r e x i t f r o m c r i t i c a l s e c t i o n o n l y p e r m i t a s i n g l e p r o c e s s a t a t i m e 6
Solution to the Critical Section Problem F o r m a l l y s t a t e s , e a c h s o l u t i o n s h o u l d e n s u r e m u t u a l e x c l u s i o n : o n l y a s i n g l e p r o c e s s c a n e x e c u t e i n i t s c r i t i c a l s e c t i o n a t a t i m e p r o g r e s s : s e l e c t i o n o f a p r o c e s s t o e n t e r i t s c r i t i c a l s e c t i o n s h o u l d b e f a i r , a n d t h e d e c i s i o n c a n n o t b e p o s t p o n e d i n d e fj n i t e l y . b o u n d e d w a i t i n g : t h e r e s h o u l d b e a fj x e d b o u n d o n h o w l o n g i t t a k e s f o r t h e s y s t e m t o g r a n t a p r o c e s s ' s r e q u e s t t o e n t e r i t s c r i t i c a l s e c t i o n O t h e r t h a n s a t i s f y i n g t h e s e r e q u i r e m e n t s , t h e s y s t e m s h o u l d a l s o g u a r d a g a i n s t d e a d l o c k s . 7
Preemptive Vs. Non-preemptive Kernels S e v e r a l k e r n e l p r o c e s s e s s h a r e d a t a s t r u c t u r e s f o r m a i n t a i n i n g fj l e s y s t e m s , m e m o r y a l l o c a t i o n , i n t e r r u p t h a n d l i n g , e t c . H o w t o e n s u r e O S e s a r e f r e e f r o m r a c e c o n d i t i o n s ? N o n – p r e e m p t i v e k e r n e l s p r o c e s s e x e c u t i n g i n k e r n e l m o d e c a n n o t b e p r e e m p t e d d i s a b l e i n t e r r u p t s w h e n p r o c e s s i s i n k e r n e l m o d e w h a t a b o u t m u l t i p r o c e s s o r s y s t e m s ? P r e e m p t i v e k e r n e l s p r o c e s s e x e c u t i n g i n k e r n e l m o d e c a n b e p r e e m p t e d s u i t a b l e f o r r e a l - t i m e p r o g r a m m i n g m o r e r e s p o n s i v e 8
Peterson’s Solution to Critical Section Problem S o f t w a r e b a s e d s o l u t i o n O n l y s u p p o r t s t w o p r o c e s s e s T h e t w o p r o c e s s e s s h a r e t w o v a r i a b l e s : i n t t u r n ; i n d i c a t e s w h o s e t u r n i t i s t o e n t e r t h e c r i t i c a l s e c t i o n b o o l e a n fm a g [ 2 ] i n d i c a t e s i f a p r o c e s s i s r e a d y t o e n t e r i t s c r i t i c a l s e c t i o n 9
Peterson's Solution Process 1 Process 0 do { do { fmag[0] = TRUE; fmag[1] = TRUE; turn = 1; turn = 0; while (fmag[1] && turn==1) while (fmag[0] && turn==0) ; ; // critical section // critical section fmag[0] = FALSE; fmag[1] = FALSE; // remainder section // remainder section } while (TRUE) } while (TRUE) S o l u t i o n m e e t s a l l t h r e e r e q u i r e m e n t s P 0 a n d P 1 c a n n e v e r b e i n t h e c r i t i c a l s e c t i o n a t t h e s a m e t i m e i f P 0 d o e s n o t w a n t t o e n t e r c r i t i c a l r e g i o n , P 1 d o e s n o w a i t i n g p r o c e s s w a i t s f o r a t m o s t o n e t u r n o f t h e o t h e r t o p r o g r e s s 1 0
Recommend
More recommend