chapter 4 threads outline

Chapter 4: Threads Outline Wh a t a r e t h r e a d s - PowerPoint PPT Presentation

Chapter 4: Threads Outline Wh a t a r e t h r e a d s ? H o w d o t h e y c o m p a r e w i t h p r o c e s s e s ? Wh y a r e t h r e a d s i m p o r t a n t ? Wh a t


  1. Chapter 4: Threads – Outline  Wh a t a r e t h r e a d s ? H o w d o t h e y c o m p a r e w i t h p r o c e s s e s ?  Wh y a r e t h r e a d s i m p o r t a n t ?  Wh a t a r e t h e c o m m o n m u l t i t h r e a d i n g m o d e l s ?  Wh a t a r e t h e c o m m o n m u l t i t h r e a d i n g l i b r a r i e s ?  D i s c u s s i o n o n t h r e a d i n g i s s u e s .  E x a m p l e s o f t h r e a d s i n c o n t e m p o r a r y O S e s .

  2. Process Overview  T h e b a s i c u n i t o f C P U u t i l i z a t i o n i s a p r o c e s s .  T o r u n a p r o g r a m ( a s e q u e n c e o f i n s t r u c t i o n s ) , c r e a t e a p r o c e s s .  P r o c e s s p r o p e r t i e s  fork() → exec() c a n b e u s e d t o s t a r t n e w p r o g r a m e x e c u t i o n  p r o c e s s e s a r e w e l l p r o t e c t e d f r o m e a c h o t h e r  c o n t e x t - s w i t c h i n g b e t w e e n p r o c e s s e s i s f a i r l y e x p e n s i v e  i n t e r - p r o c e s s c o m m u n i c a t i o n u s e d f o r i n f o r m a t i o n s h a r i n g a n d c o - o r d i n a t i o n b e t w e e n p r o c e s s e s  s h a r e d m e m o r y  m e s s a g e p a s s i n g

  3. Is the Process Abstraction Always Suitable ?  C o n s i d e r c h a r a c t e r i s t i c s f o r a g a m e s o f t w a r e  d i fg e r e n t c o d e s e q u e n c e s f o r d i fg e r e n t g a m e o b j e c t s  s o l d i e r s , c i t i e s , a i r p l a n e s , c a n n o n s , u s e r - c o n t r o l l e d h e r o e s , e t c .  e a c h o b j e c t i s m o r e o r l e s s i n d e p e n d e n t  P r o b l e m s  s i n g l e m o n o l i t h i c p r o c e s s m a y n o t u t i l i z e r e s o u r c e s o p t i m a l l y  c a n c r e a t e a p r o c e s s f o r e a c h o b j e c t  a c t i o n o f a n o b j e c t d e p e n d s o n g a m e s t a t e  s h a r i n g a n d c o - o r d i n a t i o n o f i n f o r m a t i o n n e c e s s a r y  I P C i s e x p e n s i v e  n u m b e r o f o b j e c t s p r o c e e d s i m u l t a n e o u s l y  m a y i n v o l v e l o t s o f c o n t e x t s w i t c h e s  p r o c e s s c o n t e x t s w i t c h e s a r e e x p e n s i v e

  4. Is the Process Abstraction Always Suitable ? (2)  A b i l i t y t o r u n m u l t i p l e s e q u e n c e s o f c o d e ( t h r e a d s o f c o n t r o l ) f o r d i fg e r e n t o b j e c t  i n d i v i d u a l p r o c e s s o n l y o fg e r s o n e t h r e a d o f c o n t r o l  Wa y f o r t h r e a d s o f c o n t r o l t o s h a r e d a t a e fg e c t i v e l y  p r o c e s s e s N O T d e s i g n e d t o d o t h i s  P r o t e c t i o n b e t w e e n t h r e a d s o f c o n t r o l n o t v e r y i m p o r t a n t  a l l i n o n e a p p l i c a t i o n , a n y w a y !  p r o c e s s i s a n o v e r - k i l l  S w i t c h i n g b e t w e e n t h r e a d s o f c o n t r o l m u s t b e e ffj c i e n t  c o n t e x t s w i t c h i n g i n v o l v e s a l o t o f o v e r h e a d  D i fg e r e n t t h r e a d s o f c o n t r o l m a y s h a r e m o s t i n f o r m a t i o n  p r o c e s s e s d u p l i c a t e e n t i r e a d d r e s s s p a c e

  5. Threads to the Rescue  T h r e a d s a r e d e s i g n e d t o a c h i e v e a l l t h e a b o v e r e q u i r e m e n t s !  d o a s l i t t l e a s p o s s i b l e t o a l l o w e x e c u t i o n o f a t h r e a d o f c o n t r o l  T h r e a d a r e k n o w n a s a l i g h t w e i g h t p r o c e s s  o n l y t h e n e c e s s a r y c o n t e x t i n f o r m a t i o n i s r e - g e n e r a t e d  t h r e a d - c o n t e x t : P C , r e g i s t e r s , s t a c k , o t h e r m i s c . i n f o  p r o c e s s - c o n t e x t : a l s o i n c l u d e s d a t a a n d c o d e r e g i o n s  t h r e a d s a r e e x e c u t e d w i t h i n a p r o c e s s  c o d e a n d d a t a s h a r e d a m o n g d i fg e r e n t t h r e a d s  r e d u c e d c o m m u n i c a t i o n o v e r h e a d  s m a l l e r c o n t e x t  f a s t e r c o n t e x t s w i t c h i n g  a s i n g l e a d d r e s s s p a c e f o r a l l t h r e a d s i n a p r o c e s s  r e d u c e d i n t e r - t h r e a d p r o t e c t i o n

  6. Thread Basics  T h r e a d – a l i g h t w e i g h t p r o c e s s  h a v e t h e i r o w n i n d e p e n d e n t fm o w o f c o n t r o l  s h a r e p r o c e s s r e s o u r c e s w i t h o t h e r s i b l i n g t h r e a d s  e x i s t w i t h i n t h e c o n t e x t s p a c e o f t h e s a m e p r o c e s s  T  T h r e a d s s h a r e d d a t a h r e a d s s p e c i fj c d a t a  p  t r o c e s s i n s t r u c t i o n s h r e a d i d  m  r o s t d a t a e g i s t e r s , s t a c k p o i n t e r  o  t p e n fj l e s ( d e s c r i p t o r s ) h r e a d - s p e c i fj c d a t a  s i g n a l s a n d s i g n a l h a n d l e r s ( s t a c k o f a c t i v a t i o n r e c o r d s )  c  s u r r e n t w o r k i n g d i r e c t o r y i g n a l m a s k  u  s s e r a n d g r o u p i d c h e d u l i n g p r o p e r t i e s  r e t u r n v a l u e

  7. Single and Multithreaded Process source: https://computing.llnl.gov/tutorials/pthreads/

  8. Thread Benefits  R e s p o n s i v e n e s s  f o r a n i n t e r a c t i v e u s e r , i f p a r t o f t h e a p p l i c a t i o n i s b l o c k e d  R e s o u r c e S h a r i n g  e a s i e r , v i a m e m o r y s h a r i n g  b e a w a r e o f s y n c h r o n i z a t i o n i s s u e s  E c o n o m y  s h a r i n g r e d u c e s c r e a t i o n , c o n t e x t - s w i t c h i n g , a n d s p a c e o v e r h e a d  S c a l a b i l i t y  c a n e x p l o i t c o m p u t a t i o n a l r e s o u r c e s o f a m u l t i c o r e C P U

  9. Thread Programming In Linux  T h r e a d s c a n b e c r e a t e d u s i n g t h e P t h r e a d s l i b r a r y  I E E E P O S I X C l a n g u a g e t h r e a d p r o g r a m m i n g i n t e r f a c e  m a y b e p r o v i d e d e i t h e r a s u s e r - l e v e l o r k e r n e l - l e v e l  P t h r e a d s A P I  T h r e a d m a n a g e m e n t – f u n c t i o n s t o c r e a t e , d e s t r o y , d e t a c h , j o i n , s e t / q u e r y t h r e a d a t t r i b u t e s  M u t e x e s – f u n c t i o n s t o e n f o r c e s y n c h r o n i z a t i o n . C r e a t e , d e s t r o y , l o c k , u n l o c k m u t e x e s  C o n d i t i o n v a r i a b l e s – f u n c t i o n s t o m a n a g e t h r e a d c o m m u n i c a t i o n . C r e a t e , d e s t r o y , w a i t a n d s i g n a l b a s e d o n s p e c i fj e d v a r i a b l e v a l u e s

Recommend


More recommend