linux lecture 7

63
1 Linux Operating System

Upload: dhakshana-murthi

Post on 16-Feb-2018

222 views

Category:

Documents


0 download

TRANSCRIPT

7/23/2019 Linux Lecture 7

http://slidepdf.com/reader/full/linux-lecture-7 1/63

1

Linux Operating System

許 富 皓

7/23/2019 Linux Lecture 7

http://slidepdf.com/reader/full/linux-lecture-7 2/63

2

Chapter 3

Processes

7/23/2019 Linux Lecture 7

http://slidepdf.com/reader/full/linux-lecture-7 3/63

3

How Processes Are Organized –

(1) The runqueue ists group a processes in a

TASK_RUNNING state!

Processes in aTASK_STOPPED

"EXIT_ZOMBIE

"

or EXIT_DEAD state are not in#ed in speci$ic

ists! There is no need to group processes in any o$ these

three states" %ecause stopped" zom%ie" and deadprocesses are accessed ony &ia PID 

or

&ia in#ed ists o$ the chid processes $or a particuar parent!

7/23/2019 Linux Lecture 7

http://slidepdf.com/reader/full/linux-lecture-7 4/63

4

How Processes Are Organized –

(') Processes in a TASK_INTERRUPTIBLE or

TASK_UNINTERRUPTIBLE state are

su%di&ided into many casses" each o$ which

corresponds to a speci$ic event !

n this case" the process state does not

pro&ide enough in$ormation to retrie&e the

process uic#y" so it is necessary tointroduce additiona lists of processes!

These are caed wait queues!

7/23/2019 Linux Lecture 7

http://slidepdf.com/reader/full/linux-lecture-7 5/63

5

*ait +ueue

Wait queues impement conditional waits onevents, a process wishing to wait $or a speci$ic e&ent

paces itse$ in the proper wait ueue  and reinuishes contro!

There$ore" a wait ueue represents a set ofsleeping processes" which are wo#en up %y the

#erne when some condition %ecomes true! The condition coud %e reated to,

an interrupt" such as $or a dis# operation to terminate process synchronization

timing, such as a $ixed inter&a o$ time to eapse

7/23/2019 Linux Lecture 7

http://slidepdf.com/reader/full/linux-lecture-7 6/63

6

*ait +ueue mpementation

Wait queues are impemented as doublylinked lists whose eements incude pointersto process descriptors!

-ach wait ueue is identi$ied %y a wait queuehead " a data structure o$ type 

 wait_queue_head_t:

strut __wait_queue_head

! s"i#$%&_t $%&'strut $ist_head tas&_$ist'

  ('  t)"ede* strut __wait_queue_head   +  wait_queue_head_t' 

for synchronization

the head of a

list of waiting processes

7/23/2019 Linux Lecture 7

http://slidepdf.com/reader/full/linux-lecture-7 7/63

7

*ait +ueue Synchronization

Since wait ueues are modi$ied%y interrupt handlers 

as we%y ma.or  kernel functions"

the dou%y in#ed ists must %e protected $rom

concurrent accesses" which coud induceunpredicta%e resuts!

Synchronization is achie&ed %y the $%& spin

lock  in the wait queue head !

7/23/2019 Linux Lecture 7

http://slidepdf.com/reader/full/linux-lecture-7 8/63

8

-ements o$ a wait ueue ist are o$ type 

 wait_queue_t. 

strut __wait_queue!u#si,#ed i#t *$a,s'strut tas&_strut - tas&'

  wait_queue_*u#_t *u#'strut $ist_head tas&_$ist'('

  t)"ede* strut __wait_queue wait_queue_t' 

/ata Structure o$ -ements o$ a

*ait +ueue

7/23/2019 Linux Lecture 7

http://slidepdf.com/reader/full/linux-lecture-7 9/63

9

-ach eement in the wait ueue ist

represents a sleeping process" which iswaiting $or some e&ent to occur0 its

descriptor address is stored in the tas& 

$ied!

The tas&_$ist $ied contains the pointers

that in# this eement to the list of

 processes waiting $or the same e&ent!

tas& ied and tas&_$ist ied

o$ a *ait +ueue -ement

7/23/2019 Linux Lecture 7

http://slidepdf.com/reader/full/linux-lecture-7 10/63

10

Wake up All Sleeping Processes in

a Wait Queue ? Howe&er" it is not aways con&enient to

wa#e up a seeping processes in a wait

ueue!or instance"

i$ two or more processes are waiting $or exclusive

access to some resource to %e reeased" it ma#es

sense to wa#e up .ust one process in the wait

ueue!

This process ta#es the resource" whie the other

processes continue to seep!

7/23/2019 Linux Lecture 7

http://slidepdf.com/reader/full/linux-lecture-7 11/63

11

Thundering Herd

2utipe seeping processes are awo#en

ony to race $or a resource that can %e

accessed %y one o$ them" and the resut isthat remaining processes must once more

%e put %ac# to seep!

*aste CPU time!

7/23/2019 Linux Lecture 7

http://slidepdf.com/reader/full/linux-lecture-7 12/63

12

Seeping Process Types

exclusive processes (denoted %y the &aue 1 in

the *$a,s $ied o$ the corresponding wait ueue

eement) are seecti&ey wo#en up %y the #erne!

nonexclusive processes (denoted %y the &aue

0 in *$a,s) are aways wo#en up %y the #erne when the e&ent

occurs!

7/23/2019 Linux Lecture 7

http://slidepdf.com/reader/full/linux-lecture-7 13/63

13

-xampes o$ /i$$erent Seeping

Process Types

 A process waiting $or a resource that can %e

granted to .ust one process at a time is a typicaexcusi&e process!

Processes waiting $or an e&ent i#e the

termination o$ a dis# operation are nonexcusi&e! 

7/23/2019 Linux Lecture 7

http://slidepdf.com/reader/full/linux-lecture-7 14/63

14

*u#  ied o$ a *ait +ueue -ement

the *u# $ied o$ a wait ueue eement is

used to speci$y how the processes

seeping in the wait ueue shoud %ewo#en up!

7/23/2019 Linux Lecture 7

http://slidepdf.com/reader/full/linux-lecture-7 15/63

15

/ecare a 3ew *ait +ueue Head

 A new wait ueue head may %e de$ined %y using theDE.LARE_/AIT_0UEUE_1EAD2#a3e4 macro" which staticay decares a new wait ueue head &aria%e caed #a3e  and

initiaizes its $%& and tas&_$ist $ieds!

 5de*i#e __/AIT_0UEUE_1EAD_INITIALIZER2#a3e4 ! +6$%& 7 SPIN_LO.K_UNLO.KED8 +

6tas&_$ist 7 ! 92#a3e46tas&_$ist8 92#a3e46tas&_$ist ( (

5de*i#e DE.LARE_/AIT_0UEUE_1EAD2#a3e4 +

 wait_queue_head_t#a3e7__/AIT_0UEUE_1EAD_INITIALIZER2#a3e4 

7/23/2019 Linux Lecture 7

http://slidepdf.com/reader/full/linux-lecture-7 16/63

16

-xampe

$%&

tas&_$ist #e:t

 

 "re;

DE.LARE_/AIT_0UEUE_1EAD2dis&_wait_queue4

dis&_wait_queue

7/23/2019 Linux Lecture 7

http://slidepdf.com/reader/full/linux-lecture-7 17/63

17

nitiaize a *ait +ueue -ement

The i#it_waitqueue_e#tr)2q8"4 $unction initiaizes a wait_queue_t structureq  as $oows,

 q<=*$a,s 7 >'

q<=tas& 7 "'

q<=*u# 7 de*au$t_wa&e_*u#ti%#'

 The nonexcusi&e process " wi %e awa#ened %yde*au$t_wa&e_*u#ti%#2 4" which is asimpe wrapper  $or the tr)_t%_wa&e_u"2 4 $unction discussed in 4hapter 5! 

