Operating Systems: Process Management
Download
Report
Transcript Operating Systems: Process Management
Process Management
Operating Systems
Lecture 3, 27 March 2003
Mr. Greg Vogl
Uganda Martyrs University
Overview
1.
2.
3.
4.
5.
6.
7.
Monolithic kernel vs. microkernel (lecture 1)
Program, Process, Thread
Interrupts
Process States
Process Scheduling
Processes in UNIX
Processes in Windows
27 March 2003
Operating Systems: Process
Management
2
1. Monolithic Kernel
27 March 2003
Operating Systems: Process
Management
3
Microkernel
27 March 2003
Operating Systems: Process
Management
4
2. Program
Series of commands
Instructions only (usually not data)
Stored on disk, copied into memory
Can be in use by many users and
processes at the same time
Also called software
Includes shell scripts, batch files
27 March 2003
Operating Systems: Process
Management
5
Process
Variables point to instructions + data
Loaded in memory
context contains all process state info.
stored in Process Control Block
One user and one program per process
Also called task
(or job for batch processes)
27 March 2003
Operating Systems: Process
Management
6
Process Context
program counter
register indicating next program instruction to run
other registers
accumulator, index/address, status, general
stack of subroutine return addresses
subroutines call other subroutines
values of local and global variables
pointers to open data files
user and terminal number (in multi-user OS)
27 March 2003
Operating Systems: Process
Management
7
Examples of Processes
User Processes
Shells
Text editors, databases
Background jobs (end with & in UNIX)
System Processes
Memory management, process scheduling
daemons (system background processes)
27 March 2003
Mail and print servers
Operating Systems: Process
Management
8
Thread
Also called sub- or lightweight process
Little private memory; memory is shared
Subdivides work of the process
Threads are managed by the process
Reduces high overhead for creating
processes and context switching
Java was designed to write threadbased programs
27 March 2003
Operating Systems: Process
Management
9
Thread Components
At minimum, every thread has its own
program counter
stack
Program text shared with other threads
Each procedure has a frame to hold its
local variables
Heap of objects is shared by all threads
27 March 2003
Operating Systems: Process
Management
10
Uses of Threads
Servers (database, mail, print, etc.)
one thread per client request
Network server
one thread per connection
Time-sharing
one thread per user
Real-time factory control
one thread per device
27 March 2003
Operating Systems: Process
Management
11
3. Interrupt
Signal hardwareCPU requesting services
CPU postpones its work to handle interrupt
Interrupt handler routines are part of OS code
OS switches modes (usersupervisor)
Interrupts are prioritised
Stack used to store multiple interrupt levels
Programs can mask (ignore) some interrupts
Others unmaskable (to avoid losing data)
27 March 2003
Operating Systems: Process
Management
12
Examples of Interrupts
Key pressed
Disk or other I/O task finished
System clock
27 March 2003
Operating Systems: Process
Management
13
Software Interrupts
Also called traps or exceptions
Processor interrupted by programs
Examples
Division by 0 error
Bus error
Array out of bounds
Buffer overflow
27 March 2003
Operating Systems: Process
Management
14
Context Switching
Preserve state of current process
Start or re-start another process
Performed by dispatcher (OS
component)
When to change context?
Overhead cost (takes CPU time)
Processes prioritised by properties
27 March 2003
Operating Systems: Process
Management
15
4. Process States
Runnable
Running (only one per processor)
Ready (waiting for its turn to run)
Blocked
Explicit e.g. wait() until a child terminates
Implicit e.g. read()
Blocked by another process
New (not yet allowed to wait its turn)
Suspended (e.g. swapped to virtual memory)
Terminated (finished
but still using resources)
Operating Systems: Process
27 March 2003
Management
16
Process State Diagram
Ritchie p. 62
I/O completion
running
termination
resume
suspend
ready
suspended
blocked
suspend
resume
(MLS)
ready
blocked
suspended
I/O completion
27 March 2003
Operating Systems: Process
Management
17
5. Scheduling
Assign each process time to use CPU
Determine sequence (order), timing (when)
Conflicting objectives need compromise
Scheduling levels
High: whether to admit a new process
Medium: suspend/resume a process
Low: dispatch (run) a ready process
27 March 2003
Operating Systems: Process
Management
18
Scheduling Objectives
Maximise throughput
Give all users a “fair” (not equal) chance
Provide tolerable performance
Response time for on-line user
Turnaround time for batch users
Degrade performance gracefully
OK to be slow but avoid complete collapse
Be consistent, predictable over time
27 March 2003
Operating Systems: Process
Management
19
Scheduling Criteria
Priority which may be either/both:
Assigned to job by user
Determined by properties of the job
Class of job (real-time > on-line > batch)
Resources needed (CPU time, memory)
I/O or CPU bound (the aim is a balance)
Resources already used
Time already waited
27 March 2003
Operating Systems: Process
Management
20
Types of Scheduling Policies
Preemptive (Used by most OS today)
OS stops one process to run another
Non-preemptive
process runs until termination or I/O wait
Cooperative (Used by Windows 3.11)
Programs must voluntarily give up CPU
Not managed by OS; trusts programmers
If a process hangs the whole PC hangs
27 March 2003
Operating Systems: Process
Management
21
Scheduling Policies
First come first served (FCFS/FIFO)
Shortest job first (SJF)
Shortest remaining time (SRT)
Highest response ratio next
Round robin (RR)
Multi-level feedback queues (MFQ)
27 March 2003
Operating Systems: Process
Management
22
First come first served
Also called first in first out (FIFO)
Process waiting longest is first in queue
Favours long jobs
high run-time/wait-time ratio
Favours CPU-bound jobs
I/O devices underused
27 March 2003
Operating Systems: Process
Management
23
Shortest job first (next)
Run job with shortest estimated run time
Long jobs may be delayed indefinitely
JCL commands can specify run time
27 March 2003
Operating Systems: Process
Management
24
Shortest remaining time
Run job w/ shortest est. remaining time
Highly favours short jobs
Long jobs will be delayed indefinitely
Requires estimating total run time
Requires measuring elapsed run time
27 March 2003
Operating Systems: Process
Management
25
Highest response ratio next
P = (time waiting + run time) / run time
Priority based on two factors
Favours shorter jobs
Guarantees a job cannot be starved
27 March 2003
Operating Systems: Process
Management
26
Round Robin
Each process given a set time slice
Pre-emption at end of time quantum
Hardware timer generates interrupts
After running, go to back of queue
Used in most interactive operating systems
How large should each time slice be?
Small high context switch overhead
Large user response time is reduced
In practice, about 10-20 ms
27 March 2003
Operating Systems: Process
Management
27
Multi-level feedback queues
Separate queues for different priorities
New process level 1 FIFO (highest)
After timeout level 2 FIFO, etc.
Lowest level is Round Robin
Adapts to past process behaviours
high CPU usage reduced access level
Long processes may starve
if wait time is long, promote/increase slice
27 March 2003
Operating Systems: Process
Management
28
6. UNIX process creation
PID 0: sched (process scheduler)
PID 1: init (ancestor of other processes)
daemons (automatic, for sys. admin.)
getty (one per terminal)
login (lets users log in)
shell (lets users run programs)
user-initiated processes (run from shell)
27 March 2003
Operating Systems: Process
Management
29
UNIX fork()
fork() produces new “child” process
Child is exact copy of parent
Starts in same program, at same place
Exact copy of memory space
(globals, stack, heap object)
Both calls to fork() return a value
Parent fork() returns process ID of child
Child fork() returns 0
27 March 2003
Operating Systems: Process
Management
30
UNIX exec()
Execute (load, run) another program
Overwrite calling process in memory
Uses same PID; not a new process
Used after fork() to start new process
Parent waits for child process to finish
Run in background to not hold up shell
myprogram &
27 March 2003
Operating Systems: Process
Management
31
UNIX scheduling
Varies with the flavour of UNIX
Often dynamic priority, round robin, MFQ
Processes have number priority values
0=highest, 60=lowest, 20=initial/default
Users can reduce priority using nice
nice -10 myprog
# makes priority 30
Only superuser can increase priority
nice --10 myprog
27 March 2003
# makes priority 10
Operating Systems: Process
Management
32
UNIX ps
Command to display process status
ps # list PIDs, TTYs, CPU time, name
ps -a # all terminal-created processes
ps -e # everything (incl. daemons)
ps -f # full list (more details)
ps -l # long list (ppid, priority, size, nice)
27 March 2003
Operating Systems: Process
Management
33
Other UNIX commands
top – process list, continuously updated
jobs – list jobs running in current shell
bg, fg – send jobs to back/foreground
at – run batch job at specified time(s)
kill – send signal to terminate process
sleep – pause for some seconds
27 March 2003
Operating Systems: Process
Management
34
7. Processes in Windows
Every process treated as thread
Process can create additional threads
Scheduler operates over all threads
Each process has virtual address space
No parent-child process relationship
Environment copied to new processes
27 March 2003
Operating Systems: Process
Management
35
Dynamic Link Libraries (DLLs)
Executable code routines
Linked into application only at run time
Can be shared by several programs
27 March 2003
Operating Systems: Process
Management
36
Starting Programs
Type program name (maybe full path)
MS-DOS prompt, run dialog box, Explorer
Double-click icon
Desktop, Explorer, My Computer
Click shortcut
Start or Programs menu, QuickLaunch
Start when the computer starts
autoexec.bat, config.sys, Startup menu
27 March 2003
Operating Systems: Process
Management
37
Managing Tasks
Ways to close programs
Ctrl-C; File, Exit; Close button (X); Alt-F4
Press Ctrl-Alt-Del to view task manager
End Task: stop non-responding program
Ctrl-Alt-Del again to shut down/restart
27 March 2003
Operating Systems: Process
Management
38
WinMe System Information
Loaded modules (exe, dll, etc.)
Name, version, size, file date, mfg., path
Running tasks
Name, path, PID, priority, version, size
Startup programs
Program, command, user name, location
Print jobs
27 March 2003
Operating Systems: Process
Management
39