CS330 Operating System Part IV
CS330 Operating System Part IV
Lecture 12
Limited Direct Execution
What we have seen so far:
1. Process: Each process thinks that it owns a CPU
2. Address space: Each process feels like it owns a huge address space.
3. File system tree: The user feels like operating on files directly.
What are the OS responsibilities for providing the above virtual notions?
Hardware support
The OS cannot enforce limits on processes by itself and still achieve efficiency. OS needs support
from hardware: Privilege levels.
CPU can execute in two modes: User mode and Kernel mode. Some operations are allowed
only from kernel-mode (privileged OPs): if executed from user space, the hardware will notify the
OS by raising fault/traps.
If the user process requires to invoke the services of the OS, the privilege level of the CPU
cannot be changed. The hardware provides entry instructions from the user mode which causes
a mode switch. The OS defines the handler for different entry gates. These handlers are also for
faultsexceptions, device interrupts. Registration of handlers are privileged.
After the boot, the OS needs to configure the handlers for system calls, exceptions/faults and
interrupts. The handler code is invoked by the OS when user-mode process invokes a system call
or an exception or an external interrupt.
2
Lecture 13
Priveleged ISA (x86 64)
The support needed from the hardware includes:
(i) CPU privilege levels
(ii) switching between modes- entry points and handlers
#include <stdio.h>
int main() {
asm("hlt;");
}
HLT- halts the CPU till next external interrupt. It is a privileged instruction. Cannot be exe-
cuted from the user space. In case called from the user space, the linux kernel kills the application.
Note: this protection is independent from user-user privilege (distinction) (eg.: root user and
others). The user privilege is a software construct.
#include <stdio.h>
int main() {
unsigned long cr3_val;
asm volatile (
"mov %%cr3, %0;"
:"=r" (cr3_val)
:
);
printf("%lx\n", cr3_val);
}
The CR3 register points to address space info for translation information. When executed
from user space results in protection fault.
3
Lecture 15-16
OS Mode Execution
Post Boot OS execution
4
⇒ The OS has its own stack, which switches the stack on kernel entry. In x86 64 the hardware
switches the stack pointer to the stack address configured by the OS.
Lecture 17-18
Process Scheduling
Triggers for process context switch
- The user process can invoke the scheduler through system calls like sched_yield()
- The user process can invoke sleep() to suspend itself.
- A process may have to wait for some I/O event. Eg.: read() from a file on disk.
- The OS gets the control back on every syscall and exception. Before returning from the
syscall the scheduler can de-schedule the process and schedule something else.
- Timer interrupts can be configured to generate interrupts periodically or after some config-
ured time. The OS can invoke the scheduler after handling the interrupt.
Assume a process P2 was in kernel mode initially, which was de-scheduled dome time back by
the scheduler and has been scheduled again.
6
Scheduling
A queue of ready processes is maintained. A scheduler decides to pick the process based on
some scheduling policy ans performs a context switch. The outgoing process is put back to the
ready queue (if required).
Most processes perform a mixture of CPU and I/O activities. When the process is waiting
for an an I/O it is moved to the waiting state. The process becomes ready again when the event
completion is notified (eg. on device interrupt).
What if there is no process in the ready queue?
System Idle process: There can be instances when there are zero ready processes in the
ready queue. A special process is always there. It halts the CPU (HLT in x86 64)
7
Scheduling metrics
Consider the following metrics:
Turnaround time: Time of completion - Time of arrival [objective is to minimize turnaround
time]
Waiting time: sum of time spent in ready queue [objective is to minimize waiting time]
Response time: Waiting time before the first execution of the the process [objective is to
minimize the response time]
Average value of metrics is used to represent average efficiency.
Standard deviation: measure of fairness of the scheduling process.
Lecture 19-20
Process Scheduling Policies
First Come First Serve (FCFS)
FIFO queue based non-preemptive scheduling.
Eg.:
AT CPU Burst
P1 0 100
P2 0 15
P3 0 45
P1 P2 P3
0 100 115120
FCFS
P3 P2 P1
0 5 20 120
SJF
P1 P2 P3 P2 P4 P3
0 1 2 4 8 13 22
STCF
AT CPU Burst
P1 0 15
P2 3 5
P3 6 5
P1 P2 P1 P3 P1
0 5 10 15 20 25
RR
0+2+9
Avg. Waiting time = 3
Priority Scheduling
Select the process with highest priority can be preemptive and non-preemptive.
SJF: priority based by job lengths
Advantages: practical (no assumptions)
Disadvantage: Starvation
Problem formulation with I/O bursts: most process require a series of CPU and I/O
bursts. Every CPU burst can be treated as a new process where every CPU burst start is the
process, with arrival time and the burst length as the execution length.
Lecture 21
Static priority based scheduling
Processes are assigned to different queues based on their priority. Processes from the non-empty
highest priority queue is always picked. Different queues may implement schemes.
Main concern: Starvation of low-priority processes.
10
Approximation of SJF
MLFQ can approximate SJF because:
- long running jobs are moved to low priority queues.
- New jobs are added to highest priority queue.
A shorter job may not get a chance to execute for a small duration.
Upper bounded by: #( of jobs on the highest priority queue + 1) × time quantum
MLFQ favours interactive jobs because interactive jobs maintain the highest priority as they
relinquish the CPU before quantum expires. Long running jobs are moved to low priority queues.
Thus in a steady state, interactive jobs compete with other interactive jibs.
Real time processes are always at higher priority than normal processes. (pre-emptive in
nature)
Priority value: 1 − 99 (lower value: higher priority)
FIFO: run to completion
RR: round robin within a given priority level
sched_setscheduler syscall to define scheduling class and priorities.
SCHED_OTHER: Default policy. OS dynamic priorities and variable time slicing comes into
picture.
SCHED_BATCH: Assume CPU bound while calculating priorities.
SCHED_IDLE: very low priority jobs.
nice(19) moves the process to the lowest priority queue, i.e. 139.
nice(-20) moves the process to the highest priority queue, i.e. 100.
For this scheduling class dynamic priority is calculated by the Linux kernel considering the
inter-activeness of the process.
A task is picked from the non-empty highest priority queue. Critical task queue contains
tasks which require immediate attention: hardware events, restarts, etc.
Normal task queue (a.k.a fair scheduling class) implements the heuristics to self-adjust.
If all the queues are empty, swapper task is scheduled (HLT the CPU).