#Include #Include Struct Int: Stdio.h Stdlib.h

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 7

#include<stdio.

h>
#include<stdlib.h>

struct Process
{
  int pno,first_bt,next_bt,at,ft,tat,wt,temp_bt;
}P[100];

struct Schedule
{
  int pid,endtime;
}sch[100];

int sch_cnt; //Schedule Counter


int ct; //Current time
int np; //Number of processes
int i;
int tq; //time quantum

int ind = 0;
void accept();
void showGanttChart();
void showPT();

void sort()
{
int temp,j;

for(i=0;i<np;i++)// numbers
{
for(j=0;j<np-1-i;j++)//particular iteration
if (P[j].at>P[j+1].at)//p0 = 2 p1 = 0
{
temp = P[j].pno;
P[j].pno = P[j+1].pno;
P[j+1].pno = temp;

temp = P[j].first_bt;
P[j].first_bt = P[j+1].first_bt;
P[j+1].first_bt = temp;

temp = P[j].next_bt;
P[j].next_bt = P[j+1].next_bt;
P[j+1].next_bt = temp;

temp = P[j].at;
P[j].at = P[j+1].at;
P[j+1].at = temp;

temp = P[j].ft;
P[j].ft = P[j+1].ft;
P[j+1].ft = temp;

temp = P[j].tat;
P[j].tat = P[j+1].tat;
P[j+1].tat = temp;

temp = P[j].wt;
P[j].wt = P[j+1].wt;
P[j+1].wt = temp;

temp = P[j].temp_bt;
P[j].temp_bt = P[j+1].temp_bt;
P[j+1].temp_bt = temp;

}
}
}

int main()
{
int flag=0,x;
  accept();
sort();//bubble
ind = 0;
  while(1)
  {
//logic to get process number for execution
   for(i=0;i<np;i++)
{
if(P[ind].at<=ct && P[ind].temp_bt>0)
{
x = ind;
ind = (ind + 1) % np;
break;
}
ind = (ind + 1) % np;
}
// printf("\n x = %d pronum = %d",x,P[x].pno);
flag =0;
for(i=0;i<np;i++)
{
if(P[i].temp_bt > 0)
{
flag = 1; break;
}
}
if(flag==0) //if no process has remain
      break;
//flag =0;

    sch[sch_cnt].pid=P[x].pno; // gantt chart


/*p1 = 3 tq=2*/
if(P[x].temp_bt<tq)
    {
      ct +=P[x].temp_bt; //ct+ct +1
      P[x].temp_bt=0;
    }
    else
    {
      ct +=tq;
      P[x].temp_bt-=tq;
    }
    if(P[x].temp_bt==0)
      P[x].ft=ct;
    sch[sch_cnt].endtime=ct;
    sch_cnt++;
  }

  for(i=0;i<np;i++)
  {
    P[i].temp_bt=P[i].next_bt;
  }
flag = 0;
  while(1)
  {
  for(i=0;i<np;i++)
{
if(P[ind].at<=ct && P[ind].temp_bt>0)
{
x = ind;
ind = (ind + 1) % np;
break;
}
ind = (ind + 1) % np;
}
//printf("\n x = %d pronum = %d",x,P[x].pno);
for(i=0;i<np;i++)
{
if(P[i].temp_bt > 0)
{
flag = 1; break;
}
}
    if(flag==0) //if no process then flag = 0
      break;
flag =0;

    sch[sch_cnt].pid=P[x].pno;
    if(P[x].temp_bt<tq)
    {
      ct+=P[x].temp_bt;
      P[x].temp_bt=0;
    }
    else
    {
      ct+=tq;
      P[x].temp_bt-=tq;
    }
    P[x].ft=ct;
    sch[sch_cnt].endtime=ct;
    sch_cnt++;
    if(P[x].temp_bt==0)
    {
P[x].tat=P[x].ft-P[x].at;
     P[x].wt=P[x].tat-P[x].first_bt-P[x].next_bt-2;
}
  }

  showGanttChart();
  showPT();
}

void accept()
{
  printf("Enter the number of processes:");
  scanf("%d",&np);

  for(i=0;i<np;i++)
  {

    printf("Enter the process number:");


    scanf("%d",&P[i].pno);// p0

    printf("Enter the arrival time:");


    scanf("%d",&P[i].at);//0

    printf("Enter the first burst time:");


    scanf("%d",&P[i].first_bt);//6
    P[i].temp_bt=P[i].first_bt;
    P[i].next_bt=rand()%5+1;
  }
printf("Enter the time quantum:");
scanf("%d",&tq);//2
}

void showGanttChart()
{
  for(i=0;i<sch_cnt*5;i++)
  printf("-");
  printf("\n|");
  
for(i=0;i<sch_cnt;i++)
  printf(" P%d |",sch[i].pid);
  printf("\n");
  
for(i=0;i<sch_cnt*5;i++)
  printf("-");
  printf("\n0");

  for(i=0;i<sch_cnt;i++)
  printf("%5d",sch[i].endtime);
}

void showPT()
{
  float avgtat=0,avgwt=0;
printf("\n");
  printf("\nPID\tFBT\tNBT\tAT\tFT\tTAT\tWT\n");
  for(i=0;i<np;i++)
  {
    printf("\n%d\t%d\t%d\t%d\t%d\t%d\t%d\
n",P[i].pno,P[i].first_bt,P[i].next_bt,P[i].at,P[i].ft,P[i].
tat,P[i].wt);
    avgtat+=P[i].tat;
    avgwt+=P[i].wt;
  }
  avgtat=avgtat/np;
  avgwt=avgwt/np;

  printf("Average turn arount time:%f",avgtat);


  printf("\nAverage waiting time:%f\n",avgwt);
}

You might also like