0% found this document useful (0 votes)
14 views32 pages

Operating System IT-41033

Chapter 5 of the document focuses on process scheduling within operating systems, detailing CPU scheduling concepts, criteria, and various algorithms such as FCFS, SJF, and Round-Robin. It discusses both preemptive and non-preemptive scheduling, as well as the roles of CPU schedulers and dispatchers. Additionally, it covers thread scheduling and the use of PThreads for managing concurrent execution.

Uploaded by

memezawkhin06
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views32 pages

Operating System IT-41033

Chapter 5 of the document focuses on process scheduling within operating systems, detailing CPU scheduling concepts, criteria, and various algorithms such as FCFS, SJF, and Round-Robin. It discusses both preemptive and non-preemptive scheduling, as well as the roles of CPU schedulers and dispatchers. Additionally, it covers thread scheduling and the use of PThreads for managing concurrent execution.

Uploaded by

memezawkhin06
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 32

OPERATING SYSTEM

IT-41033

CHAPTER – 5
My Profile

Dr. Naing Win Tun


Associate Professor

minnyithattant@gmail.com

minnyithattant

www.facebook.com/minnyithattant (မင််းညီသက်တ)ံ့
2
Semester - I

Overview
C-1 Introduction
C-2 System Structures
5.1 Basic Concept
5.2 Scheduling Criteria
Process Management 5.3 Scheduling Algorithms
C-3 Process Concept 5.4 Thread Scheduling
C-4 Multithreaded Programming 5.5 Multiple-Processor Scheduling
C-5 Process Scheduling 5.6 Operating System Examples
5.7 Algorithm Evaluation
5.8 Summary
Process Coordination
C- 6 Synchronization
C-7 Deadlocks
CHAPTER (5): PROCESS SCHEDULING

• Objectives
• To introduce CPU scheduling, which is the basis for multiprogrammed
operating system
• To describe various CPU-scheduling algorithms
• To discuss evaluation criteria for selecting a CPU-scheduling algorithm for a
particular system.
5.1 BASIC CONCEPT
• CPU Scheduling is central to operating system design.
• Process two states: CPU burst distribution, I/O burst cycle
• Keep several processes in memory.
• Every time one process has to wait, another process takes over the use of the CPU
• CPU-I/O burst cycle: Process execution => a cycle of CPU execution and I/O wait
(i.e., CPU burst and I/O burst).
• Large number of short CPU bursts, and a small number of long CPU bursts.
• An I/O-bound program would typically has many very short CPU bursts.
• A CPU-bound program might have a few long CPU bursts
Alternating sequence of CPU and I/O bursts

• CPU burst is when the process is being executed in the CPU


• I/O burst is when the CPU is waiting for I/O further execution
CPU Scheduler
• Short-term scheduler 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 non-preemptive Recall from chapter(3)

• All other scheduling is preemptive


• Consider access to shared data
• Consider preemption while in kernel mode
• Consider interrupts occurring during crucial OS activities
Scheduling Issues
Preemptive Scheduling
1. When a process switches from the running state to the waiting state (I/O request or wait)
2. When a process switches from the running state to the ready state (interrupt)
3. When a process switches from the waiting state to the ready state (completion I/O)
4. When a process terminates.

Recall from chapter(3) Example


Non-Preemptive Scheduling
1. When a process switches from the running state to the waiting state (I/O request or wait)
2. When a process switches from the running state to the ready state (interrupt)
3. When a process switches from the waiting state to the ready state (completion I/O)
4. When a process terminates.

• In non-preemptive scheduling, a running process cannot be


interrupted by the operating system;
• In this scheduling, once the resources (CPU cycles) are allocated
to a process, the process holds the CPU till it gets terminated or
reaches a waiting state.

Example
Dispatcher
• Dispatcher module gives control of the CPU to the process selected by the short-term scheduler:
• switching context (restore the state of process to be run next)
• switching to user mode (for security and privilege break)
• jumping to the proper location in the user program (restart that program)
• Dispatch latency – time it takes for the dispatcher to stop one process and start another running
• When multiple tasks or programs want to use the CPU, the dispatcher helps decide which one gets to go
next.
5.2 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
• 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 (interactive system)
5.3 SCHEDULING ALGORITHMS

• First-come, first-served (FCFS) scheduling


• Shortest-job-first (SJF) scheduling
• Priority scheduling
• Round-robin scheduling
• Multilevel queue scheduling
• Multilevel feedback queue scheduling
First-Come, First-Served Scheduling
• Suppose that the processes arrive in the order: P1, P2, P3
Another Sample
P1 P2 P0
0 5 7 10

