Lecture4 OS
Lecture4 OS
● Multiprogramming
● Context Switching
● Process Scheduling
● Schedulers
• In uniprogramming, one program was started and run in full to completion; the
next job would start immediately after the first one finished.
• The problem with this approach is that programs consisted of both CPU
instructions and I/O operations.
• CPU instructions were very fast, as they consisted of electrical signals. I/O
operations, however, were very slow (One example of an early I/O device was
the drum memory, which was a large magnetic device that had to be
mechanically turned for every data access).
Uniprogramming and Utilization
• If a program required many I/O operations to be performed, that created a lot
of wasted time where the CPU sat idle instead of executing instructions. We
can quantify this wasted time as the CPU utilization.
• For instance, if a system is only used for half of the time that it could be, we
would say that it experienced 50% utilization.
The green regions indicate times when the CPU is executing instructions in the
program, while the yellow indicates that the times where the CPU is idle while
waiting on an I/O operation to complete.
Uniprogramming and Utilization
The following table summarizes the time each process spends executing on the
CPU or waiting for I/O:
In this scenario, the CPU was used for a total of 15 out of 25 possible seconds, so
the system experienced 60% CPU utilization when running these three jobs:
• This approach allows the kernel to keep the CPU active and performing work
as much as possible, thereby reducing the amount of wasted time.
As before, the green regions indicate CPU execution and the yellow indicates I/O
operations. However, note that processes B and C can run while A is waiting on
its I/O operation. Similarly, A and C execute while B is waiting on I/O operations.
As a result, the CPU is only completely idle while C’s I/O operation is performed
at time 15, because A and B have already run to completion.
Multiprogramming and Concurrency
• In our revised CPU utilization calculation, the numerator does not change
because the total amount of CPU execution time has not changed.
• Only the denominator changes, to account for the reduced time wasted
waiting on A’s and B’s I/O operations.
Multiprogramming vs Uniprogramming
Implementing Multiprogramming
There are two forms of multiprogramming that have been implemented:
• Preemptive multitasking
• Cooperative Multitasking
Preemptive multitasking:
Preemptive multitasking is the most common technique in which processes
are given a maximum amount of time to run.
This amount of time is called a quantum, typically measured in milliseconds.
In preemptive multitasking, if a process issues an I/O request before its
quantum has expired, the kernel will simply switch to another process early.
However, if the quantum has expired (i.e., the time limit has been reached), the
kernel will preempt the current process and switch to another.
A multiprogramming strategy in which processes are granted a time-
limited access to the CPU and interrupted when that time limit expires.
Implementing Multiprogramming
• Cooperative multitasking
In contrast, with cooperative multitasking, a process can run for as long as it
wants until it voluntarily relinquishes control of the CPU or initiates an I/O
request.
Cooperative multitasking has a number of advantages, including its simplicity
of design and implementation.
Furthermore, if all processes are very interactive (meaning they perform many
I/O operations), it can have low overhead costs.
However, cooperative multitasking is vulnerable to rogue processes that
dominate the CPU time. For instance, if a process goes into an infinite loop that
does not perform any I/O operation, it will never surrender control of the CPU
and all other processes are blocked from running.
As a result, modern systems favour preemptive multitasking.
Topics for today…
● Multiprogramming
● Context Switching
● Process Scheduling
● Schedulers
• Context Switching involves storing the context or state of a process so that it can be reloaded
when required and execution can be resumed from the same point as earlier.
• This is a feature of a multitasking operating system and allows a single CPU to be shared by
multiple processes.
• Although context switches are critical for multiprogramming, they also introduce complexity
and overhead costs in terms of wasted time.
First, when the kernel determines that it needs to perform a context switch, it must decide
which process it will now make active; this choice is performed by a scheduling routine
that takes time to run.
Second, context switches introduce delays related to the memory hierarchy.
Steps in Context Switching
There are several steps involves in context switching of the processes. The following diagram
represents the context switching of two processes, P1 to P2, when an interrupt, I/O needs, or
priority-based process occurs in the ready queue of PCB.
Steps in Context Switching
• As we can see in the diagram, initially, the P1
process is running on the CPU to execute its task,
and at the same time, another process, P2, is in
the ready state.
• If an error or interruption has occurred or the
process requires input/output, the P1 process
switches its state from running to the waiting
state.
• Before changing the state of the process P1,
context switching saves the context of the
process P1 in the form of registers and the
program counter to the PCB1.
• After that, it loads the state of the P2 process
from the ready state of the PCB2 to the running
state.
Steps in Context Switching
1. First, the context switching needs to save the
state of process P1 in the form of the program
counter and the registers to the PCB (Program
Counter Block), which is in the running state.
• User and Kernel Mode Switching: A context switch may take place when a
transition between the user mode and kernel mode is required in the OS.
Context Switching
Context Switching gives the impression to the user that the system has multiple
CPUs by executing multiple processes.
https://drive.google.com/file/d/1OAaGxG-
ntm7ruONNSAFtw7s9BUlZWZu2/view?usp=share_link
Topics for today…
● Multiprogramming
● Context Switching
● Process Scheduling
● Schedulers
● The goal of a multiprogramming system tries to keep the CPU busy at all
times, that is, maximize CPU use.
1. Non-preemptive: Here the resource can’t be taken from a process until the
process completes execution. The switching of resources occurs when the
running process terminates and moves to a waiting state. VOLUNTARILY
● Context Switching
● Process Scheduling
● Schedulers
● All the process of the same execution state are placed in the same queue.
Therefore, whenever the state of a process is modified, its PCB needs to be
unlinked from its existing queue, which moves back to the new state queue.
● Multiprogramming
● Context Switching
● Process Scheduling
● Schedulers
● Multiprogramming
● Context Switching
● Process Scheduling
● Schedulers