OS-Pr4-S21-72
OS-Pr4-S21-72
----------------------------------------------------------------------------------------------------------------------------
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.
P1 5
P2 24
P3 16
P4 10
P5 3
/*
Algorithm:
*/
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
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);
// 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;
}
OUTPUT:
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);
// 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;
}
// 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;
}
OUTPUT:
Kunal Motwani S21 72 Practical 4
Conclusion: Hence, we have studied and implemented programs to demonstrate the concept of non-
preemptive scheduling algorithms.