hi
i'm student in university
i try to do my homework to simulate FCFS and RR

i need ur help to tell me if my answer good or :sad:

Qustion is:

the project is to write a program to simulate FCFS and RR without having any i/o, all processes have only arrival time and cpu burst. maximum 128 processes. the data for the processes sholuld be input to your program using assignment statment not scanf statemant. you will have 5 processes having arrival time as follows: 0, 2, 4, 5, 6
and cpu burst time as follows: 10,13, 15, 17, 4
RR will have time quantum of 5 and context switching time of 1
the output of your program should be the GANTT chart in the form of a table, average waiting time and average response time and average turn around time for both algorithms

table for Gantt chart means the information in a text form not a drawing for example : for each process indiacte process number, satrt time on the Gantt chart , finish time on the gantt chart
example:
process 1 5 10
process 2 10 15


this is my answer:

#include< stdio.h>
#define QUANTUM 5
#define SWITCH 1
typedef struct {
    int p_id;
    int arrival_t;
                  int cpu_t;
    int run_t;
    int end_t;
    int turnarround_t;
    int waiting_t;
   } process;

void FCFS(process pinfo[]);
void RR(process pinfo[]);
void output(int choice,process pinfo[]);
  int choice;
  main()
  {
  do {
 process pinfo[]={
     {0,0,10,0,-1,0,0},
     {1,2,13,0,-1,0,0},
     {2,4,15,0,-1,0,0},
     {3,5,17,0,-1,0,0},
     {4,6,4,0,-1,0,0},
     
   };
 printf("Which stratagy would you like to select?\n");
 printf("1--FCFS;    2--RR;\n");
 printf("3--END;\n\n");
 printf("Please enter your choice:");
 scanf("%d",&choice);
 printf("\n\n");
 switch(choice){
   case 1:
  FCFS(pinfo);
  break;
   case 2:
  RR(pinfo);
  break;
   case 3:
  printf("\nTHANKS FOR TRYING MY SIMULATION PROGRAM,GOOD BY!\n");
  break;
   default:
  printf("\nINVALID CHOICE,PLEASE RE-ENTER YOUR CHOICE AGAIN!\n\n");
 }
    } while(choice!=3);
    
  return 0;
  }
  void FCFS(process pinfo[])
  {
  int i;
  pinfo[0].end_t=pinfo[0].arrival_t+pinfo[0].cpu_t;
  for(i=1;i<10;i++)
  pinfo[i].end_t=pinfo[i-1].end_t+pinfo[i].cpu_t;
  output(choice,pinfo);
  return;
  }
 
 void RR(process pinfo[])
 {
  int i,j,t=0;
  int index;
  int count=0;
  int queue[5];
  do{
     /* When every job finished or timequatum expires */
     /*Find those which are arrived but unexecuted*/
     index=-1;
       for(i=0;i<5;++i)
   if((pinfo[i].arrival_t<=t)&&(pinfo[i].run_t==0))
      queue[++index]=i;
       /*Find those which are arrived and executed but unfinished*/
       for(j=0;j<5;++j)
   if((pinfo[j].arrival_t<=t)&&(pinfo[j].run_t>0)&&(pinfo[j].end_t<0))
      queue[++index]=j;
            /*Execut the first job in queue[indext]*/
            /*1,this job will finish until current quatum expires*/
     if((pinfo[queue[0]].cpu_t-pinfo[queue[0]].run_t)<=QUANTUM){
  /*Set endtime */
  pinfo[queue[0]].end_t=t+pinfo[queue[0]].cpu_t-pinfo[queue[0]].run_t;
  /*mark the time this job finished*/
  t+=pinfo[queue[0]].cpu_t-pinfo[queue[0]].run_t;
  /*Set current job's runtime*/
  pinfo[queue[0]].run_t=pinfo[queue[0]].cpu_t;
  /*count finished jobs*/
  count++;
      }
     /*2,this job will not finish until current quatum expires*/
     else {
  /*Mark next time when round robing happens*/
  t+=QUANTUM;
  /*Add time current job has run*/
  pinfo[queue[0]].run_t+=QUANTUM;
      }
   } while(count<5);
     output(choice,pinfo);
     return;
  }

 void output(int choice,process pinfo[])
 {
  int i;
  double tt=0;
  double wt=0;
  double avg_tt,avg_wt;
  char c[2][3]={"FCFS","RR"};
  printf("The result of stagegy \"%s\" is as follows:\n\n",c[choice-1]);
  printf("Process id\tTurnarround Time\tWaiting Time\n");
  for(i=0;i<5;++i){
     pinfo[i].turnarround_t=pinfo[i].end_t-pinfo[i].arrival_t;
     pinfo[i].waiting_t=pinfo[i].turnarround_t-pinfo[i].cpu_t;
     printf("%d\t\t\t%d\t\t\t%d\n",pinfo[i].p_id,pinfo[i].turnarround_t,
            pinfo[i].waiting_t);
     tt+=pinfo[i].turnarround_t;
     wt+=pinfo[i].waiting_t;
   }
  avg_tt=tt/5;
  avg_wt=wt/5;
  printf("\nAverage Turnarround Time\t%.2f\n",avg_tt);
  printf("Average Waiting Time\t\t%.2f\n\n",avg_wt);
  return ;
 }

-------
how i can calculate response time and gant chart
what i should to do for context

plz i need ur help to complete my project;) ;)
i should to send homwork to my teacher today

thanks

Recommended Answers

All 8 Replies

plz.... i should to sent the project in 2 houre

Views: 8

Member Avatar for iamthwee

Good for you. Let me know when you fail.

Good for you. Let me know when you fail.

:?: what do u mean

I found this snippet about the subject, maybe it would help you, maybe not, but here it is:

http://www.daniweb.com/code/snippet445.html

thanks alot 4 yr help
i need to use array not linked list

thanks again 4 ur help

thanks alot 4 yr help
i need to use array not linked list

thanks again 4 ur help

Your welcome. Run and turn it to your teacher. :)

Well with any luck, at least two people will get bounced off the course for handing in identical homework.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.