Processes and Threads - School of Computing and Engineering

Download Report

Transcript Processes and Threads - School of Computing and Engineering

Processes and Threads
Tanenbaum 2.1, 2.2
Crowley Chapters 3, 5
Stallings Chapter 3, 4
Silberschaz & Galvin 3, 4
cs431-cotter
1
Process Management
• A program is an algorithm or a plan
• A process is a program in execution
– dynamic
– program is static
• process == job
• Process includes:
–
–
–
–
program counter
stack
data section
etc.
cs431-cotter
2
The Process Model
Figure 2-1. (a) Multiprogramming of four programs. (b) Conceptual
model of four independent, sequential processes. (c) Only
one program is active at once.
cs431-cotter
3
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
Process Creation
Events which cause process creation:




System initialization.
Execution of a process creation system call by a
running process.
A user request to create a new process.
Initiation of a batch job.
cs431-cotter
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
4
Process Termination
Events which cause process termination:




Normal exit (voluntary).
Error exit (voluntary).
Fatal error (involuntary).
Killed by another process (involuntary).
cs431-cotter
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
5
Two State Process Model
• Simplest concept of process management
– running
– not running
• Dispatcher can switch between jobs in the
queue.
– Works well if all jobs in the queue are always ready to
execute.
– If not, additional states are needed.
cs431-cotter
6
Process States
Figure 2-2. A process can be in running, blocked, or ready state.
cs431-cotter Transitions between these states are as shown.
7
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
Process State Diagram
• States: New, Running, Waiting, Ready,
Terminated
new
terminated
admitted
interrupt
ready
exit
running
dispatch
I/O comp
cs431-cotter
waiting
I/O req
8
Process State Diagram
(alternate view)
• States: New, Running, Ready, suspended,
Blocked, Terminated
new
terminated
admitted
interrupt
ready
exit
dispatch
running
event occurs
event wait
activate
suspend
suspend
blocked
activate
cs431-cotter
9
Implementation of Processes (1)
Figure 2-3. The lowest layer of a process-structured operating
system handles interrupts and scheduling. Above that layer
cs431-cotter
10
are sequential processes.
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
Process Schedulers
Simple system
CPU
Ready queue
Next Level of detail
Ready queue
CPU
wait queue
cs431-cotter
11
Process Schedulers
Realistic System
job queue
CPU
ready queue
I/O
I/O queue
wait queue
time slice
expired
cs431-cotter
child
executes
fork child
interrupt
occurs
wait for
interrupt
12
Process Schedulers
• Long Term Scheduler
– Controls loading of processes into the ready queue.
– Determines the degree of multi-programming allowed
in the system (how many processes are active at one
time)
– Operates infrequently (seconds)
• Short Term Scheduler
– Controls loading of processes into CPU.
– Operates more frequently (milliseconds)
cs431-cotter
13
Implementation of Processes (2)
Figure 2-4. Some of the fields of a typical process table entry.
cs431-cotter
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
14
Process Control Block
process
pointer
state
process number
program counter
registers
memory limits
open files
Etc...
cs431-cotter
15
Process Switching
Context Switching
Process 1
PCB
Operating
System
Process 2
PCB
Save to PCB1
Reload PCB2
Save to PCB2
Reload PCB1
cs431-cotter
16
Implementation of Processes
Interrupt Processing
Figure 2-5. Skeleton of what the lowest level of the operating
system does when an interrupt occurs.
cs431-cotter
17
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
Modeling Multiprogramming
Figure 2-6. CPU utilization as a function of the number of
processes in memory.
cs431-cotter
18
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
The Classical Thread Model (1)
Figure 2-11. (a) Three processes each with one thread. (b) One
process with three threads.
cs431-cotter
19
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
The Classical Thread Model (2)
Figure 2-12. The first column lists some items shared by all
threads in a process. The second one lists some items private
to each thread.
cs431-cotter
20
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
The Classical Thread Model (3)
Figure 2-13. Each thread has its own stack.
cs431-cotter
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
21
Thread Usage (1)
Figure 2-7. A word processor with three threads.
cs431-cotter
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
22
Thread Usage (2)
Figure 2-8. A multithreaded Web server.
cs431-cotter
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
23
Thread Usage (3)
Figure 2-9. A rough outline of the code for Fig. 2-8. (a) Dispatcher
thread. (b) Worker thread.
cs431-cotter
24
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
Thread Usage (4)
Figure 2-10. Three ways to construct a server.
cs431-cotter
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
25
Thread Types
• Kernel Threads
– Small data structure / stack.
– Thread switching does not affect memory access info
– relatively fast
• User Threads
– Only need to switch PC and stack info
– Operate above (and independent of) kernel
– very fast
• Hybrid (both)
– Uses LWP to package user threads for kernel control
cs431-cotter
26
Implementing Threads in User Space
Figure 2-16. (a) A user-level threads package. (b) A threads
package managed by the kernel.
cs431-cotter
27
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
Hybrid Implementations
cs431-cotter
Figure 2-17. Multiplexing user-level threads
onto kernel-level threads.
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
28
Threads in Solaris 2
task 1
task 2
user
thread
task 3
LWP
kernel
thread
kernel
CPU
cs431-cotter
29
Pop-Up Threads
Figure 2-18. Creation of a new thread when a message arrives.
(a) Before the message arrives.
(b) After the message arrives.
cs431-cotter
30
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
Summary
• Operating systems are designed to run
processes
– They provide a framework to run multiple processes
in parallel, efficiently
– Processes are implemented to be independent of
each other
• Processes may have multiple threads
– Threads share a process context
– Threads often interact with each other
cs431-cotter
31
Questions
• What is the difference between a thread and a process. Given that a
task might be implemented using either a new thread or a new
process, discuss some of the factors that might suggest one form
over the other. (When would a process be good and when would a
thread be good?)
• What is the difference between a kernel thread and a user space
thread? What is the advantage of a user space thread? What is its
disadvantage?
• What types of events might move a process from a running state to
a ready state?
• Why is it more efficient to run (schedule) multiple processes in
parallel rather than to just run them as serial (sequential)
processes?
• What types of events might preempt a thread that is executing in the
CPU? (What events might make it change state?)
cs431-cotter
32