7/23/2019 Linux Lecture 7

http://slidepdf.com/reader/full/linux-lecture-7 18/63

18

/ecare a 3ew *ait +ueue -ement

 Aternati&ey" the DE?INE_/AIT macro, decares a new wait_queue_t &aria%e! initiaizes it with the descriptor o$ the process currenty

executing on the CPU.  initiaizes it with the address o$ theaut%re3%;e_wa&e_*u#ti%#2 4wake-up function! This $unction

in&o#es de*au$t_wa&e_*u#ti%#2 4 to awa#en the seeping

process  and then remo&es the wait ueue eement $rom the wait ueue ist!

inay" a #erne de&eoper can de$ine a custom awakeningfunction %y initiaizing the wait ueue eement with the

i#it_waitqueue_*u#_e#tr)2 4 $unction!

7/23/2019 Linux Lecture 7

http://slidepdf.com/reader/full/linux-lecture-7 19/63

19

flags

task 

func

task_list next

  prev

-xampe o$ a *ait +ueue

lock 

task_list next

  prev

flags

task 

func

task_list next

  prev

process

descriptor

process

descriptor

dis&_wait_queue

7/23/2019 Linux Lecture 7

http://slidepdf.com/reader/full/linux-lecture-7 20/63

20

unctions to Add67emo&e

