process scheduling iii. processes - uni konstanz · process scheduling 3. assignment 2. assignment...
TRANSCRIPT
Operating Systems • Prof. Dr. Marc H. Scholl • DBIS • U KN • Summer Term 2009
III.Process Scheduling
1 Operating Systems • Prof. Dr. Marc H. Scholl • DBIS • U KN • Summer Term 2009
Intended Schedule
Date Lecture Hand out Submission0 20.04. Introduction to Operating Systems Course reggistration1 27.04. Systems Programming using C (File Subsystem) 1. Assignment2 04.05. Systems Programming using C (Process Control) 2. Assignment 1. Assignment3 11.05. Process Scheduling 3. Assignment 2. Assignment4 18.05. Process Synchronization 4. Assignment 3. Assignment5 25.05. Inter Process Communication 5. Assignment 4. Assignment6 01.06. Pfingstmontag 6. Assignment 5. Assignment7 08.06. Input / Output 7. Assignment 6. Assignment8 15.06. Memory Management 8. Assignment 7. Assignment9 22.06.
Filesystems9. Assignment 8. Assignment
10 29.06.Filesystems
10. Assignment 9. Assignment11 06.07. Special subject: Transactional Memory 10. Assignment12 13.07. Special subject: XQuery your Filesystem13 20.07. Wrap up session
27.07. First examination daate12.10. Second examination date
2
Operating Systems • Prof. Dr. Marc H. Scholl • DBIS • U KN • Summer Term 2009
Processes
3 Silberschatz, Galvin and Gagne ©2005Operating Systems • Prof. Dr. Marc H. Scholl • DBIS • U KN • Summer Term 2009
An operating system executes a variety of programs:
Batch system – jobs
Time-shared systems – user programs or tasks
Textbooks use the terms job and process almost interchangeably
Process – a program in execution; process execution must progress in sequential fashion
For the OS, processes are the unit of all resource allocation
A process includes:
program counter
stack
data section
4
Process in memory
Silberschatz, Galvin and Gagne ©2005Operating Systems • Prof. Dr. Marc H. Scholl • DBIS • U KN • Summer Term 2009
Information associated with each process
Process state
Program counter
CPU registers
CPU scheduling information
Memory-management information
Accounting information
I/O status information
5 Silberschatz, Galvin and Gagne ©2005Operating Systems • Prof. Dr. Marc H. Scholl • DBIS • U KN • Summer Term 2009
As a process executes, it changes state
new: The process is being created
running: Instructions are being executed
waiting: The process is waiting for some event to occur
ready: The process is waiting to be assigned to a process
terminated: The process has finished execution
6
Silberschatz, Galvin and Gagne ©2005Operating Systems • Prof. Dr. Marc H. Scholl • DBIS • U KN • Summer Term 2009
At any time, processes can be described as either:
I/O-bound – spends more time doing I/O than computations, many short CPU bursts
CPU-bound – spends more time doing computations; few very long CPU bursts
typical processes follow a CPU–I/O burst cycle
Process execution consists of a cycle of CPU execution and I/O wait
Operating system tries to maximize CPU utilization: multiprogramming
run multiple processes and try to interleave their CPU-I/O burst cycles
Scheduling
7
Single Process
Operating Systems • Prof. Dr. Marc H. Scholl • DBIS • U KN • Summer Term 2009
Process Switching
8
Silberschatz, Galvin and Gagne ©2005Operating Systems • Prof. Dr. Marc H. Scholl • DBIS • U KN • Summer Term 2009
When CPU switches to another process, the system must save the state of the old process and load the saved state for the new process
Context-switch time is overhead; the system does no „useful work“ while switching
Time dependent on hardware support
9 Silberschatz, Galvin and Gagne ©2005Operating Systems • Prof. Dr. Marc H. Scholl • DBIS • U KN • Summer Term 2009
Long-term scheduler (or job scheduler) – selects which processes should be brought into the ready queue
controls the degree of multiprogramming
invoked very infrequently (sec, min) may be slow
Short-term scheduler (or CPU scheduler) – selects which process should be executed next and allocates CPU
invoked very frequently (msec) must be fast
Medium-term scheduler – swaps running processes out
10
Silberschatz, Galvin and Gagne ©2005Operating Systems • Prof. Dr. Marc H. Scholl • DBIS • U KN • Summer Term 2009
Job queue – set of all processes in the system
Ready queue – set of all processes residing in main memory, ready and waiting to execute
Device queues – set of processes waiting for an I/O device
Processes migrate among the various queues
11 Silberschatz, Galvin and Gagne ©2005Operating Systems • Prof. Dr. Marc H. Scholl • DBIS • U KN • Summer Term 2009 12
Silberschatz, Galvin and Gagne ©2005Operating Systems • Prof. Dr. Marc H. Scholl • DBIS • U KN • Summer Term 2009
Selects from among the processes in memory that are ready to execute, and allocates the CPU to one of them
CPU scheduling decisions may take place when a process:
1. Switches from running to waiting state
2. Switches from running to ready state
3. Switches from waiting to ready
4. Terminates
Scheduling under 1 and 4 is nonpreemptive
All other scheduling is preemptive
13 Silberschatz, Galvin and Gagne ©2005Operating Systems • Prof. Dr. Marc H. Scholl • DBIS • U KN • Summer Term 2009
Dispatcher module gives control of the CPU to the process selected by the short-term scheduler; this involves:
switching context
switching to user mode
jumping to the proper location in the user program to restart that program
Dispatch latency – time it takes for the dispatcher to stop one process and start another running
14
Silberschatz, Galvin and Gagne ©2005Operating Systems • Prof. Dr. Marc H. Scholl • DBIS • U KN • Summer Term 2009
SchedulingCPU utilization – keep the CPU as busy as possible
Throughput – # of processes that complete their execution per time unit
Turnaround time – amount of time to execute a particular process
Waiting time – amount of time a process has been waiting in the ready queue
Response time – amount of time it takes from when a request was submitted until the first response is produced, not output (for time-sharing environment)
Deadlines – esp. for real-time scheduling
15
OptimizationMax CPU utilization
Max throughput
Min turnaround time
Min waiting time
Min response time
Operating Systems • Prof. Dr. Marc H. Scholl • DBIS • U KN • Summer Term 2009
Scheduling Strategies
16
Silberschatz, Galvin and Gagne ©2005Operating Systems • Prof. Dr. Marc H. Scholl • DBIS • U KN • Summer Term 2009
Process Burst Time (duration)
P1 24
P2 3
P3 3
Suppose that the processes arrive in the order: P1 , P2 , P3
The Gantt Chart for the schedule is:
Waiting time for P1 = 0; P2 = 24; P3 = 27
Average waiting time: (0 + 24 + 27)/3 = 17
P1 P2 P3
24 27 300
17 Silberschatz, Galvin and Gagne ©2005Operating Systems • Prof. Dr. Marc H. Scholl • DBIS • U KN • Summer Term 2009
Suppose that the processes arrive in the order
P2 , P3 , P1
The Gantt chart for the schedule is:
Waiting time for P1 = 6; P2 = 0; P3 = 3
Average waiting time: (6 + 0 + 3)/3 = 3
Much better than previous case
Convoy effect (or phenomenon) short processes behind long process
P1P3P2
63 300
18
Silberschatz, Galvin and Gagne ©2005Operating Systems • Prof. Dr. Marc H. Scholl • DBIS • U KN • Summer Term 2009
Associate with each process the length of its next CPU burst. Use these lengths to schedule the process with the shortest time
Two schemes:
nonpreemptive – once CPU given to the process it cannot be preempted until completes its CPU burst
preemptive – if a new process arrives with CPU burst length less than remaining time of current executing process, preempt. This scheme is know as the Shortest-Remaining-Time-First (SRTF)
SJF is optimal – gives minimum average waiting time for a given set of processes
19 Silberschatz, Galvin and Gagne ©2005Operating Systems • Prof. Dr. Marc H. Scholl • DBIS • U KN • Summer Term 2009
Process Arrival Time Burst Time
P1 0.0 7
P2 2.0 4
P3 4.0 1
P4 5.0 4
SJF (non-preemptive)
Average waiting time = (0 + 6 + 3 + 7)/4 = 4
P1 P3 P2
73 160
P4
8 12
20
Silberschatz, Galvin and Gagne ©2005Operating Systems • Prof. Dr. Marc H. Scholl • DBIS • U KN • Summer Term 2009
Process Arrival Time Burst Time
P1 0.0 7
P2 2.0 4
P3 4.0 1
P4 5.0 4
SJF (preemptive)
Average waiting time = (9 + 1 + 0 +2)/4 = 3
P1 P3P2
42 110
P4
5 7
P2 P1
16
21Silberschatz, Galvin and Gagne ©2005Operating Systems • Prof. Dr. Marc H. Scholl • DBIS • U KN • Summer Term 2009
Can only estimate the length
Can be done by using the length of previous CPU bursts, using exponential averaging
22
Silberschatz, Galvin and Gagne ©2005Operating Systems • Prof. Dr. Marc H. Scholl • DBIS • U KN • Summer Term 2009
= 0
n+1 = n
Recent history does not count
= 1
n+1 = tn
Only the actual last CPU burst counts
If we expand the formula, we get:
n+1 = tn+(1 - ) tn -1 + …
+(1 - )j tn -j + …
+(1 - )n +1 0
Since both and (1 - ) are less than or equal to 1, each successive term has less weight than its predecessor
23 Operating Systems • Prof. Dr. Marc H. Scholl • DBIS • U KN • Summer Term 2009 24
= 1/2
0 = 10
Silberschatz, Galvin and Gagne ©2005Operating Systems • Prof. Dr. Marc H. Scholl • DBIS • U KN • Summer Term 2009
A priority number (integer) is associated with each process
The CPU is allocated to the process with the highest priority (smallest integer highest priority)
Preemptive
nonpreemptive
SJF is a priority scheduling where priority is the predicted next CPU burst time
Problem Starvation – low priority processes may never execute
Solution Aging – as time progresses increase the priority of the process
25 Silberschatz, Galvin and Gagne ©2005Operating Systems • Prof. Dr. Marc H. Scholl • DBIS • U KN • Summer Term 2009
Each process gets a small unit of CPU time (time quantum), usually 10-100 milliseconds. After this time has elapsed, the process is preempted and added to the end of the ready queue.
If there are n processes in the ready queue and the time quantum is q, then each process gets 1/n of the CPU time in chunks of at most q time units at once. No process waits more than (n-1)q time units.
Performance
q large FIFO
q small q must be large with respect to context switch, otherwise overhead is too high
rule of thumb: 80% of CPU bursts should be smaller than time quantum
26
Silberschatz, Galvin and Gagne ©2005Operating Systems • Prof. Dr. Marc H. Scholl • DBIS • U KN • Summer Term 2009
Process Burst Time
P1 53
P2 17
P3 68
P4 24
The Gantt chart is:
Typically, higher average turnaround than SJF, but better response
P1 P2 P3 P4 P1 P3 P4 P1 P3 P3
0 20 37 57 77 97 117 121 134 154 162
27 Silberschatz, Galvin and Gagne ©2005Operating Systems • Prof. Dr. Marc H. Scholl • DBIS • U KN • Summer Term 2009 28
Silberschatz, Galvin and Gagne ©2005Operating Systems • Prof. Dr. Marc H. Scholl • DBIS • U KN • Summer Term 2009 29 Silberschatz, Galvin and Gagne ©2005Operating Systems • Prof. Dr. Marc H. Scholl • DBIS • U KN • Summer Term 2009
Ready queue is partitioned into separate queues, e.g.,
foreground (interactive)
background (batch)
Each queue has its own scheduling algorithm
foreground – RR
background – FCFS
Scheduling must be done between the queues
Fixed priority scheduling; (i.e., serve all from foreground then from background). Possibility of starvation.
Time slice – each queue gets a certain amount of CPU time which it can schedule amongst its processes; i.e., 80% to foreground in RR
20% to background in FCFS
30
Silberschatz, Galvin and Gagne ©2005Operating Systems • Prof. Dr. Marc H. Scholl • DBIS • U KN • Summer Term 2009 31 Silberschatz, Galvin and Gagne ©2005Operating Systems • Prof. Dr. Marc H. Scholl • DBIS • U KN • Summer Term 2009
A process can move between the various queues; aging can be implemented this way
Multilevel-feedback-queue scheduler defined by the following parameters:
number of queues
scheduling algorithms for each queue
method used to determine when to upgrade a process
method used to determine when to demote a process
method used to determine which queue a process will enter when that process needs service
32
Silberschatz, Galvin and Gagne ©2005Operating Systems • Prof. Dr. Marc H. Scholl • DBIS • U KN • Summer Term 2009
Three queues:
Q0 – RR with time quantum 8 milliseconds
Q1 – RR time quantum 16 milliseconds
Q2 – FCFS
Scheduling
A new job enters queue Q0 which is served FCFS. When it gains CPU, job receives 8 milliseconds. If it does not finish in 8 milliseconds, job is moved to queue Q1.
At Q1 job is again served FCFS and receives 16 additional milliseconds. If it still does not complete, it is preempted and moved to queue Q2.
33 Silberschatz, Galvin and Gagne ©2005Operating Systems • Prof. Dr. Marc H. Scholl • DBIS • U KN • Summer Term 2009 34
Silberschatz, Galvin and Gagne ©2005Operating Systems • Prof. Dr. Marc H. Scholl • DBIS • U KN • Summer Term 2009
Multiple ProcessorsCPU scheduling more complex when multiple CPUs are available
Homogeneous processors within a multiprocessor
Load sharing
Asymmetric multiprocessing – only one processor accesses the system data structures, alleviating the need for data sharing
35
Real-Time SchedulingHard real-time systems – required to complete a critical task within a guaranteed amount of time
Soft real-time computing – requires that critical processes receive priority over less fortunate ones
Operating Systems • Prof. Dr. Marc H. Scholl • DBIS • U KN • Summer Term 2009
Threads
36
Operating Systems • Prof. Dr. Marc H. Scholl • DBIS • U KN • Summer Term 2009
Threads...?
37
• Multiple streams of program execution within a single process
• „Light-weight“ processes
• Switching from one thread to another requires less effort
• Threads share (some) context
• ... can be implemented „on top“, e.g., in a library (user-level threads)orinside the kernel (kernel-level threads)
Silberschatz, Galvin and Gagne ©2005Operating Systems • Prof. Dr. Marc H. Scholl • DBIS • U KN • Summer Term 2009 38
Silberschatz, Galvin and Gagne ©2005Operating Systems • Prof. Dr. Marc H. Scholl • DBIS • U KN • Summer Term 2009
Responsiveness
Resource Sharing
Economy
Utilization of MP Architectures
39 Silberschatz, Galvin and Gagne ©2005Operating Systems • Prof. Dr. Marc H. Scholl • DBIS • U KN • Summer Term 2009
Thread management done by user-level threads library
Three primary thread libraries:
POSIX Pthreads
Win32 threads
Java threads
40
Supported by the Kernel
Examples
Windows XP/2000
Solaris
Linux
Tru64 UNIX
Mac OS X
Silberschatz, Galvin and Gagne ©2005Operating Systems • Prof. Dr. Marc H. Scholl • DBIS • U KN • Summer Term 2009
Many-to-One: Many user-level threads mapped to single kernel thread
Solaris Green Threads
GNU Portable Threads
One-to-One: Each user-level thread maps to kernel thread
Windows NT/XP/2000
Linux
Solaris 9 and later
Many-to-Many
Allows many user level threads to be mapped to many kernel threads
Allows the operating system to create a sufficient number of kernel threads
Solaris prior to version 9
Windows NT/2000 with the ThreadFiber package
41 Silberschatz, Galvin and Gagne ©2005Operating Systems • Prof. Dr. Marc H. Scholl • DBIS • U KN • Summer Term 2009 42
Silberschatz, Galvin and Gagne ©2005Operating Systems • Prof. Dr. Marc H. Scholl • DBIS • U KN • Summer Term 2009 43 Silberschatz, Galvin and Gagne ©2005Operating Systems • Prof. Dr. Marc H. Scholl • DBIS • U KN • Summer Term 2009 44
Silberschatz, Galvin and Gagne ©2005Operating Systems • Prof. Dr. Marc H. Scholl • DBIS • U KN • Summer Term 2009
Similar to M:M, except that it allows a user thread to be bound to kernel thread
Examples
IRIX
HP-UX
Tru64 UNIX
Solaris 8 and earlier
45 Silberschatz, Galvin and Gagne ©2005Operating Systems • Prof. Dr. Marc H. Scholl • DBIS • U KN • Summer Term 2009
Semantics of fork() and exec() system calls
Does fork() duplicate only the calling thread or all threads?
Thread cancellation
Terminating a thread before it has finished
Two general approaches:
Asynchronous cancellation terminates the target thread immediately
Deferred cancellation allows the target thread to periodically check if it should be cancelled
Signal handling
Thread pools
Thread specific data
Allows each thread to have its own copy of data
Useful when you do not have control over the thread creation process (i.e., when using a thread pool)
Scheduler activations
46
Silberschatz, Galvin and Gagne ©2005Operating Systems • Prof. Dr. Marc H. Scholl • DBIS • U KN • Summer Term 2009
Both M:M and Two-level models require communication to maintain the appropriate number of kernel threads allocated to the application
Scheduler activations provide upcalls - a communication mechanism from the kernel to the thread library
This communication allows an application to maintain the correct number kernel threads
47 Operating Systems • Prof. Dr. Marc H. Scholl • DBIS • U KN • Summer Term 2009
Examples
48
Silberschatz, Galvin and Gagne ©2005Operating Systems • Prof. Dr. Marc H. Scholl • DBIS • U KN • Summer Term 2009
A POSIX standard (IEEE 1003.1c) API for thread creation and synchronization
API specifies behavior of the thread library, implementation is up to development of the library
Common in UNIX operating systems (Solaris, Linux, Mac OS X)
49 Silberschatz, Galvin and Gagne ©2005Operating Systems • Prof. Dr. Marc H. Scholl • DBIS • U KN • Summer Term 2009
Implements the one-to-one mapping
Each thread contains
A thread id
Register set
Separate user and kernel stacks
Private data storage area
The register set, stacks, and private storage area are known as the context of the threads
The primary data structures of a thread include:
ETHREAD (executive thread block)
KTHREAD (kernel thread block)
TEB (thread environment block)
50
Silberschatz, Galvin and Gagne ©2005Operating Systems • Prof. Dr. Marc H. Scholl • DBIS • U KN • Summer Term 2009
Linux refers to them as tasks rather than threads
Thread creation is done through clone() system call
clone() allows a child task to share the address space of the parent task (process)
51 Silberschatz, Galvin and Gagne ©2005Operating Systems • Prof. Dr. Marc H. Scholl • DBIS • U KN • Summer Term 2009
Java threads are managed by the JVM
Java threads may be created by:
Extending Thread class
Implementing the Runnable interface
52
Operating Systems • Prof. Dr. Marc H. Scholl • DBIS • U KN • Summer Term 2009
Intended Schedule
Date Lecture Hand out Submission0 20.04. Introduction to Operating Systems Course reggistration1 27.04. Systems Programming using C (File Subsystem) 1. Assignment2 04.05. Systems Programming using C (Process Control) 2. Assignment 1. Assignment3 11.05. Process Scheduling 3. Assignment 2. Assignment4 18.05. Process Synchronization 4. Assignment 3. Assignment5 25.05. Inter Process Communication 5. Assignment 4. Assignment6 01.06. Pfingstmontag 6. Assignment 5. Assignment7 08.06. Input / Output 7. Assignment 6. Assignment8 15.06. Memory Management 8. Assignment 7. Assignment9 22.06.
Filesystems9. Assignment 8. Assignment
10 29.06.Filesystems
10. Assignment 9. Assignment11 06.07. Special subject: Transactional Memory 10. Assignment12 13.07. Special subject: XQuery your Filesystem13 20.07. Wrap up session
27.07. First examination daate12.10. Second examination date
53 Operating Systems • Prof. Dr. Marc H. Scholl • DBIS • U KN • Summer Term 2009
IV.Process
Synchronization
54