Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 5

Process and Memory Management

The lifetime of a process is between a fork and an exit (process state-transition diagram, states created 8 to
zombie 9), and is largely spent sleeping, running or waiting to be scheduled
The kernel!s process table holds a list of all processes ps shows most of the information about a "
#ome info about a " is accessible o$er its lifetime and some accessible to the kernel only when the " runs
"rocess table entry fields%
state -running, sleeping,
size and location of u area & pregion entry -
for swapping & switching
'()s user identifiers permitting sending
signals between processes
"()s uni*ue process identifiers
e$ent descriptor - address of e$ent to wakeup
a sleeping process
scheduling parameters determine access to
+"' - system priority % user priority (",(%-()
array of signals not yet handled while
swapped out
execution and kernel utilisation times
u area (user area) fields%
pointer to process table entry
'()s to permit access rights
time spent executing in user and kernel modes
an array containing signal handlers
login terminal associated with the process
error from a system call
return $alue from a system call
(./ parameters% amount of data to transfer,
the address of the source (or target) data array
in user space, file offsets for (./, etc
current and root directory
user file descriptor table records the files the
process has open
size limits of a process and of a file it can
write
permission of files the process creates
Context of a Unix process
0 user-le$el
1 register
2 system-le$el
1. User-level context
0 process text (instructions)
1 data
2 user stack
3 any shared memory
2. Register context
0 program counter - next instruction to run
1 processor status register
2 pointer to user or kernel stacks
3 general registers
3. System-level context:
static part - for the P's lifetime
0 " table entry
1 u area
2 "er "rocess region ("region) entries, region tables, page tables
dynamic part - changes over P's lifetime
0 kernel stack of current layer
1 restorable registers of pre$ious system context layers
4 " mainly runs in user mode, so mainly operates in the user stack 5hen there is a system call or the " is
in$ol$ed in an interrupt, then the " runs in kernel mode and these instructions are protected by operating in
the kernel stack The figure shows the dynamic portion of the context% user and kernel stacks
126782896doc 0
logical pointer
to current
context
layer
+omponents of the +ontext of a "rocess
'ser 9e$el +ontext
Static "art of
#ystem 9e$el +ontext
Text
)ata
#tack
#hared )ata
"rocess Table :ntry
' 4rea
"er"rocess,egion Table
Static "ortion of +ontext
9ayer 1
;ernel
+ontext
9ayer <
9ayer 0
9ayer 2
('ser stack)
;ernel #tack for 9ayer 0
#a$ed ,egister +ontext
for 9ayer <
;ernel #tack for 9ayer 1
#a$ed ,egister +ontext
for 9ayer 0
;ernel #tack for 9ayer 2
#a$ed ,egister +ontext
for 9ayer 1
Dynamic "ortion of
+ontext
%
0
1
0
2
1
0
1
0 0
push push push pop pop pop
;ernel pushes a context layer, on a
0 interrupt
1 system call
2 context switch
kernel pops a context layer when
0 kernel returns from handling interrupt
1 " returns to user mode after system call
2 context switch
+ontext stack of a " can ha$e 6 layers - a push for%
each of the 7 prioritised interrupts, = a system call, =
the user-le$el%
clock interrupt
disk interrupt
tty interrupt
de$ice interrupt
software interrupt
system call
user-le$el
1. Interrupts
are from hardware, software and users
0 push current " context on stack
1 handle interrupt
2 pop (restore pre$ious) context of same
2. System calls
are library functions, which issue an interrupt to
change execution from user mode to kernel mode
1. push current " context on stack
2. handle system call
3. pop (restore pre$ious) context of same
3. Context s!itc"ing
;ernel allows a " to switch context, on%
0 exit
1 sleep
2 return to user mode after a system call,
but another " should run (priority, fairness)
3 return to user mode after an interrupt, ditto
;ernel checks integrity of all data structures and locks
before a switch
0 push current " context on stack
1 determine which " to schedule
2 pop (restore pre$ious) context of t"e sc"e#ule#
126782896doc 1
(nterrupt #e*uence
)isk (nterrupt
#oftware (nterrupt
>ake #ystem +all
:xecuting 'ser >ode
:xample of (nterrupts
;ernel +ontext 9ayer 2
:xecute )isk (nterrupt
?andler
#a$e ,egister +ontext
of #oftware (nterrupt
?andler
;ernel +ontext 9ayer 1
:xecute #oftware (nterrupt
?andler
#a$e ,egister +ontext
of #ys +all
;ernel +ontext 9ayer 0
:xecute #ys +all
#a$e ,egister +ontext
for user layer
'ser +ontext 9ayer <
('ser 9e$el)
'ser +ontext 9ayer <
('ser 9e$el)
Typical +ontext 9ayers of a #leeping "rocess
;ernel +ontext 9ayer 1
:xecute +ode for +ontext
#witch
#a$e ,egister +ontext of
#ys +all
;ernel +ontext 9ayer 0
:xecute #ys +all
#a$e ,egister +ontext
'ser 9e$el
(n$oke #leep 4lgorithm
>ake #ystem +all
:xecuting 'ser >ode
"rocess0 "rocess1 interrupt
(0
(1
(2
(3
suspend (0
& sleep (1
(2
(3
suspend (0
(1
resume (2
5ake & (7
resume (8
(7 suspend
(8 & sleep
(6
(8 5ake &
(9 resume
end (6
(8
(9
(0<
end
126782896doc 2
(0
(1
(2
(3
(7
(8
(6
(8
(9
(<
(0
(0
(1
(2
(3
(0
(1
(2
(3
(7
'ser process (nterrupt.trap
'ser process interrupted by 1
routines in se*uence
(0
(1
(2
(3
(7
(8
(6
(8
(9
(<
(0
(0
(1
(2
(3
(0
(1
(2
'ser process interrupted
(nterrupt routines interrupted
'ser process (nterrupt.trap
(0
(1
(2
(3
(7
(8
(6
(8
(9
(0
(1
(2
(3
(7
(8
(6
(8
(9
(0<
(0
(1
(2
'ser process1 (nterrupt.trap
1 concurrent user processes timeslicing
process1 is interrupted
'ser process0
"rocesses.interrupts running
+ontext switch (nterrupt
"rocess0
"rocess1
"rocess2
"rocess3
"rocesses wait for e$ents
1 processes can be woken by the
same e$ent
;ey input
)isk input
5ait child death
The figure shows many items mentioned abo$e 1 "s share text, ie they run the same code, eg after a fork
The global kernel process table has entries for each "@ each " has its u area@ each " has a "region (a "!s
text, data & stack regions)@ "regions are mapped to the global region table@ which is mapped to $irtual and
on to physical memory
The entries of the region table ha$e the following fields%
size
location of physical memory
type% text, pri$ate data, shared memory or stack
status% locked, used, loading into memory and.or $alid
pointer to the inode (the file) which originally uses the region
count of processes which use the region
The malloc and free + functions and the new and delete +== operators use the brk() system
call to change the data region size
#hared memory system calls (shmget, shmat, shmdt, shmctl) enable communication by
attaching to the same region of memory in the global region table 4ttached memory is accessed $ia
pointers, so communication speed is $ery high, and o$erheads low
fork() creates an identical duplicate process
sproc() creates a lightweight process, which is nearly identical to its parent The new child process
shares the $irtual address space of the parent process, rather than simply being a duplicate The parent and
the child each ha$e their own program counter $alue and stack pointer, but all the text and data regions are
$isible to both processes #uch lightweight process ha$e similar o$erheads to full processes
exec() o$erlays a new process image on an old process The new process image is constructed from an
ordinary, executable file, so is often used to replace the child!s text in a fork() There can be no return from
a successful exec() because the calling process image is o$erlaid by the new process image
pthreads are "/#(A threads 4"( that enable portable parallel processing, see man pthread_create
2 differences between threads and processes%
0 4 " has its own set of state information, eg its own effecti$e user () and set of open file descriptors
Threads exist within a process and do not ha$e distinct copies of these state $alues Threads share the
single state belonging to their process
1 -ormally, each " has a uni*ue address space of memory segments which are accessible only to that
process ("s created with sproc() share address space)
Threads within a process always share the single address space belonging to their process
2 "s are scheduled by the kernel 4 change of process re*uires 1 context switches
126782896doc 3
stack
33;
data
21;
text
1<;
B
4
stack
73;
data
18;
text
1<;
"rocess
memory
,egion table "region table "rocess table
rocess #ata structures
u areas
Threads are scheduled by code that operates largely in the user address space, without kernel assistance
Thread scheduling is faster than process scheduling
4ttribute "/#(A Threads '-(A "rocesses
#ource portability #tandard interface, portable between
$endors (#/.(:+ 9937-0%0998
fork() is a '-(A standard
+reation o$erhead ,elati$ely small Cuite large
Block.'nblock ()ispatch)
/$erhead
Dew microseconds >any microseconds
4ddress space #hared #eparate
>emory-mapped files and
arenas
#hared :xplicit sharing only
>utual exclusion obEects >utexes and condition $ariables@
"/#(A semaphores@ message *ueues
semaphores and locks@ "/#(A
semaphores@ message *ueues
Diles, pipes, and (./ streams #hared single-process file table #eparate file table
#ignal masks and signal
handlers
:ach thread has a mask but handlers
are shared
:ach process has a mask and its own
handlers
,esource limits #ingle-process limits 9imits apply to each process separately
"rocess () /ne "() applies to all threads "() per process
>ulti-threads share the context, but ha$e their own stack
-------------------------------------------------------------------------------------------------------------------------------
$utorial %xercises
0 :xplain what happens to the stacks of a process when the following happen%
the process issues a system call
a time slice
while a process issues a system call, it is interrupted by a software, a de$ice, a tty, a disk and a clock
interrupt
1 :xplain the maEor differences between a 'nix process and a "/#(A thread
126782896doc 7
/ne process
/ne thread
user
stack
kernel
stack
user addrs
space
process
control
block
/ne process
>any threads
user addrs
space
process
control
block
thread
contrl
block
user
stack
kernel
stack
thread
contrl
block
user
stack
kernel
stack
thread
contrl
block
user
stack
kernel
stack
$"rea#s
4 unix process can be said to ha$e
0 thread of execution
"/#(A threads pt"rea#s
are run within a process

You might also like