0% found this document useful (0 votes)
13 views8 pages

OS-Pr4-S21-72

The document outlines a practical lab experiment focused on non-preemptive scheduling algorithms, specifically First Come First Served (FCFS) and Shortest Job First (SJF). It includes theoretical explanations, scheduling objectives, and criteria, as well as algorithms and sample programs for both scheduling methods. The conclusion emphasizes the implementation of these concepts through programming exercises.

Uploaded by

motwanikunal63
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)
13 views8 pages

OS-Pr4-S21-72

The document outlines a practical lab experiment focused on non-preemptive scheduling algorithms, specifically First Come First Served (FCFS) and Shortest Job First (SJF). It includes theoretical explanations, scheduling objectives, and criteria, as well as algorithms and sample programs for both scheduling methods. The conclusion emphasizes the implementation of these concepts through programming exercises.

Uploaded by

motwanikunal63
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/ 8

Kunal Motwani S21 72 Practical 4

----------------------------------------------------------------------------------------------------------------------------
Course Code: CSL403 Name: Operating System Lab

Class: SE Semester: IV

EXPERIMENT NO 4
Aim: Write a program to demonstrate the concept of non-preemptive scheduling algorithms.
a. First Come First Served (FCFS)
b. Shortes Job First(SJF) Non-preemptive

Theory:
CPU is always busy in Multiprogramming. Because CPU switches from one job to another job. But in
simple computers CPU sit idle until the I/O request granted. scheduling is a important OS function. All
resources are scheduled before use.(cpu,
memory, devices…..) Process scheduling is an essential part of a
Multiprogramming operating systems. Such operating systems allow more than one process to be loaded
into the executable memory at a time and the loaded process shares the CPU using time multiplexing.

Scheduling Objectives
• Maximize throughput.
• Maximize number of users receiving acceptable response times.
• Be predictable.
• Balance resource use.
• Avoid indefinite postponement.
• Enforce Priorities.
• Give preference to processes holding key resources

SCHEDULING QUEUES:
Process are present in rooms knows as queues.
There are 3 types
1. job queue: when processes enter the system, they are put into a job queue, which consists all processes
in the system. Processes in the job queue reside on mass storage and await the allocation of main memory.
2. ready queue: if a process is present in main memory and is ready to be allocated to cpu for execution,
is kept in ready queue.
3. device queue: if a process is present in waiting state (or) waiting for an i/o event to complete is said to
be in device queue. (or) The processes waiting for a particular I/O device is called device queue

Context Switch: Assume, main memory contains more than one process. If cpu is executing a process, if
time expires or if a high priority process enters into main memory, then the scheduler saves information
about current process in the PCB and switches to execute the another process. The concept of moving CPU
by scheduler from one process to other process is known as context switch.

Non-Preemptive Scheduling: CPU is assigned to one process, CPU do not release until the competition
of that process. The CPU will assigned to some other process only after the previous process has finished.
Kunal Motwani S21 72 Practical 4

Preemptive scheduling: here CPU can release the processes even in the middle of the execution. CPU
received a signal from process p2. OS compares the priorities of p1 ,p2. If p1>p2, CPU continues the
execution of p1. If p1 >p2, CPU continues the execution of p1.

SCHEDULING CRITERIA:
1. Throughput: how many jobs are completed by the cpu with in a time period.
2. Turn around time : The time interval between the submission of the process and time of the completion
is turn around time. TAT = Waiting time in ready queue + executing time + waiting time in waiting queue
for I/O.
3. Waiting time: The time spent by the process to wait for cpu to be allocated.
4. Response time: Time duration between the submission and first response.
5. Cpu Utilization: CPU is costly device, it must be kept as busy aspossible. Eg: CPU efficiency is 90%
means it is busy for 90 units, 10 units idle.

First Come First Served scheduling (FCFS): The process


that request the CPU first is holds the cpu first. If a process request the cpu then it is loaded into the ready
queue, connect CPU to that process. Consider the following set of processes that arrive at time 0, the length
of the cpu burst time given in milli seconds. burst time is the time, required the cpu to execute that job, it
is in milli seconds. Process Burst time(milliseconds)

Process Burst time(milliseconds)

P1 5

P2 24

P3 16

P4 10

P5 3

waiting time= starting time- arrival time

Waiting time for p1=0


Waiting time for p2=5-0=5
Waiting time for p3=29-0=29
Waiting time for p4=45-0=45
Waiting time for p5=55-0=55

Average waiting time= 0+5+29+45+55/5 = 125/5 = 25 ms.


Kunal Motwani S21 72 Practical 4
Turn around time= waiting time + burst time

Turn around time for p1= 0+5=5.


Turn around time for p2=5+24=29
Turn around time for p3=29+16=45
Turn around time for p4=45+10=55
Turn around time for p5= 55+3=58

Average turn around time= (5+29++45+55+58/5) = 187/5 =37.5 millisecounds