-ements $rom a *ait +ueue Once an eement is de$ined" it must %e inserted into a

wait ueue! The add_wait_queue( ) $unction inserts a nonexcusi&e

process in the first  position o$ a wait ueue ist! The add_wait_queue_e:$usi;e( ) $unction inserts an

excusi&e process in the last  position o$ a wait ueue ist!

The re3%;e_wait_queue( ) $unction remo&es thecorresponding wait ueue eement o$ a process $rom a

wait ueue ist! The waitqueue_ati;e( ) $unction chec#s whether a

gi&en wait ueue ist is empty!

7/23/2019 Linux Lecture 7

http://slidepdf.com/reader/full/linux-lecture-7 21/63

21

unctions That 4an Put a Process

to a *ait +ueue  A process wishing to wait $or a speci$ic condition 

can in&o#e any o$ the $unctions shown in the

$oowing ist!  s$ee"_%#2 4

i#terru"ti@$e_s$ee"_%#2 4

s$ee"_%#_ti3e%ut2 4

i#terru"ti@$e_s$ee"_%#_ti3e%ut2 4 wait_e;e#t and wait_e;e#t_i#terru"ti@$e 

macros

7/23/2019 Linux Lecture 7

http://slidepdf.com/reader/full/linux-lecture-7 22/63

22

The s$ee"_%#() $unction

;%id s$ee"_%#2wait_queue_head_t -wq4

! wait_queue_t wait'

 

i#it_waitqueue_e#tr)29wait8 urre#t4'

urre#t<=state 7 TASK_UNINTERRUPTIBLE'add_wait_queue2wq89wait4'

- wq "%i#ts t% the wait queue head -

shedu$e2 4're3%;e_wait_queue2wq8 9wait4'

