OS-(I) Assignment 3: Set A- 1)

(1) Write the program to simulate FCFS CPU-scheduling. The arrival time and first CPU- burst for different n number of processes should be input to the algorithm. Assume that the fixed IO waiting time (2 units). The next CPU-burst should be generated randomly. The output should give Gantt chart, turnaround time and waiting time for each process. Also find the average waiting time and turnaround time.

Solution:
#include<stdio.h>
int findWaitingTime(int bt[],int wt[],int n)
{
    int wtavg=0,i;
    wt[0]=0;
    for(i=1;i<n;i++)
    {
       wt[i]=bt[i-1]+wt[i-1]+2;
       wtavg=wtavg+wt[i];
    }
    return(wtavg);
}
int findTurnAroundTime(int bt[],int wt[],int tat[],int n)
{
    int tatavg=0,i;
    for (i=0;i<n;i++)
    {
       tat[i]=bt[i]+wt[i];
       tatavg=tatavg+tat[i];
    }
    return(tatavg);
}
void main()
{
   int at[10],bt[10],wt[10],tat[10],i,n;
   int avgwt,avgtat;
   printf("\nEnter the number of process\n");
   scanf("%d",&n);
   for(i=0;i<n;i++)
   {
      bt[i]=rand()%10;
   }
  for(i=0;i<n;i++)
  {
     printf("%d\t",bt[i]);
  } 
   avgwt=findWaitingTime(bt,wt,n)/n;
   avgtat=findTurnAroundTime(bt,wt,tat,n)/n;
   printf("\n\nP|\tBT\tWT\tTAT|\n");
   for(i=0;i<n;i++)
   {
      printf("%d",i);
      printf("\t%d",bt[i]);
      printf("\t%d",wt[i]);
      printf("\t%d\n",tat[i]);
   }
   printf("\nGantt Chart\n");
   for(i=0;i<n;i++)
   {
       printf("|   P%d\t",i);
   }
   printf("|\n");
   for(i=0;i<n;i++)
   {
      printf("%d\t ",wt[i]);
   }
   printf("%d\n",tat[i-1]);
   printf("Average Waiting Time=%d",avgwt);
   printf("\n");
   printf("Average Turn Around Time=%d",avgtat);
}

0 Comments:

Post a Comment