Search This Blog

Friday, November 25, 2011

PRIORITY SCHEDULING ALGORITHM IN C

#include<stdio.h>
#include<conio.h>

int main()
 {
    int temp,temp1,n,pp[10],bt[10],waiting[10],awt=0,i,twt=0,j;
   printf("Enter the number of process : ");
   scanf("%d",&n);
 
   for(i=0;i<n;i++)
    {
      printf("\n Enter process burst time");

      scanf("%d",&bt[i]);
            printf(" : time priorities \n");
            scanf("%d",&pp[i]);
   
    }
  for(i=0;i<n-1;i++)
   {
     for(j=i+1;j<n;j++)
     {
       if(pp[i]>pp[j])
       {
         temp=pp[i];
         pp[i]=pp[j];
         pp[j]=temp;
         temp1=bt[i];
         bt[i]=bt[j];
         bt[j]=temp1;
   
      }
   }
}
waiting[0]=0;
 for(i=1;i<n;i++)
                    {
                                     waiting[i]=waiting[i-1]+bt[i-1];
                                     twt=twt+waiting[i];
                                     }
                                   
                                                          printf("\ntotal waiting time=%d",twt);
                                    
                                                      awt=twt/n;
                                     printf("\nAverage waiting time=%d",awt);

getch();
}

SJF SCHEDULING ALGORITHM IN C

#include<stdio.h>
#include<conio.h>
#include<process.h>
int main()
{
char temp[5];
int tot=0,wt[10],bt[10],i,j,n,temp1;
int avg=0;

printf("enter no of processes:");
scanf("%d",&n);
printf("enter burst time");
for(i=0;i<n;i++)
{
scanf("%d",&bt[i]);
}
for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
{
if(bt[i]>bt[j])
{
temp1=bt[i];
bt[i]=bt[j];
bt[j]=temp1;

}
}
}
wt[0]=0;
for(i=1;i<n;i++)
{
wt[i]=wt[i-1]+bt[i-1];
tot=tot+wt[i];
}
avg=tot/n;
printf("total waiting time=%d\n avg waiting time=%d",tot,avg);
getch();
}

FCFS SCHEDULING ALGORITHM IN C

#include<stdio.h>
#include<conio.h>
#include<process.h>
int main()
{
    int n,i,burst[100],awt=0,twt=0,waiting[100];
    printf("Enter number of processes");
    scanf("%d",&n);
    printf("Enter burst time");
    for(i=0;i<n;i++)
    {
                    scanf("%d",&burst[i]);
                    }
                    waiting[0]=0;
                    for(i=1;i<n;i++)
                    {
                                     waiting[i]=waiting[i-1]+burst[i-1];
                                     twt=twt+waiting[i];
                                     }
                                    
                                                          printf("\ntotal waiting time=%d",twt);
                                     
                                                      awt=twt/n;
                                     printf("\nAverage waiting time=%d",awt);
                                     getch();
                                     }