You are on page 1of 33

Lecture 3

Processes&

Threads
24 August, 2010

1
what is a process?

2
resource + execution

3
typical content of a process control block:

registers, state, priority, pid, parent


program counters, program status word
CPU time used
pointers to memory segments
working directory
opened files
user ID, group ID

etc.
4
OS maintains
process table
(one PCB / process)

5
OS saves and
restores PCBs to
context switch
between processes
6
when to context switch?

7
blocked I/O
e.g. Java InputStreamʼs read( )

8
interrupt
( I/O or Timer )
e.g. time allocated to a process is used up
or when data arrives over the network

9
ready running

new blocked exit

10
which ready process to
run next ?

11
what causes a new
process to be created?

12
system initialization
(e.g, Linux init process)

13
explicit creation through
system call
(e.g, fork( ), CreateProcess( ))

14
upon user requests
(e.g., double click an icon, typing a command)

15
what causes a process
to terminate ?

16
finish running
(with or without error)

17
fatal error
(an example from Lab 1)

18
killed by another
process
(e.g., kill( ) , TerminateProcess( ))

19
recall: process-related
system calls

fork, exec, wait, exit

20
fork wait

exec exit

21
consider a
Web browser

22
23
consider a
Web server

24
concurrent multi-process server

while (1)
block until new connection
fork( )
if (is child process)
handle new connection
exit( )

25
fork( ) is expensive
(do we really need to duplicate all the
resources ?)

26
threads
same resource, different executions

27
a multi-threaded process

global variables, files,


children, address space

stack stack stack


state state state
registers registers registers

28
advantages of
multi-threading

29
vs single-threaded
improved responsiveness
exploits parallelism
abstraction for “independent”
sequence of execution
30
vs multi-process

cheaper
allows sharing of resources

31
POSIX Threads API
   pthread_create(  )
   pthread_exit(  )
   pthread_join(  )
   pthread_yield(  )

32
thread scheduling
done by either
process or kernel

33

You might also like