Get Source Code for First Come First Served



First Come First Served (FCFS): 

In operating systems, tasks that are queued to be processed go through different scheduling algorithms in different operating systems. First Come First Served or FCFS is one of them.

In first come first served, literary, the task that comes first to be processed gets processed first, then the next one and this continues. This is a less preferable way of scheduling as any task can block the processor for infinite amount of time. Also as all tasks are given the same priority, more important tasks might be waiting too long to be processed.


However, I explained First Come First Served (FCFS) in my another post earlier and as many of my readers wanted to have the source code, here I am publishing the source code of the program. I am not currently explaining the source code, please try to play with it to understand it. I am removing comments intentionally.


Get Source Code for First Come First Served

Source Code:



#include<iostream.h>
#include<stdio.h>

int main(){
     int t=0,i,j,p,s=0, print=0, idle=0;
     float sum=0;

     cout<<"Please enter the number of proces : ";
     cin>>p;

     int a[p][4];
     int b[p][5];

     cout<<"\n\nProcess\tArrival\tBurst\n-------\t-------\t-----\n";

     for(i=0;i<p;i++){
          for(j=0;j<3;j++){
               cin>>a[i][j];
               }
          a[i][3]=a[i][2];
          }

     cout<<"\n\nTime-Line is as follows (Verticle View)....\n\n";

     /**************************Processing Starts Here*************/
     for(i=0,t=a[0][1];i<p;i++){
          while(a[i][2]!=0){
               if(t>=a[i][1]){
                    if(print==0)
                         printf("%5d-----------\n        |p-%-4d|\n",t,a[i][0]);
                    print=1;
                    idle=0;
                    a[i][2]--;
                    }
               else{
                    if(idle==0){
                         printf("%5d-----------\n        |Idle  |\n",t);
                         idle=1;
                         }
                    }
               t++;            
               }
          print=0;
          b[s][0]=a[i][0];
          b[s][1]=a[i][1];
          b[s][2]=t;
          b[s][3]=a[i][3];
          b[s][4]=((t-a[i][1])-a[i][3]);
          sum+=b[s][4];
          s++;
          }

     printf("%5d-----------\n",t); 
     /**************************Processing Ends Here*************/

     cout<<endl<<endl;
     cout<<"Table of processes with completion record as they were completed\n\n";
     cout<<"\n\nProcess\tArrival\tFinish\tTotal\tWait\n------------------------------------\n";

     for(i=0;i<s;i++)
          cout<<b[i][0]<<"\t"<<b[i][1]<<"\t"<<b[i][2]-1<<"\t"<<b[i][3]<<"\t"<<b[i][4]<<"\n";

     cout<<"\n\nAvg. Wait time = "<<sum/p<<endl<<endl;
     cout<<"__________________________________________________________\nProgrammed by : Tanmay Chakrabarty, UITS, Rajshahi Campus.\n\n";

     system("pause");
     return 0;
     }


Following is a screenshot of the program with input and output.

Get Source Code for First Come First Served

I hope that you can now study and experiment the source code to write yours and that would be much better, more efficient and strict than this one.

Recommended Recommends

Comments

Contact Us