• Gantt Chart for the schedule:

• Waiting time for P1 = 0, P2 = 24, P3 = 27


• Average waiting time => (P1+P2+P3)/3 = 17

Note: that is easy


First-Come, First-Served Scheduling (FCFS)
• Suppose that the processes arrive in the order: P2, P3, P1
P2 =3ms, P3=3ms , P1=24
• The Gantt chart for the schedule is:

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


• Average waiting time: (6 + 0 + 3)/3 = 3
• Convoy effect - short process behind long process
• Consider one CPU-bound and many I/O-bound processes
FCFS Example 0 6 10 14 16

(Non-preemptive) P2 P3 P1 P0

• Turnaround time = Completion time – Arrival time


• Waiting time = Turnaround time – Burst time
Process Complete Time Turnaround Time Waiting Time

P0 16 16-3=13 13-2=11

P1 14 14-2=12 12-4=8

P2 6 6-0=6 6-6=0

P3 10 10-1=9 9-4=5

Note: that is also easy • Average waiting time = (11+8+0+5) / 4 = 6 ms


FCFS Example (Animation)
Time 0 P1 = 4

Process Arrival Time Burst Time


Time 1
P1 0 4 Time 2
P2 1 2 Time 3
P3 3 3 CPU
Time 4 P2 = 2
P4 6 1
Time 5 P3 = 3
Time 6
Time 7 P4 = 6

Process queue Time 8

P4 = 6 P3 = 3 P2 = 2 P1 = 4
Shortest-Job-First Scheduling (SJF)

• Algorithm associates with each process the length of its next CPU burst.
• Use these lengths to schedule the process with the shortest time
• Algorithm can be either preemptive or nonpreemptive.
• A more appropriate term for this scheduling method: Shortest-next-CPU-Burst Algorithm
Shortest-Job-First Scheduling (SJF)

• SJF Scheduling chart

• Average waiting time = (3 + 16 + 9 + 0) / 4 = 7 ms


• In FCFS, average waiting time = 10.25 ms

Note: notice length of its next CPU burst.


Example of Non-Preemptive SJF
Process Arrival Time Burst Time
P1 0 7
P2 2 4
P3 4 1
P4 5 4

• SJF (non-preemptive)

• Average waiting time = (0 + 6 + 3 + 7) / 4 = 4

Note: start 0, notice length of its next CPU burst.


Example of Preemptive SJF

Explanation:
Start 0, but 1ms, P2 arrive, so, P2 run, at that time
P2’s burst time is shortest, so, P2 executes. And, next
shortest is P4, it executes. Then, P1 and P3

• SJF
• Waiting time = Total waiting time – no. of ms process executed - Arrival time

• Average waiting time = [(10-1-0)+(1-0-1)+(17-0-2)+(5-0-3)] / 4 = 26 / 4 = 6.5 ms


Note: start 0, notice (first time preemptive) and length of its next CPU burst.
Example of Preemptive SJF
Process Arrival Time Burst Time
Explanation:
P1 0 7
Start 0, but 2ms, P2 arrive, so, P2 run, at that time,
P2 2 4 4ms,P3 arrive, so, P3 executes. It is shortest, then, P2,
P4 and P1 executes 5ms remained.
P3 4 1
P4 5 4

• SJF (preemptive)
• Waiting time = Total waiting time – no. of ms process executed - Arrival time

• Average waiting time = (11-2-0)+(5-2-2)+(4-0-4)+(7-0-5) =(9 + 1 + 0 + 2) / 4 = 3ms


Priority Scheduling (Preemptive)
Arrival Burst 0 2 5 33 40 49 51 67
Process Priority
Time Time
P1 P4 P2 P4 P1 P3 P5
P1 0 11 9 2
P2 5 28 0 Explanation:
Start 0, but 2ms, P4 arrive, P4 run, when running time
P3 12 2 3 5ms, P2 arrive, its’ priority is 0, assume=>0 is highest
P4 2 10 7 1 priority, so, P2 run. And P4 is priority 1, P4 run, P1, P3,
and P5.
P5 9 16 4
• Waiting time = Total waiting time – no. of ms process executed - Arrival time
Waiting Time P1 = (40-2-0) = 38 ms
Waiting Time P2 = (5-0-5) = 0 ms
Waiting Time P3 = (49-0-12) = 37 ms
Waiting Time P4 = (33-3-2) = 28 ms • Average Waiting Time = (38+0+37+28+42) / 5 = 29 ms

