c code for sjf non preemptive

mail@pastecode.io avatar
unknown
plain_text
a month ago
1.6 kB
2
Indexable
Never
#include<stdio.h>

void swap(int p[],int i,int j){
 int temp=p[i];
 p[i]=p[j];
 p[j]=temp;
 }
 
void main(){
 int n,pos,min=1000;
 printf("Enter the number of processes");
 scanf("%d",&n);
 int p[n],at[n],bt[n],ct[n],tat[n],wt[n];

printf("Enter the processes\n ");
 for(int i=0;i<n;i++){
 scanf("%d",&p[i]);
 }

 printf("Enter the arrival time of\n ");
 for(int i=0;i<n;i++){
 printf("process %d :",(i+1));
 scanf("%d",&at[i]);
 }
 printf("Enter the burst time of\n ");
 for(int i=0;i<n;i++){
 printf("process %d :",(i+1));
 scanf("%d",&bt[i]);
 }
 
 for(int i=0;i<n;i++){
  for(int j=i+1;j<n;j++){
   if(at[i]>at[j]){
    swap(p,i,j);
    swap(at,i,j);
    swap(bt,i,j);
    }
   else if(at[i]==at[j])
   {
    if(bt[i]>bt[j]){
     swap(p,i,j);
     swap(at,i,j);
     swap(bt,i,j);
     }
   }
  }
 }
     ct[0]=at[0] + bt[0];
  for(int i=1; i<n; i++)
  {
	for(int j=i; j<n; j++)
	{
	    if(at[j]<=ct[i-1])
	   {
              if(bt[j]<min)
              {
                 min=bt[j];
                 pos=j;
              }
	   }
	}
    swap(p,i,pos);
    swap(at,i,pos);
    swap(bt,i,pos);
    min=1000;
    ct[i]=ct[i-1]+bt[i];
   } 
     
   for(int i=0;i<n;i++)
   {
	tat[i]=ct[i]-at[i];
	wt[i]=tat[i]-bt[i];
   }
 float awt=0, atat=0;
 printf("\np\t at\t bt\t ct\t tat\t wt"); 
  for(int i=0;i<n;i++)
  {
    printf("\n%d\t %d\t %d\t %d\t %d\t %d",p[i], at[i], bt[i], ct[i], tat[i], wt[i]);
  }
  for(int i=0;i<n;i++)
  { 
    atat+=tat[i];
    awt+=wt[i];
  }
  atat=atat/n;
  awt=awt/n;
  printf("\n avg tat=%.2f and avg wt=%.2f",atat, awt); 
 }
 
 
Leave a Comment