C program for Priority Scheduling
by Ranjith | March 29th, 2010.C program for Priority Scheduling
#include<stdio.h> #include<conio.h> #include<iostream.h> void main() { clrscr(); int x,n,p[10],pp[10],pt[10],w[10],t[10],awt,atat,i; printf("Enter the number of process : "); scanf("%d",&n); printf("\n Enter process : time priorities \n"); for(i=0;i<n;i++) { printf("\nProcess no %d : ",i+1); scanf("%d %d",&pt[i],&pp[i]); p[i]=i+1; } for(i=0;i<n-1;i++) { for(int j=i+1;j<n;j++) { if(pp[i]<pp[j]) { x=pp[i]; pp[i]=pp[j]; pp[j]=x; x=pt[i]; pt[i]=pt[j]; pt[j]=x; x=p[i]; p[i]=p[j]; p[j]=x; } } } w[0]=0; awt=0; t[0]=pt[0]; atat=t[0]; for(i=1;i<n;i++) { w[i]=t[i-1]; awt+=w[i]; t[i]=w[i]+pt[i]; atat+=t[i]; } printf("\n\n Job \t Burst Time \t Wait Time \t Turn Around Time Priority \n"); for(i=0;i<n;i++) printf("\n %d \t\t %d \t\t %d \t\t %d \t\t %d \n",p[i],pt[i],w[i],t[i],pp[i]); awt/=n; atat/=n; printf("\n Average Wait Time : %d \n",awt); printf("\n Average Turn Around Time : %d \n",atat); getch(); }
Share and Enjoy:




















June 1st, 2010 at 6:14 pm
//non -preemptive priority
#include
#include
main()
{
int p[10],wt[10],bt[10],tat[10],ttat=0,twt=0,i,j,temp,n,pri[10];
float awt,atat;
clrscr();
printf(“enter the no. of process=>\n”);
scanf(“%d”,&n);
printf(“enter process=>\n”);
for(i=0;i\n”);
for(i=0;i<n;i++)
{
scanf("%d",&bt[i]);
}
printf("Enter the priority\n");
for(i=0;i<n;i++)
{
scanf("%d",&pri[i]);
}
for(i=0;i<n;i++)
{
for(j=0;jpri[j+1])
{
temp=p[j];
p[j]=p[j+1];
p[j+1]=temp;
temp=bt[j];
bt[j]=bt[j+1];
bt[j+1]=temp;
temp=pri[j];
pri[j]=pri[j+1];
pri[j+1]=temp;
}
}
}
wt[0]=0;
for(i=1;i<n;i++)
{
wt[i]=wt[i-1]+bt[i-1];
twt=twt+wt[i];
}
for(i=0;i<n;i++)
{
tat[i]=wt[i]+bt[i];
ttat=ttat+tat[i];
}
awt=(float)twt/n;
atat=(float)ttat/n;
printf("p_no.\tP_bt\t p_wt\t p_tat\n");
for(i=0;i<n;i++)
printf("%d \t%d\t %d\t %d\n",p[i],bt[i],wt[i],tat[i]);
printf("total waiting time=%d\n avg waiting time=%f",twt,awt);
printf("\ntotal turnedaroundtime=%d\n avg turnedaround time=%f",ttat,atat);
getch();
}
June 1st, 2010 at 6:16 pm
#include
#include
void main()
{
int i,j,b[10],temp1,wt[10],tut[10],awt,atut,n,swt=0,stut=0,temp,pt[10];
clrscr();
printf(“Enter the no of jobs:”);
scanf(“%d”,&n);
printf(“Enter the burst time:”);
for(i=1;i<=n;i++)
scanf("%d",&b[i]);
printf("Enter the priorty of jobs:");
for(i=1;i<=n;i++)
scanf("%d",&pt[i]);
for(i=1;i<n;i++)
{
for(j=i;jpt[j])
{
temp=b[i];
b[i]=b[j];
b[j]=temp;
temp1=pt[i];
pt[i]=pt[j];
}
}
}
wt[1]=0;
for(i=2;i<=n;i++)
wt[i]=wt[i-1]+b[i-1];
for(i=1;i<=n;i++)
tut[i]=wt[i]+b[i];
for(i=1;i<=n;i++)
{
swt=swt+wt[i];
stut=stut+tut[i];
}
atut=stut/n;
awt=swt/n;
printf("Avg waiting time=%d\n",awt);
printf("Avg turn arround time=%d\n",atut);
getch();
}
August 2nd, 2010 at 11:31 am
sir i think ur source code is not purely correct
#include
#include
#include
void main()
{
clrscr();
int x,n,p[10],pp[10],pt[10],w[10],t[10],awt,atat,i;
printf(“Enter the number of process : “);
scanf(“%d”,&n);
printf(“\n Enter process : time priorities \n”);
for(i=0;i<n;i++)
{
printf("\nProcess no %d : ",i+1);
scanf("%d %d",&pt[i],&pp[i]);
p[i]=i+1;
}
for(i=0;i<n-1;i++)
{
for(int j=i+1;j<n;j++)
{
if(pp[i]<pp[j])
{
x=pp[i];
pp[i]=pp[j];
pp[j]=x;
x=pt[i];
pt[i]=pt[j];
pt[j]=x;
x=p[i];
p[i]=p[j];
p[j]=x;
}
}
}
w[0]=0;
awt=0;
t[0]=pt[0];
atat=t[0];
for(i=1;i<n;i++)
{
w[i]=t[i-1];
awt+=w[i];
t[i]=w[i]+pt[i];
atat+=t[i];
}
printf("\n\n Job \t Burst Time \t Wait Time \t Turn Around Time Priority \n");
for(i=0;i<n;i++)
printf("\n %d \t\t %d \t\t %d \t\t %d \t\t %d \n",p[i],pt[i],w[i],t[i],pp[i]);
awt/=n;
atat/=n;
printf("\n Average Wait Time : %d \n",awt);
printf("\n Average Turn Around Time : %d \n",atat);
getch();
}