You are on page 1of 48

Operating System

Muhammad Tauqeer
Operating System
 Operating system is system software
 It work as a interface b/w user and hardware
 Interaction between user and hardware is provided by Operating Sytem
 Why OS?
 User access to hardware without OS is difficult
 Without OS the user can write their own program for any hardware they want to access
 OS is used to access hardware to the user they want to access easily
Operating System Goals

 Execute user programs and make solving user problems easier.


 Make the computer system convenient to use.
 Use the computer hardware in an efficient manner.
Functions of OS
 Resource Manager
 Process management
 Storage Management
 Memory management
 Security
Batch Operating Systems
 The users did not interact directly with the system.
 The users prepared a job and submitted this to the computer operator.
 Operator takes the job and group similar jobs into batch.
 Operator takes the job as input, process it on CPU and gives the results back to the
users.
 Medium used are; Punch card, magnetic tape and paper tape.
Batch Systems
Multi-Programming OS (Non-Preemptive)
 A multiprogramming operating system runs multiple programs on a single processor
computer.
 Increase CPU utilization by arranging the jobs.
 CPU always have to execute one jobs/process.
 Less idle time.
 Keeps multiple jobs in main memory.
 When CPU switches to other job?
 When job is executed and it start wait for I/O event, then CPU picks
another job
 Example:
 Two processes P1 and P2 with CPU and I/O bursts of one time
unit each
Multi-Programming Operating System
Multi-Programming Operating System
Multi-Programming Operating Sytem
 Job pool
 Jobs enter in the system are kept in the job pool (disk)
 Jobs are brought in the memory for execution

 Jobs scheduling
 If several jobs are waiting to be brought in the memory and there is no enough space then
CPU select jobs among them which is know as scheduling
Multitasking Operating Systems (Preemptive)
 Extension of multiprogramming
 Uses multiprogramming and CPU scheduling to provide portion of time to each user
 CPU executes multiple jobs by switching among them typically assigned some specific
quantum of time for which process is to be executed.
 This switching occurs so fast that user feel like interacting with each executing task at the
same time.
Multiprocessing Operating Systems
 More than one processor is used.
 Allow more than one process to be executed at the same time.
 These multiple processor shares memory bus, memory and devices.
 Two types of multiprocessing OS
 Symmetric
 Asymmetric
 Enhanced performance
 Fast processing
 More Reliable
Multiprocessing Operating Systems
Real-Time Systems
 Used in real-time systems where the time constraints are fixed and followed strictly.
 Well-defined fixed-time constraints.
 If system does not produce within time constraints it will be failed.
 Used in traffic control, ATM, Robots etc.
 Two types:
 Soft: Less Strict time
 Hard: Very strict time constraint
Process States
 As a process executes, it changes states

 Each process may be in either of the following states


 New - The process is being created.
 Running - Instructions are being executed.
 Waiting - The process is waiting for some event to occur (such as an I/O
completion or reception of a signal.
 Ready - The process is waiting to be assigned to a processor.
 Terminated - The process has finished execution
Process State Diagram
Process Control Block (PCB)

 Each process is represented in the operating system by a process control block (PCB)
 Also called task control block
 When the process is created by the operating system it creates a data structure to store the
information of that process. This is known as Process Control Block (PCB).
 A Process Control block (PCB) is a data structure that stores information about each process.
Process Control Block (PCB)…
Process Control Block (PCB)
 Process state: The state may be new, ready, running, waiting, halted and so on.

 Program counter: The counter indicates the address of the next instruction to be executed for
this process.

 CPU registers: The registers vary in number and type, depending on the computer
architecture.
 They include accumulators, index registers, stack pointers and general-purpose registers,
plus any condition code information. Along with the program counter, this state
information must be saved when an interrupt occurs, to allow the process to be continued
correctly afterwards.
Process Control Block (PCB) …
 CPU Scheduling information: This information includes a process priority, pointers to
scheduling queues, and any other scheduling parameters

 Memory-management Information: This information may include the value of the


base and limit registers, the page tables, or the segment tables, depending onthe memory
system used by the operating system.

 Accounting Information: This information includes the amount of CPU and real time used,
time
limits, account numbers, job or process numbers, and so on.

 I/O Status Information: The information includes the list of I/O devices allocated to the
process, a list of open files, and so on many pieces of information associated with a specific
process, including

 PCB simply serves as the repository for any information that may vary from process to process
Process scheduling
 We have already studies that more than one processes can be executed in case of Multi-
programming and Time sharing system

 Process scheduler selects an available process (possibly from a set of several available
processes) for program execution on the CPU

 A uniprocessor system can have only one running process at a given time. If
more processes exist, the rest must wait until the CPU is free and can be
rescheduled

 Context switch
 Switching the CPU from one process to another requires saving of the context of the
current process and loading the state of the new process
Context Switching
CPU Scheduling
CPU Scheduling
 Basic concept
 Objective of multiprogramming is to have some process running at all times, in order to maximize
CPU utilization.
 In multiprogramming, a process is executed until it must wait, typically for the completion of some
I/O request.
 When one process has to wait, the OS takes the CPU away from that process and gives the CPU to
another process.

 Life of a process
 Process execution consist cycle of CPU execution and I/O wait.
 Processes alternates between these two states.
 Process starts from CPU burst, followed by I/O wait and so on.
 Process terminates upon the CPU burst.
Life of a Process
CPU Scheduler…
 Short-term scheduler selects a process from among the processes in the ready queue
 Invokes the dispatcher to have the CPU allocated to the selected process
 Ready queue can be implemented as a FIFO queue, a tree, or simply an unordered linked list
and records in ready queue are PCB of processes

 Dispatcher gives control of the CPU to the process selected by the short-term scheduler;
this function involves:
 Switching context – saving current process context and load the context of another

 Dispatch latency – time it takes for the dispatcher to stop one process and start another
running.
 Typically, a few microseconds
Scheduling Criteria
 CPU utilization
 Keep the CPU as busy as possible
 Throughput
 Number of processes that complete their execution per time unit
 Turnaround time
 Amount of time to execute a particular process = CPU + I/O + Wait of memory + Waiting
time
 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)
