Download Colour version (for viewing)

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Unix security wikipedia , lookup

Distributed operating system wikipedia , lookup

DNIX wikipedia , lookup

Burroughs MCP wikipedia , lookup

Process management (computing) wikipedia , lookup

Transcript
Operating Systems
Part of E1.9 - “Principles of Computers and Software
Engineering”
Lecture 3: Introduction to process management
Objectives
“ To
introduce the concepts of:
“ Process
state and process control block
“ Process scheduling
“ Threads
“ To
describe the steps involved in:
“ Process
creation
“ Process termination
“ Process switching
E1.9 - Operating systems (Lect 3)
2
Process control block (1)
“
Previous lecture defined process as:
“
“
Program in execution
Consisting of
z
z
z
z
“
“
Instructions
Data
Stack and context
Process attributes
The state of a process is defined by its current
activity (e.g. running, waiting)
Process is represented using a process control block.
E1.9 - Operating systems (Lect 3)
3
Process Control Block (2)
“
Typical Process Control Block (PCB) includes:
“
Process state
“
“
“
“
Program counter
CPU registers
CPU-scheduling information
“
“
e.g values of base and limit registers
Accounting information
“
“
e.g process priority
Memory management information
“
“
e.g. running, waiting
e.g process number
I/O status information
“ e.g list of I/O devices allocated and list of open files
E1.9 - Operating systems (Lect 3)
4
Process States (1)
Two-state model (1)
dispatch
Process
Creation
Not-running
running
Process
Termination
pause
Processes either running or not-running
“ Processes created as not-running
“ After completion, processes are terminated
“
E1.9 - Operating systems (Lect 3)
5
Process States (1)
Two-state model (2)
Process management in the two-state model:
Selecting a process for running
“ Dispatching of a process
“
“
“
“
Move process from not-running to running state
Restore its context
Pausing of a process
“
“
Move process from running to not-running state
Save its context
E1.9 - Operating systems (Lect 3)
6
Process States (1)
Two-state model (3)
Creation
Termination
Not-running-queue
PN
PN-1
…
P2
P1
dispatch
Processor
pause
Process scheduling decisions:
“
“
Which process to pick next from the not-running
queue?
When is a process paused?
E1.9 - Operating systems (Lect 3)
7
Process States (2)
Five-state model (1)
“
Two state model: some processes in the not-running
queue might not actually be ready to execute
“
“
“
Need to separate not running processes in more
categories:
“
“
“
“
e.g waiting for I/O
Dispatching of such process is a waste of processor time
New: process was just created but has not yet entered the
system
Waiting: process is waiting for I/O
Ready: process is ready to execute
Result: Five state model
E1.9 - Operating systems (Lect 3)
8
Process States (2)
Five-state model (2)
Five-state model:
“
Process
Creation
New
dispatch
admitted
exit
ready
running
pause
I/O or event
completion
“
Waiting
Process
Termination
Terminated
I/O or
event
wait
Three queues: ready, new, waiting
E1.9 - Operating systems (Lect 3)
9
Process States (3)
Seven-state model (1)
“
Five-state model:
“
“
“
“
All processes (including those waiting for I/O) are loaded in
memory
Problem if memory is limited
We can store (suspend) some of processes in secondary
storage
Need to add new two categories of process states:
“
“
“
Ready-suspend: Process is ready to execute (not waiting for
I/O) but is suspended to secondary storage
Waiting-suspend: Process is waiting for I/O and has been
suspended to secondary storage.
Seven-state model takes into account these new
states
E1.9 - Operating systems (Lect 3)
10
Process States (3)
Seven-state model (2)
Process
Creation
New
dispatch
admitted
exit
ready
running
pause
Load from disk
ready-suspend I/O or event
Terminated
I/O or
event
wait
Waiting
completion
Load from
disk
Process
Termination
Suspend to
disk
Waiting-suspend
E1.9 - Operating systems (Lect 3)
11
Process States (3)
Seven-state model (3)
“
Seven state model illustrates the need for three
different types of scheduling
“
Long term scheduling:
“
“
“
Medium-term scheduling
“
“
Determine whether process is admitted to system
Determine whether process is placed to ready or ready-suspend
queue
Determine when to move a process to/from the suspended state
Short-term or CPU scheduling
“
“
Determine when to interrupt a process currently running (note:
this is different to processes interrupted by other events (e.g.
hardware interrupt)).
Determine which process from the ready queue to run next.
E1.9 - Operating systems (Lect 3)
12
Process States (4)
Scheduling queues and queuing diagrams (1)
“
Scheduling queues
“
“
Need even more queues – apart from new, ready
and waiting, now need new, ready, waiting,
waiting-suspend, ready-suspend.
Also useful to have various I/O device queues to
place processes waiting for a specific device (one
per device).
Process scheduling moves processes
between queues
“ Queuing diagrams
“
E1.9 - Operating systems (Lect 3)
13
Process States (4)
Scheduling queues and queuing diagrams (2)
Creation
Ready-queue
PN
I/O1
I/O2
Termination
PN-1
…
P2
P1
Blocked-waiting-for-I/O1 queue
B1
B2
…
Bm-1
Bm
Blocked-waiting-for-I/O2 queue
C1
C2
…
Cm-1 Cm
dispatch
Processor
pause
Request I/O1
Request I/O2
(Example of a (partial)
queuing diagram)
E1.9 - Operating systems (Lect 3)
14
Process States (5)
Process Tables
OS maintains process tables
that hold the PCBs.
“ One entry per PCB
“ Pointer entry in PCB points to
the next PCB in the queue.
“
Example: The ready queue and various I/O device queues
[Silberschatz et al 2001]
E1.9 - Operating systems (Lect 3)
15
Operations on Processes(1)
Overview
“
Major process operations used in the diagrams:
“
“
“
Process creation
Process termination
Process switch
E1.9 - Operating systems (Lect 3)
16
Operations on Processes(2)
Process creation (1)
“
Process creation
“
“
“
“
“
“
Create new PCB
Allocate memory for instructions, data, stack
Load instructions and data into allocated memory
Initialise stack and context
Initialise PCB
When?
“
“
“
“
System initialisation
Execution of a process-creation system call by a running
process (e.g. POSIX fork, Win32 CreateProcess)
User creates a new process (e.g. through the shell)
Initiation of a batch job
E1.9 - Operating systems (Lect 3)
17
Operations on Processes(2)
Process creation (2)
The creating process is called parent process;
new processes are called children of this
process
“ Possibilities after creation:
“
“
In terms of execution
“
“
“
Parent continues execution concurrently with the children
Parent waits for some or all of children to terminate in
order to continue.
In terms of address space of the new process:
“
“
Child process is a duplicate of the parent process
Child process has a new program loaded into it
E1.9 - Operating systems (Lect 3)
18
Operations on Processes(3)
Process termination
“
Process termination
“
“
“
Release resources allocated to process
Delete PCB entries
When?
“
“
“
“
Completion of task (normal, voluntary exit)
Exit due to error, e.g division by zero, I/O error, protection
error.
Exit due to termination of parent process (cascading
termination)
Termination by the OS, e.g if a resource is unavailable or its
use has exceeded accounting limits.
E1.9 - Operating systems (Lect 3)
19
Operations on Processes(4)
Process switch
“
Process or context switch
“
“
“
Context switch time is pure overhead
“
“
Kernel saves context (e.g registers, pointers,
memory management information etc) into the
process’ PCB
Loads context of the new process scheduled to
run
can become substantial depending on whether
parts of memory must be saved too.
Hardware support can reduce this overhead
(e.g by providing multiple sets of registers).
E1.9 - Operating systems (Lect 3)
20
Threads(1)
Need for
“
Processes encompass two concepts:
“
“
“
Threads:
“
“
“
“
“
“
Code execution
Resource grouping
Can be thought at lightweight processes
Share code section, data section and OS resources (e.g
open files) with other threads within the process
Have own program counter, register set and stack.
Allow several threads of execution within the same process
Avoid context switching overheads
But no protection (e.g. memory) between threads
E1.9 - Operating systems (Lect 3)
21
Threads(2)
How? (1)
“
User-level threads: Threads library (e.g POSIX
pthreads)
“
“
“
“
“
Each process starts with a single thread present.
A thread can create new threads by calling library
procedures, e.g thread_create.
Parent thread typically specifies the name of a procedure for
the new thread to run
Parent thread gets identifier for the new thread.
Advantages
“
“
Fast to create and manage
Disadvantages
“
“
Have to write own scheduling algorithm
Difficult to handle with blocking system calls
E1.9 - Operating systems (Lect 3)
22
Threads(2)
How? (2)
“
Kernel-level threads
“
“
Kernel performs creation, scheduling and
management in kernel space.
Disadvantages:
“
“
Advantages
“
“
“
Slower to create and manage than user-level threads
Can easily handle threads with blocking system calls
In multiprocessor environments, kernel can schedule
threads on different processors
Recycling threads
E1.9 - Operating systems (Lect 3)
23
Threads(4)
Benefits
“
Responsiveness
“
“
“
Economy
“
“
Interactive processes can perform a lengthy
operation in a separate thread, while they handle
user’s input in another thread
Example: Multithreaded Web browser
Allocating memory and resources for processes is
costly
Utilization of multiprocessor architectures
E1.9 - Operating systems (Lect 3)
24
Process management
What next?
“
Next three lectures will examine issues introduced today
“
“
“
First: Process scheduling
Second: Process synchronization
Third: Deadlocks
E1.9 - Operating systems (Lect 3)
25
Summary
Process management
Process state models include two-, five- and seven-state models
“ The steps involved in process creation, termination, and switch
were presented.
“ Threads are used to allow for multiple executions to take place
within the same process.
“
Next lecture:
Process scheduling
E1.9 - Operating systems (Lect 3)
26
Recommended Reading
“
Read chapter 4 and section 5.1 of Silberschatz’s book
“
Read chapter 2, sections 2.1-2.2.5 of Tanenbaum’s book
Rest of chapter 2 will be covered in later lectures
“
Read chapter 3, and section 4.1 of Stallings’ book
E1.9 - Operating systems (Lect 3)
27