(

(1) Remove urre#t from the runqueue.

() !n order to make shedu$e24 resume its execution"

there must #e some other kernel control path setting this

 process #ack to TASK_RUNNING state and putting it

 #ack to the runqueue after (1) is executed.

7/23/2019 Linux Lecture 7

http://slidepdf.com/reader/full/linux-lecture-7 23/63

23

i#terru"ti@$e_s$ee"_%#2 4 

unction The i#terru"ti@$e_s$ee"_%#2 4 

$unction is identica to s$ee"_%#2 4" except

that it sets the state o$ the current process toTASK_INTERRUPTIBLE instead o$ setting it

to TASK_UNINTERRUPTIBLE" so that the

process aso can %e wo#en up %y recei&ing a signal !

7/23/2019 Linux Lecture 7

http://slidepdf.com/reader/full/linux-lecture-7 24/63

24

unctions ncude Timers

The s$ee"_%#_ti3e%ut2 4 and

i#terru"ti@$e_s$ee"_%#_ti3e%ut2 4 

$unctions are simiar to the pre&ious ones!

8ut they aso aow the caer to de$ine a time

interval  a$ter which the process wi %e wo#en

up %y the #erne!

To do this" they in&o#e the shedu$e_ti3e%ut2 4 $unction instead o$ shedu$e2 4 ! P!S!, See the section 9An Appication o$ /ynamic Timers, the

#a#%s$ee"2 4 System 4a9 in 4hapter :!

7/23/2019 Linux Lecture 7

http://slidepdf.com/reader/full/linux-lecture-7 25/63

25

unction "re"are_t%_wait2 4" 

and *i#ish_wait2 4  The "re"are_t%_wait2 4

 "re"are_t%_wait_e:$usi;e2 4  and*i#ish_wait2 4 $unctions

  " introduced in Linux '!:" o$$er yet anotherway to put the current process to seep ina wait queue!

7/23/2019 Linux Lecture 7

http://slidepdf.com/reader/full/linux-lecture-7 26/63

26

  DE?INE_/AIT2wait4' "re"are_t%_wait_e:$usi;e29wq8 9wait8 TASK_INTERRUPTIBLE4'- wq is the head %* the wait queue -

C 666C i* 2%#diti%#4 FC shedu$e24' Path C

C *i#ish_wait29wq8 9wait4' Path C F

The "re"are_t%_wait2 4 and "re"are_t%_wait_e:$usi;e2 4 $unctions, set the process state to the &aue passed as the third parameter! set the exclusive flag  in the wait ueue eement respecti&ey to ; (nonexcusi&e) or 1

(excusi&e)! insert the wait ueue eement wait into the ist o$ the wait ueue head wq !

 As soon as the process is awa#ened" it executes the *i#ish_wait2 4 $unction" unction *i#ish_wait2 4C 

sets the process state to TASK_RUNNING (.ust in case the awa#ing condition %ecomestrue %e$ore in&o#ing shedu$e2 4)!

remo&es the wait queue element  $rom the wait queue list  (uness this has aready %eendone %y the wake-up function)!

 "re"are_t%_wait_e:$usi;e2 4

 *i#ish_wait2 4 

7/23/2019 Linux Lecture 7

http://slidepdf.com/reader/full/linux-lecture-7 27/63

27

 wait_e;e#t and

 wait_e;e#t_i#terru"ti@$e The wait_e;e#t and wait_e;e#t_i#terru"ti@$e macros put the

caing process to seep on a wait ueue unti a gi&encondition is &eri$ied!

or instance" the wait_e;e#t2wq8%#diti%#4 macroessentiay yieds the $oowing $ragment,

  DE?INE_/AIT2__wait4'*%r 2''4! "re"are_t%_wait29wq8 9__wait8 TASK_UNINTERRUPTIBLE4'i* 2%#diti%#4 @rea&'shedu$e2 4'*i#ish_wait29wq8 9__wait4'

  (

*i#ish_wait29wq8 9__wait4' 

Remove urre#t from the

runqueue.

7/23/2019 Linux Lecture 7

http://slidepdf.com/reader/full/linux-lecture-7 28/63

28

4omparisons %etween the a%o&e

unctions (1)

The s$ee"_%#2 4<i#e $unctions cannot %e

used in the common situation where one

has to test a condition and atomicay putthe process to seep when the condition is

3OT &eri$ied0 there$ore" %ecause they are a

we<#nown source o$ race conditions" their

use is /S4O=7A>-/!

7/23/2019 Linux Lecture 7

http://slidepdf.com/reader/full/linux-lecture-7 29/63

29

4omparisons %etween the a%o&e

unctions (')

2oreo&er" in order to insert an excusi&e

process into a wait ueue" the #erne must

ma#e use o$ the "re"are_t%_wait_e:$usi;e2 4 $unction

  or 

 .ust in&o#e

add_wait_queue_e:$usi;e2 4 directy!

 Any other heper $unction inserts the

process as nonexcusi&e!

7/23/2019 Linux Lecture 7

http://slidepdf.com/reader/full/linux-lecture-7 30/63

30

4omparisons %etween the a%o&e

unctions (?)

inay" uness *i#ish_wait2 4 are

used" the #erne must remo&e the wait

ueue eement $rom the ist a$ter thewaiting process has %een awa#ened!

7/23/2019 Linux Lecture 7

http://slidepdf.com/reader/full/linux-lecture-7 31/63

31

*a#e up Seeping Processes

The #erne awa#ens processes in the wait

ueues" putting them in the TASK_RUNNING 

state" %y means o$ one o$ the $oowing macros,

 wa&e_u"" wa&e_u"_#r" 

 wa&e_u"_a$$" 

 wa&e_u"_i#terru"ti@$e"

 wa&e_u"_i#terru"ti@$e_#r"

 wa&e_u"_i#terru"ti@$e_a$$"

 wa&e_u"_i#terru"ti@$e_s)#" and

 wa&e_u"_$%&ed !

7/23/2019 Linux Lecture 7

http://slidepdf.com/reader/full/linux-lecture-7 32/63

32

 wa&e_u" 2acro

the wa&e_u" macro is essentiay eui&aent

to the $oowing code $ragment,

;%id wa&e_u"2wait_queue_head_t -q4

!strut $ist_head -t3"'

 wait_queue_t -urr'

$ist_*%r_eah2t3"8 9q<=tas&_$ist4

 !urr 7 $ist_e#tr)2t3"8 wait_queue_t8 tas&_$ist4'  i*2urr<=*u#2urr8TASK_INTERRUPTIBLEHTASK_UNINTERRUPTIBLE

  8 >8 NULL4 99 urr<=*$a,s4

 @rea&'

(

(

7/23/2019 Linux Lecture 7

http://slidepdf.com/reader/full/linux-lecture-7 33/63

33

-xpanation o$ 2acro wa&e_u"  –

(1) The $ist_*%r_eah macro scans a items in the q<=tas&_$ist dou%y in#ed ist" that is" a processes inthe wait ueue!

or each item" the $ist_e#tr) macro computes theaddress o$ the corresponding wait_queue_t &aria%e! The *u# $ied o$ this &aria%e stores the address o$ the wa#e<up

$unction" which tries to wa#e up the process identi$ied %y thetas& $ied o$ the wait ueue eement!

$ a process has %een e$$ecti&ey awa#ened (the $unctionreturned 1) and i$ the process is excusi&e(urr<=*$a,s eua to 1)" the oop terminates!

7/23/2019 Linux Lecture 7

http://slidepdf.com/reader/full/linux-lecture-7 34/63

34

-xpanation o$ 2acro wa&e_u"  –

(') 8ecause a nonexclusive processes are

aways at the %eginning o$ the dou%y

in#ed ist and a exclusive processes are at the end" the $unction awayswa#es the nonexcusi&e processes

  and then wa#es O3- excusi&e process" i$ any

exists!

7/23/2019 Linux Lecture 7

http://slidepdf.com/reader/full/linux-lecture-7 35/63

35

Process 7esource Limits

-ach process has an associated set o$

resource imits" which speci$y the

amount o$ system resources it can use! These imits #eep a user $rom

o&erwheming the system (its CPU" dis#

space" and so on)!

7/23/2019 Linux Lecture 7

http://slidepdf.com/reader/full/linux-lecture-7 36/63

36

Locations That Store the

7esources Limits o$ a Process The resource imits $or the current process are stored in

the urre#t<=si,#a$<=r$i3  $ied" that is" in a $iedo$ the process@s signal descriptor !

P!S!, See the section 9/ata Structures Associated with Signas9in 4hapter 11! strut r$i3it r$i3RLIM_NLIMITSJ' The $ied is an array o$ eements o$ type strut r$i3it" one

$or each resource imit,

  strut r$i3it! u#si,#ed $%#, r$i3_ur'u#si,#ed $%#, r$i3_3a:'

(' 

7/23/2019 Linux Lecture 7

http://slidepdf.com/reader/full/linux-lecture-7 37/63

37

 RLIMIT_AS and RLIMIT_.ORE 

RLIMIT_AS The maximum size o$ process address space" in %ytes! The #erne chec#s this &aue when the process uses 3a$$%2 4 

or a reated $unction to enarge its address space! P!S!, See the section 9The Process@s Address Space9 in 4hapter !

RLIMIT_.ORE The maximum core dump file size" in %ytes! The #erne chec#s this &aue when a process is a%orted" %e$ore

creating a core $ie in the current directory o$ the process! P!S!, See the section 9Actions Per$ormed upon /ei&ering a Signa9 in

4hapter 11!

$ the imit is ;" the #erne won@t create the $ie!

7/23/2019 Linux Lecture 7

http://slidepdf.com/reader/full/linux-lecture-7 38/63

38

RLIMIT_.PU and RLIMIT_DATA 

RLIMIT_.PU The maximum CPU time $or the process" in seconds! $ the process exceeds the imit" the #erne sends it a

SIGX.PU signa" and then" i$ the process doesn@tterminate" a SIGKILL signa! P!S!, see 4hapter 11!

RLIMIT_DATA  The maximum heap size" in %ytes! The #erne chec#s this &aue %e$ore expanding the

heap o$ the process! P!S!, See the section 92anaging the Heap9 in 4hapter !

7/23/2019 Linux Lecture 7

http://slidepdf.com/reader/full/linux-lecture-7 39/63

39

RLIMIT_?SIZE and RLIMIT_LO.KS

RLIMIT_?SIZEThe maximum $ie size aowed" in %ytes!

$ the process tries to enarge a $ie to a sizegreater than this &aue" the #erne sends it a

SIGX?SZ signa!

RLIMIT_LO.KS2aximum num%er o$ $ie oc#s (currenty" not

en$orced)!

7/23/2019 Linux Lecture 7

http://slidepdf.com/reader/full/linux-lecture-7 40/63

40

RLIMIT_MEMLO.K and RLIMIT_MSG0UEUE

RLIMIT_MEMLO.K The maximum size o$ nonswappable memory " in %ytes!

The #erne chec#s this &aue when the process tries tooc# a page $rame in memory using the 3$%&2 4 or

 3$%&a$$2 4 system cas P!S!, See the section 9Aocating a Linear Address nter&a9 in

4hapter !

RLIMIT_MSG0UEUE2aximum num%er o$ %ytes in POSIX message queues!

P!S!, See the section 9POSB 2essage +ueues9 in 4hapter 1!

7/23/2019 Linux Lecture 7

http://slidepdf.com/reader/full/linux-lecture-7 41/63

41

RLIMIT_NO?ILE and RLIMIT_NPRO.

RLIMIT_NO?ILE The maximum num%er o$ open file descriptors!

The #erne chec#s this &aue when opening a new $ieor dupicating a $ie descriptor (see 4hapter 1')!

RLIMIT_NPRO. The maximum num%er o$ processes that the user can

own! P!S!, see the section 9The $%#e2 4" *%r&2 4" and

;*%r&2 4 System 4as9 ater in this chapter!

7/23/2019 Linux Lecture 7

http://slidepdf.com/reader/full/linux-lecture-7 42/63

42

RLIMIT_STA.K and RLIMIT_SIGPENDING

RLIMIT_RSSThe maximum num%er o$ page frames owned

%y the process (currenty" not en$orced)! RLIMIT_SIGPENDINGThe maximum num%er o$ pending signas $or

the process! P!S!, See 4hapter 11!

7/23/2019 Linux Lecture 7

http://slidepdf.com/reader/full/linux-lecture-7 43/63

43

RLIMIT_STA.K

RLIMIT_STA.KThe maximum stac# size" in %ytes!

The #erne chec#s this &aue %e$oreexpanding the =ser 2ode stac# o$ the

process! P!S!, See the section 9Page aut -xception

Hander9 in 4hapter !

7/23/2019 Linux Lecture 7

http://slidepdf.com/reader/full/linux-lecture-7 44/63

44

strut r$i3it

The r$i3_ur $ied is the current resource

limit  $or the resource!

or exampe" urre#t<=si,#a$<=r$i3RLIMIT_.PUJ6r$i3_ur represents

the current imit on the CPU time o$ the running

process!

The r$i3_3a: $ied is the maximum

allowed value $or the resource imit!

7/23/2019 Linux Lecture 7

http://slidepdf.com/reader/full/linux-lecture-7 45/63

45

ncrease the r$i3_ur o$ Some

7esource 8y using the ,etr$i3it2 4 and

setr$i3it2 4 system cas" a user can

aways increase the r$i3_ur o$ someresource up to r$i3_3a:!

Howe&er" ony the superuser  can increase

ther$i3_3a:

 $ied or set ther$i3_ur

 

$ied to a &aue greater than the

corresponding r$i3_3a: $ied!

7/23/2019 Linux Lecture 7

http://slidepdf.com/reader/full/linux-lecture-7 46/63

46

RLIM_IN?INIT

2ost resource imits contain the &aue

RLIM_IN?INIT 2>:********4" which

means that no user imit is imposed on the

corresponding resource! P!S!, O$ course" rea imits exist due to #erne design

restrictions" a&aia%e RAM" a&aia%e space on dis#"

etc!!

Howe&er" the system administrator may chooseto impose stronger imits on some resources!

INIT_RLIMITS 

7/23/2019 Linux Lecture 7

http://slidepdf.com/reader/full/linux-lecture-7 47/63

47

o! the "esource #imits of a $ser

Process Are Set? *hene&er a user ogs into the system" the #erne

creates a process owned %y the superuser" whichcan in&o#e setr$i3it2 4 to decrease the

r$i3_3a: and r$i3_ur $ieds $or a resource! The same process ater executes a login shell  and

%ecomes owned %y the user! -ach new process created %y the user inherits the

content o$ the r$i3  array $rom its parent" andthere$ore the user cannot o&erride the imits en$orced%y the administrator!

7/23/2019 Linux Lecture 7

http://slidepdf.com/reader/full/linux-lecture-7 48/63

48

Process Switch

To contro the execution o$ processes" the #ernemust %e a%e to suspend the execution o$ the process running on the

CPU  and resume the execution o$ some other process

pre&iousy suspended!

This acti&ity goes &ariousy %y the names process switch task switch

  or  context switch!

7/23/2019 Linux Lecture 7

http://slidepdf.com/reader/full/linux-lecture-7 49/63

49

Hardware 4ontext

*hie each process can ha&e its own addressspace" a processes ha&e to share the CPU registers!

So %e$ore resuming the execution o$ a process" the#erne must ensure that each such register is oadedwith the &aue it had when the process wassuspended!

The set o$ data that must %e oaded into the registers%e$ore the process resumes its execution on theCPU is caed the hardware context  !

7/23/2019 Linux Lecture 7

http://slidepdf.com/reader/full/linux-lecture-7 50/63

50

Hardware 4ontext 7epositories

The hard!are context  is a su%set o$ the

 process execution context " which incudes

a in$ormation needed $or the process

execution!

n Linux" a part o$ the hardware context o$

a process is stored in the process

descriptor " whie the remaining part is

sa&ed in the Kernel Mode stack !

7/23/2019 Linux Lecture 7

http://slidepdf.com/reader/full/linux-lecture-7 51/63

51

Process Switch and Hardware 4ontext

 Assumptions, oca &aria%e "re; re$ers to the process

descriptor o$ the process %eing switched out!

#e:t re$ers to the one %eing switched in to

repace it!

 A process s!itch can %e de$ined as the

acti&ity consisting o$ sa&ing the hardwarecontext o$ "re; and repacing it with the

hardware context o$ #e:t!

7/23/2019 Linux Lecture 7

http://slidepdf.com/reader/full/linux-lecture-7 52/63

52

The Pace *here Process Switches Occur 

Process switching occurs only in Cerne

2ode!

The contents o$ a registers used %y aprocess in =ser 2ode ha&e aready %een

sa&ed %e$ore per$orming process switching!This incudes the contents o$ the ss and esp 

pair that speci$ies the =ser 2ode stac# pointeraddress!

7/23/2019 Linux Lecture 7

http://slidepdf.com/reader/full/linux-lecture-7 53/63

53

Tas# State Segment in Linux

The D; x D: architecture incudes a speci$ic

segment type caed the Task tate egment  

(TSS)" to store hardware contexts!

8ut Linux doesn@t use TSS $or hardwarecontext switches!

Howe&er Linux is nonetheess $orced to set up

a TSS $or each distinct CPU in the system!

7/23/2019 Linux Lecture 7

http://slidepdf.com/reader/full/linux-lecture-7 54/63

54

Tas# State Segment 4omponents

=sed %y Linux *hen an D; x D: CPU switches $rom =ser

2ode to Cerne 2ode" it $etches the address

of the Kernel Mode stack  $rom the TSS! *hen a =ser 2ode process attempts to

access an IO port %y means o$ an i# or %ut 

instruction" the CPU may need to access an

IO !ermission "itmap stored in the TSS to&eri$y whether the process is aowed to

address the port!

7/23/2019 Linux Lecture 7

http://slidepdf.com/reader/full/linux-lecture-7 55/63

55

tss_strut Structure

The tss_strut structure descri%es the$ormat o$ the TSS!

The i#it_tss array  stores one TSS $or

each CPU on the system!  At each process switch" the #erne updates

some $ieds o$ the TSS so that thecorresponding CPU@s control unit  may sa$ey

retrie&e the in$ormation it needs! 8ut there is no need to maintain TSSs $or

processes when they@re not running!

7/23/2019 Linux Lecture 7

http://slidepdf.com/reader/full/linux-lecture-7 56/63

56

Tas# State Segment /escriptor 

-ach TSS has its own D<%yte Task tateegment #escriptor  (TSSD)!

This descriptor incudesa ?'<%it Base *ie$d  that points to the TSS 

starting addressa ';<%it Li3it *ie$d !

The S $ag o$ a TSSD is ceared to denote the $actthat the corresponding TSS is a ystem egment ! P!S!, see the section 9Segment /escriptors9 in 4hapter '!

The T)"e $ied is set to either or 11 to denote thatthe segment is actuay a TSS!

7/23/2019 Linux Lecture 7

http://slidepdf.com/reader/full/linux-lecture-7 57/63

57

8usy 8it

n the nte@s origina design" each process in the

system shoud re$er to its own TSS!

The second east signi$icant %it o$ the T)"e $ied (E

%its) o$ the corresponding TSSD is caed the "usy bit 1, i$ the process is %eing executed %y a CPU 

;, otherwise!

%n #inux design& there is 'ust one T  for each$!% & so the (usy bit is al!ays set to )*

7/23/2019 Linux Lecture 7

http://slidepdf.com/reader/full/linux-lecture-7 58/63

58

TSSD<reated 7egisters

The TSSDs created %y Linux are stored in the&lobal #escriptor Table (GDT)" whose %aseaddress is stored in the !"t#  register o$ each CPU!

The t#  register o$ each CPU contains the TSSD Seector o$ the corresponding TSS! The register aso incudes two hidden" nonprogramma%e

$ieds, the Base and Li3it *ie$ds o$ the TSSD! n this way" the processor can address the TSS directy

without ha&ing to retrie&e the TSS address $rom theGDT!

7/23/2019 Linux Lecture 7

http://slidepdf.com/reader/full/linux-lecture-7 59/63

59

TSSD and i#it_tss  Array

 Linux’s GDT Linux’s GDT 

 "er<.PU i#it_tss

#<

de*au$t_$dt

7/23/2019 Linux Lecture 7

http://slidepdf.com/reader/full/linux-lecture-7 60/63

60

The thread  ied

 At e&ery process switch" the hardware context o$ theprocess %eing repaced must %e sa&ed somewhere!

t cannot %e sa&ed on the TSS" as in the origina

nte design" %ecause Linux uses a singe TSS $oreach processor" instead o$ one $or e&ery process!

Thus" each process descriptor  incudes a $iedcaed thread  o$ type thread_strut" in which

the #erne sa&es the hardware context whene&er theprocess is %eing switched out!

7/23/2019 Linux Lecture 7

http://slidepdf.com/reader/full/linux-lecture-7 61/63

61

O&er&iew o$ the thread_sturt 

/ata structure thread_sturt  incudes

$ieds $or most o$ the CPU registers"

except the genera<purpose registers suchas e$%" e&%" etc!" which are stored in the

Kernel Mode stack !

7/23/2019 Linux Lecture 7

http://slidepdf.com/reader/full/linux-lecture-7 62/63

62

Where Could a Process S!itch

+ccur? A process switch may occur at .ust one

we<de$ined point, the shedu$e2 4 

$unction" which is discussed at ength in4hapter 5!

7/23/2019 Linux Lecture 7

http://slidepdf.com/reader/full/linux-lecture-7 63/63

Per$orming the Process Switch

-ssentiay" e&ery process switch consistso$ two steps,

Switching the !age &lobal #irectory  to instaa new address space! P!S!, *e@ descri%e this step in 4hapter !

Switching the Kernel Mode stack  and the

hardware context " which pro&ides a thein$ormation needed %y the #erne to executethe new process" incuding the CPU registers!