CPU Scheduling Programs

CPU SCHEDULING JAVA PROGRAM,FCFS,SJF,ROUND ROBIN,CPU SCHEDULING PROGRAM DOWNLOAD.

#include<stdio.h>
#include<conio.h>
void fcfs()
{
int n,p[10],bt[10],wt[10],ta[10],twt=0,tat=0;
float awt=0.0,atat=0.0;
clrscr();
printf(" First Come First Served");
 printf("\n\nEnter the number of process:");
 scanf("%d",&n);
 for(int i=0;i  {
  printf("\n Enter burst time for P%d:",i);
  scanf("%d",&bt[i]);
  p[i]=i ;
 }
wt[0]=0;
for(i=0;i {
 wt[i+1]=wt[i]+bt[i];
 ta[i]=wt[i]+bt[i];
}
for(i=0;i {
 twt=twt+wt[i];
 tat=tat+ta[i];
}
awt=twt/n;
atat=tat/n;
   printf("\n");
   printf("Process_no Burst time Wait time Turn around time\n\n");
   for(i=0;i     printf("P%d %d %d %d\n",p[i],bt[i],wt[i],ta[i]);
    printf("Avg wait time is %3.2f\n\nAvg turn around time is %3.2f",awt,atat);


}
void SJFNP()
{
int n,p[10],bt[10],wt[10],ta[10],twt=0,tat=0;
float awt=0.0,atat=0.0;
clrscr();
 printf(" Shortest Job First Non Preemption");
 printf("\n\nEnter the number of process:");
 scanf("%d",&n);
 for(int i=0;i  {
  printf("\n Enter burst time for P%d:",i);
  scanf("%d",&bt[i]);
  p[i]=i;
 }

int j,temp;
wt[0]=0;
for(i=0;i {
 for(j=0;j   {
   if(bt[i]    {
    temp=bt[i];
    bt[i]=bt[j];
    bt[j]=temp;
    temp=p[i];
    p[i]=p[j];
    p[j]=temp;
   }
  }
 }
for(i=0;i {
 wt[i+1]=wt[i]+bt[i];
 ta[i]=wt[i]+bt[i];
}
for(i=0;i {
 twt=twt+wt[i];
 tat=tat+ta[i];
}
awt=(float)twt/n;
atat=(float)tat/n;
printf("\n");
  printf("Process_no Burst time Wait time Turn around time\n\n");
   for(i=0;i     printf("P%d %d %d %d\n",p[i],bt[i],wt[i],ta[i]);
    printf("Avg wait time is %3.2f\n\nAvg turn around time is %3.2f",awt,atat);
}
void SJFP()
{
int n,p[10],bt[10],wt[10],ta[10],ar[10],pn[10],w[10],bt1[10],ar1[10],twt=0,tat=0;
float awt=0.0,atat=0.0;
clrscr();

 printf(" Shortest Job First With Preemption");
 printf("\n\nEnter the number of process:");
 scanf("%d",&n);
 for(int i=0;i  {
  printf("\n Enter burst time for P%d:",i);
  scanf("%d",&bt[i]);
  printf("\n Enter Arrival time for P%d:",i);
  scanf("%d",&ar[i]);
  p[i]=i;
  bt1[i]=bt[i];
  ar1[i]=ar[i];

 }

int j,temp,m=0;
wt[-1]=0;
for(i=0;i {
 for(j=0;j   {
   if(ar[i]    {
    temp=ar[i];
    ar[i]=ar[j];
    ar[j]=temp;
    temp=p[i];
    p[i]=p[j];
    p[j]=temp;
    temp=bt[i];
    bt[i]=bt[j];
    bt[j]=temp;

   }
  }
 }
for(i=0;i {
  pn[i]=p[m];
  w[i]=ar[i+1];
  bt[m]=bt[m]-(ar[i+1]-ar[i]);
  if(bt[m]>bt[i+1])
   m=i+1;
}
for(i=0;i {
 for(j=0;j   {
   if(bt[i]    {
    temp=ar[i];
    ar[i]=ar[j];
    ar[j]=temp;
    temp=p[i];
    p[i]=p[j];
    p[j]=temp;
    temp=bt[i];
    bt[i]=bt[j];
    bt[j]=temp;

   }
  }
 }
for(i=0;i {
 pn[i+n-1]=p[i];
 w[i+n-1]=w[i+n-2]+bt[i];
}
for(i=0;i<(2*n)-1;i++)
  printf("\n pn[%d]=%d \t w[%d]=%d \n",i,pn[i],i,w[i]);
for(i=0;i {
  for(j=2*(n-1);j>=0;j--)
  {
    if(i==pn[j])
    {
      ta[i]=w[j]-ar1[i];
      break;
    }
  }
}
  for(i=0;i {
 wt[i]=ta[i]-bt1[i];
 twt=twt+wt[i];
 tat=tat+ta[i];
}
awt=(float)twt/n;
atat=(float)tat/n;

   printf("\n");
   printf("Process_no Burst time Arrival time Wait time Turn around time\n\n");
   for(i=0;i     printf("P%d %d %d     %d %d\n",p[i],bt1[i],ar1[i],wt[i],ta[i]);
    printf("Avg wait time is %3.2f\n\nAvg turn around time is %3.2f",awt,atat);

}
void PNP()
{
int n,p[10],bt[10],wt[10],ta[10],pr[10],twt=0,tat=0;
float awt=0.0,atat=0.0;
 clrscr();
 printf(" Priority Scheduling With No Preemption");
 printf("\n\nEnter the number of process:");
 scanf("%d",&n);
 for(int i=0;i  {
  printf("\n Enter burst time for P%d:",i);
  scanf("%d",&bt[i]);
  printf("\n Enter Priority for P%d:",i);
  scanf("%d",&pr[i]);
  p[i]=i;
 }

int j,temp;
wt[0]=0;
for(i=0;i {
 for(j=0;j   {
   if(pr[i]    {
    temp=pr[i];
    pr[i]=pr[j];
    pr[j]=temp;
    temp=p[i];
    p[i]=p[j];
    p[j]=temp;
    temp=bt[i];
    bt[i]=bt[j];
    bt[j]=temp;
   }
  }
 }
for(i=0;i {
 wt[i+1]=wt[i]+bt[i];
 ta[i]=wt[i]+bt[i];
}
for(i=0;i {
 twt=twt+wt[i];
 tat=tat+ta[i];
}
awt=twt/n;
atat=tat/n;



   printf("\n");
   printf("Process_no Burst time Priority Wait time Turn around time\n\n");
   for(i=0;i     printf("P%d %d %d %d %d\n",p[i],bt[i],pr[i],wt[i],ta[i]);
    printf("Avg wait time is %3.2f\n\nAvg turn around time is %3.2f",awt,atat);
}

void PWP()
{
int n,p[10],bt[10],wt[10],ta[10],ar[10],pn[10],w[10],bt1[10],ar1[10],pr[10],pr1[10],twt=0,tat=0;
float awt=0.0,atat=0.0;
clrscr();
 printf(" Priority With Preemption");
 printf("\n\nEnter the number of process:");
 scanf("%d",&n);
 for(int i=0;i  {
  printf("\n Enter burst time for P%d:",i);
  scanf("%d",&bt[i]);
  printf("\n Enter Arrival time for P%d:",i);
  scanf("%d",&ar[i]);
  printf("\n Enter Priority for P%d:",i);
  scanf("%d",&pr[i]);
  p[i]=i;
  bt1[i]=bt[i];
  ar1[i]=ar[i];
  pr1[i]=pr[i];
 }


int j,temp,m=0;
wt[-1]=0;
for(i=0;i {
 for(j=0;j   {
   if(ar[i]    {
    temp=ar[i];
    ar[i]=ar[j];
    ar[j]=temp;
    temp=p[i];
    p[i]=p[j];
    p[j]=temp;
    temp=bt[i];
    bt[i]=bt[j];
    bt[j]=temp;
    temp=pr[i];
    pr[i]=pr[j];
    pr[j]=temp;
   }
  }
 }
for(i=0;i {
  pn[i]=p[m];
  w[i]=ar[i+1];
  bt[m]=bt[m]-(ar[i+1]-ar[i]);
  if(pr[m]>pr[i+1])
   m=i+1;
}
for(i=0;i {
 for(j=0;j   {
   if(pr[i]    {
    temp=ar[i];
    ar[i]=ar[j];
    ar[j]=temp;
    temp=p[i];
    p[i]=p[j];
    p[j]=temp;
    temp=bt[i];
    bt[i]=bt[j];
    bt[j]=temp;
    temp=pr[i];
    pr[i]=pr[j];
    pr[j]=temp;
   }
  }
 }
for(i=0;i {
 pn[i+n-1]=p[i];
 w[i+n-1]=w[i+n-2]+bt[i];
}
for(i=0;i<(2*n)-1;i++)
  printf("\n pn[%d]=%d \t w[%d]=%d \n",i,pn[i],i,w[i]);
for(i=0;i {
  for(j=2*(n-1);j>=0;j--)
  {
    if(i==pn[j])
    {
      ta[i]=w[j]-ar1[i];
      break;
    }
  }
}
for(i=0;i {
 wt[i]=ta[i]-bt1[i];
 twt=twt+wt[i];
 tat=tat+ta[i];
}
awt=(float)twt/n;
atat=(float)tat/n;



   printf("\n");
   printf("Process_no Burst time Arrival time Priority Wait time  TAT\n\n");
   for(i=0;i     printf("P%d %d %d %d %d   %d\n",p[i],bt1[i],ar1[i],pr1[i],wt[i],ta[i]);
    printf("Avg wait time is %3.2f\n\nAvg turn around time is %3.2f",awt,atat);
}

void RR()
{

  int st[10],bt[10],wt[10],tat[10],n,tq;
  int i,count=0,swt=0,stat=0,temp,sq=0;
  float awt=0.0,atat=0.0;
  clrscr();
   printf("\n\n\t\t ROUND ROBIN");
  printf("Enter number of processes:");
  scanf("%d",&n);
   for(i=0;i    {
     printf("\nEnter burst time for P%d:",i+1);
     scanf("%d",&bt[i]);
     st[i]=bt[i];
   }
   printf("\nEnter time quantum:");
   scanf("%d",&tq);
   while(1)
   {
     for(i=0,count=0;i      {
       temp=tq;
       if(st[i]==0)
       {
count++;
continue;
       }
       if(st[i]>tq)
st[i]=st[i]-tq;
       else
if(st[i]>=0)
{
 temp=st[i];
 st[i]=0;
}
sq=sq+temp;
tat[i]=sq;
     }
     if(n==count)
     break;
   }
   for(i=0;i    {
    wt[i]=tat[i]-bt[i];
    swt=swt+wt[i];
    stat=stat+tat[i];
   }
   awt=(float)swt/n;
   atat=(float)stat/n;
   printf("\n");
   printf("Process_no Burst time Wait time Turn around time\n\n");
   for(i=0;i     printf("P%d %d %d %d\n",i+1,bt[i],wt[i],tat[i]);
    printf("Avg wait time is %3.2f\n\nAvg turn around time is %3.2f",awt,atat);
    getch();
}
void main()
{
int ch;
clrscr();
do
{
printf("\n1.FCFS \n2.SJF-With no pre-emption \n3.SJF-With Pre-emption ");
printf("\n4.Priority With no pre-emption \n5.Priority With pre-emption \n6.Round Robin \n7.Exit");
printf("\n\nEnter your choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:
fcfs();
break;
case 2:
SJFNP();
break;
case 3:
SJFP();
break;
case 4:
PNP();
break;
case 5:
PWP();
break;
case 6:
RR();
break;
case 7:
break;
}
}while(ch<7);
}



Output
-------

1.FCFS
2.SJF-With no pre-emption
3.SJF-With Pre-emption
4.Priority With no pre-emption
5.Priority With pre-emption
6.Round Robin
7.Exit
Enter your choice:1
                First Come First Served
Enter the number of process:2
 Enter burst time for P0:2
 Enter burst time for P1:3
Process_no      Burst time      Wait time       Turn around time
P0              2               0               2
P1              3               2               5
Avg wait time is 1.00
Avg turn around time is 3.00
1.FCFS
2.SJF-With no pre-emption
3.SJF-With Pre-emption
4.Priority With no pre-emption
5.Priority With pre-emption
6.Round Robin
7.Exit
Enter your choice:2
                Shortest Job First Non Preemption
Enter the number of process:2
 Enter burst time for P0:2
 Enter burst time for P1:3
Process_no      Burst time      Wait time       Turn around time
P0              2               0               2
P1              3               2               5
Avg wait time is 1.00
Avg turn around time is 3.50
1.FCFS
2.SJF-With no pre-emption
3.SJF-With Pre-emption
4.Priority With no pre-emption
5.Priority With pre-emption
6.Round Robin
7.Exit
Enter your choice:3
                Shortest Job First With Preemption
Enter the number of process:2
 Enter burst time for P0:5
 Enter Arrival time for P0:1
 Enter burst time for P1:3
Enter Arrival time for P1:0
 pn[0]=1         w[0]=1
 pn[1]=1         w[1]=3
 pn[2]=0         w[2]=8
Process_no      Burst time      Arrival time    Wait time       Turn around time
P1                 5                 1                         2                        7
P0                           3                 0                         0                        3
Avg wait time is 1.00
Avg turn around time is 5.00
1.FCFS
2.SJF-With no pre-emption
3.SJF-With Pre-emption
4.Priority With no pre-emption
5.Priority With pre-emption
6.Round Robin
7.Exit
Enter your choice:4
                Priority Scheduling With No Preemption
Enter the number of process:2
 Enter burst time for P0:2
 Enter Priority for P0:1
 Enter burst time for P1:3
 Enter Priority for P1:2
Process_no      Burst time      Priority        Wait time       Turn around time
P0              2               1               0              2
P1              3                  2              2                         5
Avg wait time is 1.00
Avg turn around time is 3.00
1.FCFS
2.SJF-With no pre-emption
3.SJF-With Pre-emption
4.Priority With no pre-emption
5.Priority With pre-emption
6.Round Robin
7.Exit
Enter your choice:5
                Priority With Preemption
Enter the number of process:2
 Enter burst time for P0:6
 Enter Arrival time for P0:0
 Enter Priority for P0:2
 Enter burst time for P1:8
 Enter Arrival time for P1:1
 Enter Priority for P1:1
 pn[0]=0         w[0]=1
 pn[1]=1         w[1]=9
 pn[2]=0         w[2]=14
Process_no      Burst time      Arrival time    Priority        Wait time   TAT
P1               6               0               2              8          14
P0               8               1               1               0          8
Avg wait time is 4.00
Avg turn around time is 11.00
1.FCFS
2.SJF-With no pre-emption
3.SJF-With Pre-emption
4.Priority With no pre-emption
5.Priority With pre-emption
6.Round Robin
7.Exit
Enter your choice:6
Enter number of processes:2
Enter burst time for P1:2
Enter burst time for P2:3
Enter time quantum:1
Process_no      Burst time      Wait time       Turn around time
P1             2              1                  3
P2             3                 2                  5
Avg wait time is 1.50
Avg turn around time is 4.00
1.FCFS
2.SJF-With no pre-emption
3.SJF-With Pre-emption
4.Priority With no pre-emption
5.Priority With pre-emption
6.Round Robin
7.Exit
Enter your choice:7

No comments: