lecture process creation 6

Upload: ravieengr

Post on 07-Apr-2018

218 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/4/2019 Lecture Process Creation 6

    1/4

    2B10 : Computer Architecture II

    Linux Process Structure

    Adam Greenhalgh

    http://www.cs.ucl.ac.uk/staff/a.greenhalgh/teaching/

    9th March 2004

    Lecture Outline

    Traditional Process Structure

    Comparison with Linux task struct

    Please note hand outs are printed in column format.

    9th March 2004 2/ ??

    What is a process ?

    A Process is an instance of a program in execution.Key Linux files

    include/linux/sched.h

    include/linux/sched.c

    include/asm-i386/processor.h

    include/linux/types.h

    include/asm-i386/posix types.h

    9th March 2004 3/ ??

    Process Control BlockProcess Control Block

    ContainsContains state informationstate information such as:such as:

    P r o c e s s S t a t e P r o c e s s S t a t e

    P r o c e s s I D

    P r o c e s s I D

    P r i o r i t y

    P r i o r i t y

    M e m o r y p o i n t e r s

    M e m o r y p o i n t e r s

    R e s o u r c e s A l l o c a t e d

    R e s o u r c e s A l l o c a t e d

    R e g i s t e r S a v e A r e a

    R e g i s t e r S a v e A r e a

    O w n e r

    O w n e r

    P a r e n t

    P a r e n t

    Scheduling involves saving volatile registers inScheduling involves saving volatile registers in

    one PCB and restoring them from anotherone PCB and restoring them from another

    R e a d y , e t c

    t o r e l a t e I / O , e v e n t s 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 s ( I P C ) , e t c

    o r t i m e s l o t , t i m e t o r u n e t c .

    l o c a t i o n o f p r o c e s s c o d e a n d d a t a ,

    M M U r e g i s t e r s

    t e r m i n a l s , d e v i c e s

    p r o c e s s o r r e g i s t e r s , s t a c k p o i n t e r e t c .

    U s e r I D

    P a r e n t p r o c e s s I D

    Process state

    The C structure task struct defines a process :

    332 s t r u c t t a s k s t ru c t {333 v o l a t i l e l on g s t a te ; /* -1 unrunnable, 0 runnable, >0

    stopped */

    334 s t r u c t t h r e ad i n f o t h r e a d i n f o ;335 a t o m i c t u sa ge ;

    The process state is one of the following :

    106 # defi ne TASK RUNNING 0

    107 # def in e TASK INTERRUPTIBLE 1

    108 # de fi ne TASK UNINTERRUPTIBLE 2

    109 # defi ne TASK STOPPED 4

    110 # defi ne TASK ZOMBIE 8

    111 # defi ne TASK DEAD 16

    ../linux2.6.2/include/linux/sched.h

    9th March 2004 5/ ??

    Process ID

    367 p i d t p i d ;

    368 p i d t p gr p ; /* Accessed via process_group() */

    369 p i d t t t y o l d p g r p ;

    370 p i d t s e s si o n ;

    371 p i d t t g i d ;

    ../linux2.6.2/include/linux/sched.h

    28 t y p e d e f k er ne l p id t p id t ;

    ../linux2.6.2/include/linux/types.h

    14 t y p e d e f i n t k e r n e l p i d t ;

    ../linux2.6.2/include/asm/posix types.h

    An int is 16 bits, hence we have 216 possible process ids.

    9th March 2004 6/ ??

  • 8/4/2019 Lecture Process Creation 6

    2/4

    Priority (1)

    determines process execution order

    set via sys setpriority() system call

    nice() system call has been deprecated.

    341 i n t p ri o , s t a t i c p r i o ;

    342 s t r uc t l i s t he a d r u n l i s t ;

    343 p r i o a r r a y t array ;

    ../linux2.6.2/include/linux/sched.h

    9th March 2004 7/ ??

    Priority (2)268 /*

    269 * Priority of a process goes from 0..MAX_PRIO-1, valid RT

    270 * priority is 0..MAX_RT_PRIO-1, and SCHED_NORMAL tasks are

    271 * in the range MAX_RT_PRIO..MAX_PRIO-1. Priority values

    272 * are inverted: lower p->prio value means higher priority.

    273 *

    274 * The MAX_RT_USER_PRIO value allows the actual maximum

    275 * RT priority to be separate from the value exported to

    276 * user-space. This allows kernel threads to set their

    277 * priority to a value higher than any user task. Note:

    278 * MAX_RT_PRIO must not be smaller than MAX_USER_RT_PRIO.

    279 */

    281 # def ine MAX USER RT PRIO 100

    282 # def ine MAX RT PRIO MAX USER RT PRIO

    284 # d e f i n e MAX PRIO ( MAX R T PRI O + 4 0 )

    ../linux2.6.2/include/linux/sched.h

    9th March 2004 8/ ??

    sys setpriority()

    283 a sm li nk ag e l o ng s y s s e t p r i o r i t y ( i n t w hi ch , i n t who , i n t

    n i c e v a l )

    ../linux2.6.2/kernel/sys.c

    which

    Identifies the group of processes.

    who

    The pid, pgrp or uid field.

    niceval

    The new priority level.

    9th March 2004 9/ ??

    kernel/sys.c

    283 a sm li nk ag e l o ng s y s s e t p r i o r i t y ( i n t w hi ch , i n t who , i n t

    n i c e v a l )284 {285 s t r u c t t a s k s t ru c t g , p ;286 s t r u c t u s e r st r u c t user ;287 s t r u c t p id pid ;288 s t r uc t l i s t h e ad l ;289 i n t e r ro r = EINVAL;

    290

    291 i f ( w h i ch > 2 | | which < 0)292 g o t o o u t ;

    293

    294 /* normalize: avoid signed division (rounding problems) */

    295 e r r o r = ESRCH;

    296 i f ( n i c e v a l < 20)

    297 n i c e v al = 20 ;

    9th March 2004 10/ ??

    298 i f ( n i c e v a l > 19 )

    299 n i c e v a l = 1 9 ;

    300

    301 r e a d l o c k ( & t a s k l i s t l o c k ) ;

    302 s w i t c h ( w h ic h ) {303 cas e PRIO PROCESS:

    304 i f ( ! w ho )

    305 who = c u r r e n t>pid ;

    306 p = f i n d t a s k b y p i d ( who ) ;

    307 i f ( p )

    308 e r r o r = s e t o n e p r i o ( p , n i c ev a l , e r r o r ) ;

    309 break ;

    310 case PRIO PGRP :

    311 i f ( ! w ho )312 who = process group ( cur ren t ) ;

    313 f o r e a c h t a s k p i d ( who , PID TYPE PGI D , p , l , p i d )

    314 e r r o r = s e t o n e p r i o ( p , n i c ev a l , e r r o r ) ;

    315 break ;

    316 case PRIO USER:

    9th March 2004 11/ ??

    317 i f ( ! w ho )

    318 u s er = c u r r e n t>user ;

    319 e l s e

    320 u s er = f i n d u s e r ( who ) ;321

    322 i f ( ! user )

    323 g o to o u t u n l o c k ;

    324

    325 d o e a c h t h r e a d ( g , p )

    326 i f ( p>u i d = = w ho )

    327 e r r o r = s e t o n e p r i o ( p , n i c ev a l , e r r o r ) ;

    328 w h i l e e a c h t h r e a d ( g , p ) ;

    329 break ;

    330 }331 o u t u n l o c k :

    332 r e ad u n lo c k (& t a s k l i s t l o c k ) ;

    333 out :

    334 r e t u r n e r r o r ;

    335 }

    9th March 2004 12/ ??

  • 8/4/2019 Lecture Process Creation 6

    3/4

    Memory Pointers

    358 s t r u c t m m s t ru c t mm, active mm ;

    ../linux2.6.2/include/linux/sched.h

    185 s t r u c t m m st r uc t {

    186 s t r u c t v m a r ea s t r uc t mmap; /* list of VMAs */187 s t r u c t r b r o o t mm rb ;

    188 s t r u c t v m a r ea s t r uc t mmap cache ; /* last find_vma result*/

    189 u n s ig n e d l o n g f r e e a r e a c a c h e ; /* first hole */

    190 p g d t pgd;191 a t o m i c t m m u se rs ; /* How many users with user space?

    */

    192 a t o m i c t m m c ou nt ; /* How many references to "struct

    mm_struct" (users count as 1) */

    193 i n t m ap c o un t ; /* number of VMAs */

    194 st ru ct rw semaphore mmap sem;

    9th March 2004 13/ ??

    195 s p i n l o c k t p a g e t a b le l o c k ; /* Protects task page tables

    and mm->rss */

    196

    197 s t r u c t l i s t h e a d m ml i st ; /* List of all active mms.

    These are globally strung

    198 * together off init_mm.mmlist, and are protected

    199 * by mmlist_lock

    200 */

    201

    202 u ns i gn e d l o n g s t a r t c o d e , e nd c od e , s t a r t d a t a , e n d da t a ;

    203 u ns ig ne d l o ng s t a r t b r k , b rk , s t a r t s t a c k ;

    204 u ns i gn e d l o n g a r g s t a r t , a r g e nd , e n v s t a r t , e nv en d ;

    205 u ns i gn e d l o n g r s s , t o t a l v m , l o ck e d vm ;

    206 u ns i gn e d l o n g d e f f l a g s ;207 cpumask t cpu vm mask;

    208

    209 u n s ig n e d l o n g s a v ed a u xv [ 4 0 ] ; /* for /proc/PID/auxv */

    210

    211 unsigned dumpable :1 ;

    9th March 2004 14/ ??

    212 # i f d e f CONFIG HUGETLB PAGE

    213 i n t u s e d h u ge t l b ;

    214 # e n d i f

    215 /* Architecture-specific MM context */216 m m c o n t ex t t c o n t e x t ;

    217

    218 /* coredumping support */

    219 i n t c o r e w a i t e rs ;

    220 s t r u c t c o m pl e t i on c o r e s t a r t u p d o n e , c o r e d o ne ;221

    222 /* aio bits */

    223 r wl oc k t i o ct x l i s t l o c k ;

    224 s t r u c t k i o c tx i o c t x l i s t ;225

    226 s t r uc t k i o ct x d e f au l t k i o c tx ;

    227 } ;

    ../linux2.6.2/include/linux/sched.h

    9th March 2004 15/ ??

    Resources Allocated417 s t r uc t t t y s t r u c t t t y ; /* NULL if no tty */

    ../linux2.6.2/include/linux/sched.h

    258 s t r uc t t t y s t r u c t {259 i n t m ag ic ;

    260 s t r uc t t t y d r i v e r d r i v e r ;261 i n t i n de x ;

    262 s t r uc t t t y l d i s c l d i s c ;

    263 s t r u c t t e rm i os termios , t e r m i o s l o c k e d ;264 c h a r name [ 6 4 ] ;

    265 i n t p gr p ;

    266 i n t s e s si o n ;

    267 u n si g ne d l o n g f l a g s ;

    268 i n t c o un t ;

    269 s t r u c t w i ns i z e w i n si z e ;

    270 u n s ig n e d c h a r s t o pp e d : 1 , h w s t o pp e d : 1 , f l o w s t o p p e d : 1 ,

    p a c k e t : 1 ;

    9th March 2004 16/ ??

    271 u n s ig n e d c h a r l o w l a t e n c y : 1 , w ar ne d : 1 ;

    272 u ns ig ne d c ha r c t r l s t a t u s ;

    273

    274 s t r uc t t t y s t r u c t l i n k ;275 s t r u c t f a s y nc s t r u c t fasync ;276 s t r uc t t t y f l i p b u f f e r f l i p ;

    277 i n t m a x f l i p c n t ;

    278 i n t a l t s p e ed ; /* For magic substitution of 38400 bps */

    279 w a it q u e ue h e a d t w r i t e w a i t ;

    280 w a i t q u e u e h e a d t r e a d w a i t ;

    281 s t r u c t w o r k s t r u c t h an gu p w or k ;

    282 v o i d d i s c d a t a ;283 v o i d d r i v e r d a t a ;

    284 s t ru c t l i st h e ad t t y f i l e s ;285

    286 # defi ne N TTY BUF SIZE 4096

    287

    288 /*

    289 * The following is data for the N_TTY line discipline.

    9th March 2004 17/ ??

    For

    290 * historical reasons, this is included in the tty

    structure.

    291 */292 u n s ig n e d i n t c ol u mn ;

    293 u n si g ne d c h ar l n e x t : 1 , e r a s i n g : 1 , r aw : 1 , r e a l r a w : 1 , i c a no n

    : 1 ;

    294 u n si g ne d c h ar c l o s i n g : 1 ;

    295 unsigned shor t minimum to wake ;

    296 u n s ig n e d l o n g o v e r r u n t i m e ;

    297 i n t n u m o ve r ru n ;

    298 u n s ig n e d l o n g p r o c es s c h ar m a p [ 2 5 6 / ( 8 s i z e o f ( u n s i g n ed l o n g )) ] ;

    299 char r e a d b u f ;300 i n t r ea d h e ad ;

    301 i n t r e a d t a i l ;

    302 i n t r e a d c nt ;

    303 u n s ig n e d l o n g r e a d f l a g s [ N TT Y BUF SI ZE/ ( 8 s i z e o f ( u n s i g n ed

    long ) ) ] ;

    9th March 2004 18/ ??

  • 8/4/2019 Lecture Process Creation 6

    4/4

    304 i n t c a no n d a ta ;

    305 unsigned long canon head ;

    306 u n s ig n e d i n t c a no n c ol u mn ;

    307 s t r u c t s em ap ho re a t o m i c r e a d ;

    308 s t r u c t s em ap ho re a t o m i c w r i t e ;

    309 s p i n l o c k t r e a d l oc k ;310 /* If the tty has a pending do_SAK, queue it here - akpm */

    311 s t r u c t w o r k s t r u c t SA K work ;

    312 } ;

    ../linux2.6.2/include/linux/tty.h

    9th March 2004 19/ ??

    Register Save Area420 /* CPU-specific state of this task */

    421 s t r u c t t h r e a d s t r u c t t h re a d ;

    ../linux2.6.2/include/linux/sched.h

    406 s t r u c t t h r e a d st r u c t {407 /* cached TLS descriptors. */

    408 s t r u c t d e s c s t r u c t t l s a r r a y [ G DT ENTRY TLS ENTRIES] ;

    409 u n s ig n e d l o n g e sp 0 ;

    410 u ns i gn e d l o n g s y s e n t e r c s ;

    411 u n s ig n e d l o n g e i p ;

    412 u n s ig n e d l o n g e sp ;

    413 u n s ig n e d l o n g f s ;

    414 u n s ig n e d l o n g g s ;

    415 /* Hardware debugging registers */

    416 unsigned long debugreg [ 8 ] ; /* %%db0-7 debug registers */

    417 /* fault info */

    418 u ns i gn e d l o n g c r 2 , t r a p n o , e r r o r c o d e ;

    9th March 2004 20/ ??

    419 /* floating point info */

    420 u ni on i 3 8 7 u n i on i 3 87 ;

    421 /* virtual 86 mode info */

    422 s t r u c t v m8 6 s t ru ct u s er vm86 info ;423 u ns i gn e d l o ng s c r ee n b i t ma p ;

    424 u n s ig n e d l o n g v 8 6 f l a g s , v 86 ma sk , s a ve d es p 0 ;

    425 u ns ig ne d i n t s a ve d f s , s a ve d g s ;

    426 /* IO permissions */

    427 u n s ig n e d l o n g i o b i t m a p p t r ;428 } ;

    ../linux2.6.2/include/asmi386/processor.h

    9th March 2004 21/ ??

    Owner

    404 u i d t u i d , e u i d , s u i d , f s u i d ;

    405 g i d t g i d , e g i d , s g i d , f s g i d ;

    406 i n t n g ro u ps ;

    ../linux2.6.2/include/linux/sched.h

    36 t yp ed ef k e rn el ui d 32 t u i d t ;

    ../linux2.6.2/include/linux/types.h

    16 t yp ed ef u ns ig ne d s ho r t k e r n e l u i d t ;

    ../linux2.6.2/include/asmi386/posix types.h

    9th March 2004 22/ ??

    Parent

    380 s t r u c t t a s k s t ru c t parent ; /* parent process */381 s t r u c t l i s t h e a d c h i l dr e n ; /* list of my children */

    382 s t r u c t l i s t h e a d s i b l i n g ; /* linkage in my parents

    children list */

    9th March 2004 23/ ??