Waiting Time P5 = (51-0-9) = 42 ms

Note: start 0, notice priority, and preemptive.


Priority Scheduling (Non-Preemptive)
Arrival Burst 0 4 9 11 12 15
Process Priority
Time Time
P1 P4 P5 P3 P2
P1 0 4 3
P2 1 3 2 • Turnaround time = Completion time – Arrival time
P3 2 1 1
P4 3 5 0 • Waiting time = Turnaround time – Burst time
P5 4 2 0
Turnaround Time P1 = (4-0) = 4 ms Waiting Time P1 = (4-4) = 0 ms
Turnaround Time P2 = (15-1) = 14 ms Waiting Time P2 = (14-3) = 11 ms
Turnaround Time P3 = (12-2) = 10 ms Waiting Time P3 = (10-1) = 9 ms
Turnaround Time P4 = (9-3)= 6 ms Waiting Time P4 = (6-5) = 1 ms
Turnaround Time P5 = (11-4) = 7 ms Waiting Time P5 = (7-2) = 5 ms

• Average Waiting Time = (0+11+9+1+5) / 5 = 5.2 ms


Round-Robin Scheduling
P1

P5 P2
• Design for time sharing system
• Similar to FCFS
P4 P3
• Preemptive is added to switch between process.
• CPU scheduler goes around the ready queue, allocating the CPU to each process for time
interval of up to 1 time quantum.
Round-Robin Scheduling Example
Burst 0 4 7 10 14 18 22 26 30
Process
Time P1 P2 P3 P1 P1 P1 P1 P1
P1 24
P2 3 • Turnaround time = Completion time – Arrival time
P3 3 • Waiting time = Turnaround time – Burst time

• Set of process: arrive at time 0, round robin with time quantum takes 4 ms

Turnaround Time P1 = (30-0) = 30 ms Waiting Time P1 = (30-24) = 6 ms


Turnaround Time P2 = (7-0) = 7 ms Waiting Time P2 = (7-3) = 4 ms
Turnaround Time P3 = (10-0) = 10 ms Waiting Time P3 = (10-3) = 7 ms

• Average Waiting Time = (6+4+7) / 3 = 5.6 ms


Note: RR’s time quantum Check 5.7.1
Multilevel Queue Scheduling
• Algorithms for processes are classified into different groups.
RR FCFS
Foreground Processes Background Processes
(Interactive) (Batch)

i.e, Gaming, writing i.e, downloading, run backup


• A multilevel queue scheduling algorithm partitions the ready queue into several separate
queues
• Fixed priority scheduling (serve all from foreground, from background)
• Time slice: eg; 80% to foreground in RR, 20% to background in FCFS
Multilevel Queue Scheduling
Multilevel Feedback Queue Scheduling
Multilevel Feedback Queue Scheduler’s parameter;
• 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 the process needs service

0 Highest priority

1 Medium priority

2 Low priority
5.4 THREAD SCHEDULING

PThreads Scheduling

• Thread Creation: Threads are created using pthread_create.


• Thread Execution: Threads execute concurrently, sharing the same memory space.
• Scheduling: The operating system schedules threads, deciding which one runs next.
• Context Switching: Switching between threads involves saving and restoring thread states.
5.4 THREAD SCHEDULING
PThreads Scheduling Example int main(int argc, char *argv[])
#include <pthread.h> {
#include <stdio.h> pthread_t tid[NUM_THREADS];
#include <stdlib.h> // Array to hold thread identifiers
#define NUM_THREADS 5 int thread_ids[NUM_THREADS];
void *runner(void *param) // Array to hold thread IDs
{ // Create threads

int thread_id = *((int *)param); for (int i = 0; i < NUM_THREADS; i++)

// Get the thread ID from the parameter { thread_ids[i] = i;

printf("Hello from thread %d!\n", thread_id); // Assign a unique ID to each thread


pthread_exit(0); pthread_create(&tid[i], NULL, runner, &thread_ids[i]);
// Create a new thread
}
}// Wait for all threads to finish
for (int i = 0; i < NUM_THREADS; i++)
{ pthread_join(tid[i], NULL); }
return 0; }
Relaxing exercise: Shortest Job First Scheduling (SJF Preemptive)

Draw grant chart and find average waiting time using SJF preemptive.

Arrival Burst
Process
Time Time
P1 2 6
P2 5 2
P3 1 8
P4 0 3
P5 4 4

• Waiting time = Total waiting time – no. of ms process executed - Arrival time
References
Images: Internet
Source: Operating System Concepts (8th Edition)

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy