# SIMULATION OF PRIORITY SCHEDULING(PR) IN PROCESS SCHEDULING

AIM:
To write a c program in UNIX environment to implement the following scheduling discipline Operating System (OS) LAB in LINUX environment .
Priority Scheduling (PR)
ALGORITHM:
· Get the number of process from the user.
· Get also CPU service time for each process from the user.
· Sort the CPU time of process according to the process priority in ascending order
· Now for each process the waiting time is equivalent to CPU time of previous process.
· The ratio of waiting time of all the process to the number of process will give the average waiting time.
· Display the result.
# include<stdio.h>
main()
{
int j,i,k=0,ptime[25],n,s=0,I,sum=0;
char name[25][25];
int t,p,time[10],pos[10],priority[25];
float avg;
printf ("enter the no. of process: \t");
scanf ("%d",&n);
for(i=0;i<n;i++)
{
printf("enter the name for processes: \t");
printf("%d \t",i+1);
scanf("%s",name[i]);
}
printf("\n \n");

for(i=0;i<n;i++)
{
printf("enter the process time: \t");
printf("%s \t",name[i]);
scanf("%d",&ptime[i]);
}
printf("\n \n");
for(i=0;i<n;i++)
{
printf("enter the priority for process: \t");
printf("%d \t",i+1);
scanf("%d",&priority[i]);
}
printf("\n process – name \t process – time \n");
for(i=0;i<n;i++)
{
printf("\t %s \t \t %d \t\t %d\n",name[i],ptime[i],priority[i]);
}
printf("\n \n PRIORITY SCHEDULING \n \n");
for(i=0;i<n;i++)
{
pos[i]=i;
time[i]=ptime[i];
}
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
p=time[i];
time[i]=time[j];
time[j]=p;
t=pos[i];
pos[i]=pos[j];
pos[j]=t;
p=priority[i];
priority[i]=priority[j];
priority[j]=p;
}
}

for(i=0;i<n;i++)
{
printf("process %s from %d to %d \n",name,pos[i],k,(k+time[i]));
k+=time[i];
}
for(i=0;i<(n-1);i++)
{
s+=time[i];
sum+=s;
}
avg=(float)sum/n;
printf("\n\n average waiting time: \t");
printf("%2fmsec",avg);
sum=avg=s=0;
for(i=0;i<n;i++)
{
s+=ptime[i];
sum+=s;
}
avg=(float)sum/n;
printf("\n turn around time is \t");
printf("%2fmsec",avg);
return 0;
}
CONCLUSION:
Thus the c program for priority scheduling discipline was written and executed successfully.