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

Expt No. 5

C Program to implement non preemptive priority scheduling algorithm
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 views5 pages

Expt No. 5

C Program to implement non preemptive priority scheduling algorithm
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/ 5

C Program to implement non preemptive priority scheduling algorithm

#include <stdio.h>

int main()
{
int bt[20],p[20],wt[20],tat[20],pr[20],i,j,n,total=0,pos,temp,avg_wt,avg_tat; printf("Enter
Total Number of Process:");
scanf("%d",&n);

printf("\nEnter Burst Time and Priority\n");


for(i=0;i<n;i++)
{
printf("\nP[%d]\n",i+1);
printf("Burst Time:");
scanf("%d",&bt[i]);
printf("Priority:");
scanf("%d",&pr[i]);
p[i]=i+1; //contains process number
}

//sorting burst time, priority and process number in ascending order using selection sort for(i=0;i<n;i++)
{
pos=i;
for(j=i+1;j<n;j++)
{
if(pr[j]<pr[pos])
pos=j;
}

temp=pr[i];
pr[i]=pr[pos];
pr[pos]=temp;

temp=bt[i];
bt[i]=bt[pos];
bt[pos]=temp;

temp=p[i];
p[i]=p[pos];
p[pos]=temp;
}

wt[0]=0; //waiting time for first process is zero


//calculate waiting time
for(i=1;i<n;i++)
{
wt[i]=0;
for(j=0;j<i;j++)
wt[i]+=bt[j];

total+=wt[i];
}

avg_wt=total/n; //average waiting time


total=0;

printf("\nProcess\t Burst Time \tWaiting Time\tTurnaround Time"); for(i=0;i<n;i++)


{
tat[i]=bt[i]+wt[i]; //calculate turnaround time total+=tat[i];
printf("\nP[%d]\t\t %d\t\t %d\t\t\t%d",p[i],bt[i],wt[i],tat[i]); }

avg_tat=total/n; //average turnaround time printf("\n\nAverage


Waiting Time=%d",avg_wt);
printf("\nAverage Turnaround Time=%d\n",avg_tat); return 0;
}
OP:
b)Write a program to demonstrate the concept of Preemptive Scheduling Algorithm.

Preemptive Scheduling:
Preemptive scheduling is used when a process switches from running state to ready state or from the waiting
state to ready state. The resources (mainly CPU cycles) are allocated to the process for a limited amount of
time and then taken away, and the process is again placed back in the ready queue if that process still has
CPU burst time remaining. That process stays in the ready queue till it gets its next chance to
execute.Algorithms based on preemptive scheduling are Round Robin Shortest Remaining Time
first(SRTF),Priority

What is Round Robin Scheduling?


Round Robin Scheduling is a scheduling algorithm used by the system to schedule CPU utilization.
This is a preemptive algorithm. There exist a fixed time slice associated with each
request called the quantum. The job scheduler saves the progress of the job that is being executed currently
and moves to the next job present in the queue when a particular process is executed for a given time quantum.
No process will hold the CPU for a long time. The switching is called a context switch. It is probably one
of the best scheduling algorithms. The efficiency of this algorithm depends on the quantum value.

ROUND ROBIN SCHEDULING ALGORITHM We first have a queue where the processes are
arranged in first come first serve order. A quantum value is allocated to execute each process.
The first process is executed until the end of the quantum value. After this, an interrupt is generated
and the state is saved.
The CPU then moves to the next process and the same method is followed. Same steps
are repeated till all the processes are over.

ADVANTAGES:
Low overhead for decision making.
Unlike other algorithms, it gives equal priority to all processes.
Starvation rarely occurs in this process.

DISADVANTAGES:
The efficiency of the system is decreased if the quantum value is low as frequent switching takes
place.
The system may become unresponsive if the quantum value is high.
How to compute below times in Round Robin using a program?

1. Completion Time: Time at which process completes its execution.


2. Turn Around Time: Time Difference between completion time and arrival time. Turn Around
Time = Completion Time – Arrival Time
3. Waiting Time(W.T): Time Difference between turn around time and burst time. Waiting
Time = Turn Around Time – Burst Time
#include<stdio.h>

int main()
{
int i, limit, total = 0, x, counter = 0, time_quantum;
int wait_time = 0, turnaround_time = 0, arrival_time[10], burst_time[10], temp[10]; float
average_wait_time, average_turnaround_time;
printf("Enter Total Number of Processes:");
scanf("%d", &limit);
x = limit;
for(i = 0; i < limit; i++)
{
printf("Enter Details of Process[%d]\t", i + 1);

printf("Arrival Time:\t");

scanf("%d", &arrival_time[i]);

printf("Burst Time:\t");

scanf("%d", &burst_time[i]);

temp[i] = burst_time[i];
}

printf("Enter Time Quantum:\t");


scanf("%d", &time_quantum);
printf("nProcess IDttBurst Timet Turnaround Timet Waiting Timen"); for(total = 0, i
= 0; x != 0;)
{
if(temp[i] <= time_quantum && temp[i] > 0)
{
total = total + temp[i];
temp[i] = 0;
counter = 1;
}
else if(temp[i] > 0)
{
temp[i] = temp[i] - time_quantum;
total = total + time_quantum;
}
if(temp[i] == 0 && counter == 1)
{
x--;
printf("Process[%d]\t\t%d\t\t %d\t\t\t %d", i + 1, burst_time[i], total - arrival_time[i], total -
arrival_time[i] - burst_time[i]);
wait_time = wait_time + total - arrival_time[i] - burst_time[i]; turnaround_time =
turnaround_time + total - arrival_time[i]; counter = 0;
}
if(i == limit - 1)
{
i = 0;
}
else if(arrival_time[i + 1] <= total)
{
i++;
}
else
{
i = 0;
}
}
average_wait_time = wait_time * 1.0 / limit;
average_turnaround_time = turnaround_time * 1.0 / limit;
printf("Average Waiting Time:\t%f", average_wait_time);
printf("Avg Turnaround Time:\t%f", average_turnaround_time); return 0;
}

OP:

Conclusion:- Hence we hve studied the concept of non-preemptive and Preemptive Scheduling
Algorithm.Scheduling Algorithm and successfully.

SIGN GRADEDATE

You might also like