Introduction to non-preemptive priority scheduling :
The SJF algorithm is a special case of the general priority-scheduling algorithm. A priority is associated with each process, and the CPU is allocated to the process with the highest priority.Equal-priority processes are scheduled in FCFS order. An SJF algorithm is simply a priority algorithm where the priority (p) is the inverse of the (predicted) next CPU burst. The larger the CPU burst, the lower the priority, and vice versa. Non-preemptive execution means the execution of a process will not be deferred at any time.
C program for non-preemptive priority scheduling :
#include<stdio.h>
int main()
{
int at[10],bt[10],pr[10],priority[10];
int n,i,j,temp,time=0,count,over=0,sum_wait=0,sum_turnaround=0,start;
float avgwait,avgturn;
printf("Enter the number of processes\n");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter the arrival time,priority and execution time for process %d\n",i+1);
scanf("%d%d%d",&at[i],&priority[i],&bt[i]);
pr[i]=i+1;
}
for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
{
if(at[i]>at[j])
{
temp=priority[i];
priority[i]=priority[j];
priority[j]=temp;
temp=at[i];
at[i]=at[j];
at[j]=temp;
temp=bt[i];
bt[i]=bt[j];
bt[j]=temp;
temp=pr[i];
pr[i]=pr[j];
pr[j]=temp;
}
}
}
printf("\n\nProcess\t|Arrival time\t|Priority\t|Execution time\t|Start time\t|End time\t|
waiting time\t|Turnaround time\n\n");
while(over<n)
{
count=0;
for(i=over;i<n;i++)
{
if(at[i]<=time)
count++;
else
break;
}
if(count>1)
{
for(i=over;i<over+count-1;i++)
{
for(j=i+1;j<over+count;j++)
{
if(priority[i]>priority[j])
{
temp=priority[i];
priority[i]=priority[j];
priority[j]=temp;
temp=at[i];
at[i]=at[j];
at[j]=temp;
temp=bt[i];
bt[i]=bt[j];
bt[j]=temp;
temp=pr[i];
pr[i]=pr[j];
pr[j]=temp;
}
}
}
}
start=time;
time+=bt[over];
printf("p[%d]\t|\t%d\t|\t%d\t|\t%d\t|\t%d\t|\t%d\t|\t%d\t|\t%d\n",pr[over],at[over],
priority[over],bt[over],start,time,time-at[over]-bt[over],time-at[over]);
sum_wait+=time-at[over]-bt[over];
sum_turnaround+=time-at[over];
over++;
}
avgwait=(float)sum_wait/(float)n;
avgturn=(float)sum_turnaround/(float)n;
printf("Total waitinf time is %d\n",sum_wait);
printf("Average waiting time is %f\n",avgwait);
printf("Total turnaround time is %d\n",sum_turnaround);
printf("Average turnaround time is %f\n",avgturn);
return 0;
}
OUTPUT :
The SJF algorithm is a special case of the general priority-scheduling algorithm. A priority is associated with each process, and the CPU is allocated to the process with the highest priority.Equal-priority processes are scheduled in FCFS order. An SJF algorithm is simply a priority algorithm where the priority (p) is the inverse of the (predicted) next CPU burst. The larger the CPU burst, the lower the priority, and vice versa. Non-preemptive execution means the execution of a process will not be deferred at any time.
C program for non-preemptive priority scheduling :
#include<stdio.h>
int main()
{
int at[10],bt[10],pr[10],priority[10];
int n,i,j,temp,time=0,count,over=0,sum_wait=0,sum_turnaround=0,start;
float avgwait,avgturn;
printf("Enter the number of processes\n");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter the arrival time,priority and execution time for process %d\n",i+1);
scanf("%d%d%d",&at[i],&priority[i],&bt[i]);
pr[i]=i+1;
}
for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
{
if(at[i]>at[j])
{
temp=priority[i];
priority[i]=priority[j];
priority[j]=temp;
temp=at[i];
at[i]=at[j];
at[j]=temp;
temp=bt[i];
bt[i]=bt[j];
bt[j]=temp;
temp=pr[i];
pr[i]=pr[j];
pr[j]=temp;
}
}
}
printf("\n\nProcess\t|Arrival time\t|Priority\t|Execution time\t|Start time\t|End time\t|
waiting time\t|Turnaround time\n\n");
while(over<n)
{
count=0;
for(i=over;i<n;i++)
{
if(at[i]<=time)
count++;
else
break;
}
if(count>1)
{
for(i=over;i<over+count-1;i++)
{
for(j=i+1;j<over+count;j++)
{
if(priority[i]>priority[j])
{
temp=priority[i];
priority[i]=priority[j];
priority[j]=temp;
temp=at[i];
at[i]=at[j];
at[j]=temp;
temp=bt[i];
bt[i]=bt[j];
bt[j]=temp;
temp=pr[i];
pr[i]=pr[j];
pr[j]=temp;
}
}
}
}
start=time;
time+=bt[over];
printf("p[%d]\t|\t%d\t|\t%d\t|\t%d\t|\t%d\t|\t%d\t|\t%d\t|\t%d\n",pr[over],at[over],
priority[over],bt[over],start,time,time-at[over]-bt[over],time-at[over]);
sum_wait+=time-at[over]-bt[over];
sum_turnaround+=time-at[over];
over++;
}
avgwait=(float)sum_wait/(float)n;
avgturn=(float)sum_turnaround/(float)n;
printf("Total waitinf time is %d\n",sum_wait);
printf("Average waiting time is %f\n",avgwait);
printf("Total turnaround time is %d\n",sum_turnaround);
printf("Average turnaround time is %f\n",avgturn);
return 0;
}
No comments:
Post a Comment