Wednesday, October 10, 2012

FCFS and Round Robin Schedular Implementation in C | Operating System

Write the programs to implement the below given CPU scheduling algorithms.
1.  FCFS
2.  RR
Take the inputs, Process name, Arrival time of the process, CPU execution time of
the process. For implementing RR additional input,  the desired time quantum should be
taken from the user. Calculate waiting time, turn around time for each
process.


Solution :-
The given program solution using Structure calculates waiting time and turn around time for each process with idel time gives required output.



/* Program For FCFS and RR Implementation */
#include<stdio.h>
int ttime=0,ir,jr,tempr,kr,idel=0,tatr=0;


struct CpuSch
{
int at,xt;//arrival time ,execution time
char name[20];
//int end;
};

int main()
{
int np;
printf("Please Enter No of Process\n");
scanf("%d",&np);
int i;

struct CpuSch prs[np],temp;
// Take input for no of Process in Structure
for(i=0;i<np;i++)
{
printf("Please Enter %d  Process Name \n",i+1);
scanf("%s",prs[i].name);
printf("Please Enter Arrival time for  %s\n",prs[i].name);
scanf("%d",&prs[i].at);
printf("Please Enter Execution time for %s\n",prs[i].name);
scanf("%d",&prs[i].xt);
}
int a,b;

//Sorting Process According to Arrival time
for(a=0;a<np;a++)
{
 for(b=0;b<np-1;b++)
 {
 if(prs[b].at>prs[b+1].at)
 {
 temp=prs[b];
 prs[b]=prs[b+1];
 prs[b+1]=temp;
 }
 }
}

int j,sum;
/************* FCFS for wait time tat***********************/
int wtm,tatm,ft,itm;//waiting time turnAroundtime finish time ideal time
wtm=0;
tatm=0;
ft=prs[0].at;
printf("\n\t******FCFS SCHEDULING******\n");
printf("Process No\tProcess Name\tArrival Time\tExecution Time\tWaitingTime\tTurnAroundTime\n");
printf("----------\t------------\t------------\t--------------\t-----------\t--------------\n");
for(j=0;j<np;j++)
{
itm=0;
wtm=ft-prs[j].at;

if(wtm<0)
{
itm=(-1)*wtm;
wtm=0;
} 

ft=ft+prs[j].xt+itm;
tatm=ft-prs[j].at;
printf("%8d %15s %15d %15d %15d %15d\n",j+1,prs[j].name,prs[j].at,prs[j].xt,wtm,tatm);
}
printf("\nTotal Time:-%d\n",ft);
/********************************************************/
/*************Round Robin********************************/
 int tslice,ftr[10];
            jr=0;
  printf("\n\t******ROUND ROBIN SCHEDULING******\n");
            printf("Enter the time slice:\n");
            scanf("%d",&tslice);
            printf("PROCESS NAME \t REMAINING TIME\t TOTAL TIME");
     printf("\n----------\t------------\t------------\n");
while(jr<np)
{ 
  for(ir=0;ir<np;ir++)
  {

    if(prs[ir].at<=ttime)
    {
    
    if(prs[ir].xt>0)
    {  
      if(prs[ir].xt>=tslice)
      { 
        ttime+=tslice;
        prs[ir].xt=prs[ir].xt-tslice;
        printf("\n%s\t\t %d \t\t %d",prs[ir].name,prs[ir].xt,ttime);
        if(prs[ir].xt==0)
 {
  ftr[ir]=ttime;
 if(prs[ir+1].at>ttime)
 {
  idel=prs[ir+1].at-ttime;
  ttime=ttime+idel;
 }
 jr++; 
 }
      }
      else
      { 
        ttime+=prs[ir].xt;
        prs[ir].xt=0;
 ftr[ir]=ttime;
        printf("\n%s\t\t %d \t\t %d",prs[ir].name,prs[ir].xt,ttime);
        jr++;
      }//prs[ir].xt==0
    }
  }
 else
{
//idel=atime[ir]-ttime;
}
}
}
printf("\n");

for(kr=0;kr<np;kr++)
{
printf("\nTurn Aroud Time OF %s Process Is:%d\n",prs[kr].name,ftr[kr]-prs[kr].at);
}
printf("\n");
return 0;
}


0 comments:

Post a Comment