/*
Algorithm:

1- Input the processes along with their burst time (bt).


2- Find waiting time (wt) for all processes. 3- As first process that comes need not to
wait so waiting time for process 1 will be 0 i.e. wt[0] = 0. 4- Find waiting time
for all other processes i.e. for process i -> wt[i] = bt[i-1] + wt[i-1] .
5- Find turnaround time = waiting_time + burst_time for all
processes.
6- Find average waiting time = total_waiting_time /
no_of_processes.
7- Find average turnaround time = total_turn_around_time
/ no_of_processes.

*/
Shortes Job First(SJF) Non-preemptive
This algorithm associates with each process the length of the process's next CPU burst. When the CPU
is available, it is assigned to the process that has the smallest next CPU burst. If the next CPU bursts of two
processes are the same, FCFS scheduling is used to break the tie. Scheduling depends on the length of the
next CPU burst of a process, rather than its total length.

Example:

Gantt chart:
Kunal Motwani S21 72 Practical 4
Waiting Time:
Process Waiting Time
P1 3
P2 16
P3 9
P4 0

Average waiting time:

Average waiting time = (3 + 16 + 9 + 0) / 4 = 7 milliseconds.

Advantages:
1. The SJF scheduling algorithm is provably optimal, in that it gives the minimum average waiting time
for a given set of processes.
2. Moving a short process before a long one decreases the waiting time of the short process more than
it increases the waiting time of the long process. Consequently, the average waiting time decreases.
3. The SJF algorithm is optimal.

Disadvantages:
1. The real difficulty with the SJF algorithm is knowing the length of the next CPU request. For long-
term (job) scheduling in a batch system, we can use as the length the process time limit that a user
specifies when he submits the job. Thus, users are motivated to estimate the process time limit
accurately, since a lower value may mean faster response.
2. It cannot be implemented at the level of short-term CPU scheduling. With short-term scheduling,
there is no way to know the length of the next CPU burst.
/** Algorithm:
1. Input:
o Number of processes.
o Arrival times and burst times of the processes.
2. Sort:
o Sort the processes based on their burst times. If two processes have the same burst time, then sort
by their arrival times.
3. Execution:
o The process with the shortest burst time is selected first and executed.
o After executing a process, update the current time.
4. Calculation:
o After executing all processes, calculate waiting time and turnaround time for each process.
5. Output:
o Display the processes in the order of execution, and show their waiting times, turnaround times,
and average waiting/turnaround times.

*/
Kunal Motwani S21 72 Practical 4
Program for FCFS:

#include <stdio.h>

// FCFS code

struct process {
int waiting_time;
int burst_time;
int processor;
int turn_around;
};

void main() {
int n, i;
printf("Enter number of processors: ");
scanf("%d", &n);

struct process p[n];

// Input data
for(i = 0; i < n; i++) {
printf("Enter burst time for processor %d: ", i+1);
scanf("%d", &p[i].burst_time);
p[i].processor = i+1;
}

// Perform FCFS
p[0].waiting_time = 0;
for(i = 1; i < n; i++) {
p[i].waiting_time = p[i-1].waiting_time + p[i-1].burst_time;
}

// Display results
float sum = 0.0 , total = 0.0;
printf("\nProcessor\tBurst Time\tWaiting Time\tTurnaround Time\n");
for(i = 0; i < n; i++) {
p[i].turn_around = p[i].waiting_time + p[i].burst_time;
printf("\t%d\t\t\t%d\t\t\t%d\t\t\t\t%d\n", p[i].processor, p[i].burst_time,
p[i].waiting_time, p[i].turn_around);
sum+= p[i].waiting_time;
total+=p[i].turn_around;
}

printf("\nAverage Waiting Time : %f",sum/n);


printf("\nAverage Turn Around Time : %f",total/n);
}
Kunal Motwani S21 72 Practical 4

OUTPUT:

Program for non-preemptive SJF:


#include <stdio.h>

// Non-Preemptive SJF code

struct process {
int waiting_time;
int burst_time;
int processor;
int turn_around;
};

void main() {
int n, i, j;
printf("Enter number of processors: ");
scanf("%d", &n);

struct process p[n], temp;

// Input data
Kunal Motwani S21 72 Practical 4
for(i = 0; i < n; i++) {
printf("Enter burst time for processor %d: ", i+1);
scanf("%d", &p[i].burst_time);
p[i].processor = i+1;
}

// Sort processes by burst time


for(i = 0; i < n-1; i++) {
for(j = 0; j < n-i-1; j++) {
if(p[j].burst_time > p[j+1].burst_time) {
temp = p[j];
p[j] = p[j+1];
p[j+1] = temp;
}
}
}

// Perform SJF
p[0].waiting_time = 0;
for(i = 1; i < n; i++) {
p[i].waiting_time = p[i-1].waiting_time + p[i-1].burst_time;
}

// Display results
float sum = 0.0, total = 0.0;
printf("\nProcessor\tBurst Time\tWaiting Time\tTurnaround Time\n");
for(i = 0; i < n; i++) {
p[i].turn_around = p[i].waiting_time + p[i].burst_time;
printf("\t%d\t\t\t%d\t\t\t%d\t\t\t\t%d\n", p[i].processor, p[i].burst_time,
p[i].waiting_time, p[i].turn_around);
sum += p[i].waiting_time;
total += p[i].turn_around;
}

printf("\nAverage Waiting Time: %f", sum/n);


printf("\nAverage Turn Around Time: %f", total/n);
}

OUTPUT:
Kunal Motwani S21 72 Practical 4

Conclusion: Hence, we have studied and implemented programs to demonstrate the concept of non-
preemptive scheduling algorithms.

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