Optimization Criteria
 Maximize CPU utilization

 Maximize throughput

 Minimize turnaround time

 Minimize waiting time

 Minimize response time


Scheduling
Algorithms
 The following are the scheduling algorithms
 First-Come-First-Served (FCFS) Scheduling
 Shorted Job First (SJF) Scheduling
 Shortest Remaining Time First (SRTF)
Scheduling
 Priority Scheduling
 Round-Robin Scheduling
 Multilevel Queues Scheduling
 Multilevel Feedback Queues Scheduling

 We will discuss each of them one by one


First Come First Scheduling (FCFS)
 The process that enters the ready queue first is scheduled first, regardless of the size of its
next CPU burst

 Example: Process Burst Time


P1 24
P2 3
P3 3
 Suppose that processes arrive into the system in the order: P1, P2 , P3
First Come First Scheduling (FCFS)…
 Processes are served in the order: P1, P2, P3
 The Gantt Chart for the schedule is:

P1 P2 P3

0 24 27 30

 Waiting times P1 = 0; P2 = 24; P3 = 27


 Average waiting time: (0+24+27)/3 = 17
First Come First Scheduling (FCFS)…
 Suppose that processes arrive in the order: P2 , P3 , P1 .
 The Gantt chart for the schedule is:

P2 P3 P1

0 3 6 30

 Waiting time for P1 = 6; P2 = 0; P3 = 3


 Average waiting time: (6 + 0 + 3)/3 = 3
First Come First Scheduling (FCFS)…

Process Arrival Time Burst Time Completion time Turn around time Waiting time Response time

P1 0 8

P2 1 4

P3 2 6

P4 3 2
First Come First Scheduling (FCFS)…

Process Arrival Time Burst Time Completion time Turn around time Waiting time Response time

P1 0 8 8 8 0 0

P2 1 4 12 11 7 7

P3 2 6 18 16 10 10

P4 3 2 20 17 15 15
First Come First Scheduling (FCFS)…

Process Arrival Time Burst Time Completion time Turn around time Waiting time Response time

P1 0 2

P2 1 2

P3 5 3

P4 6 4
First Come First Scheduling (FCFS)…

Process Arrival Time Burst Time Completion time Turn around time Waiting time Response time

P1 0 2 2 2 0 0

P2 1 2 4 3 1 1

P3 5 3 8 3 0 0

P4 6 4 12 6 2 2
Shortest Job First Scheduling (SJF)
 Process with the shortest CPU burst is scheduled first.

 Non-preemptive – once CPU given to a 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 it—Shortest-Remaining-Time-First (SRTF).

 SJF is optimal non-preemptive scheduling algorithm – gives minimum average waiting


time for a given set of processes.
Non-Preemptive SJF
Process Arrival Time Burst Time
P1 0.0 7
P2 2.0 4
P3 4.0 1
P4 5.0 4
 Gantt chart
P1 P3 P2 P4

0 12 16

7
 Average waiting time = (0+6+3+7)/4 = 4
Non-Preemptive SJF

Process Arrival Time Burst Time Completion time Turn around time Waiting time Response time

P1 1 3

P2 2 4

P3 1 2

P4 4 4
Non-Preemptive SJF

Process Arrival Time Burst Time Completion time Turn around time Waiting time Response time

P1 1 3 6 5 2 2

P2 2 4 10 8 4 4

P3 1 2 3 2 0 0

P4 4 4 14 10 6 6
Preemptive SJF

 Gantt chart
P1 P2 P3 P2 P4 P1

0 2 4 5 11 16
7
 Average waiting time = (9 + 1 + 0 +2)/4 = 3
Preemptive SJF

Process Arrival Time Burst Time Completion time Turn around time Waiting time Response time

P1 0 5

P2 1 3

P3 2 4

P4 4 1
Preemptive SJF

Process Arrival Time Burst Time Completion time Turn around time Waiting time Response time

P1 0 5 9 9 4 0

P2 1 3 4 3 0 0

P3 2 4 13 11 7 7

P4 4 1 5 1 0 0
Priority Scheduling
 A priority number (integer) is associated with each process
 The CPU is allocated to the process with the highest priority (higher integer  highest
priority).
 Preemptive
 Non-preemptive

 SJF is a priority scheduling where priority is the predicted next CPU burst time.
Priority Scheduling

Priority Process Arrival Time Burst Time Completion time Turn around time Waiting time Response time

10 P1 0 5

20 P2 1 4

30 P3 2 2

40 P4 4 1
Priority Scheduling

Priority Process Arrival Time Burst Time Completion time Turn around time Waiting time Response time

10 P1 0 5 12 12 7

20 P2 1 4 8 7 3

30 P3 2 2 4 2 0

40 P4 4 1 5 1 0
Any Question?

Thank you

You might also like