First-Come, First-Served Scheduling Algorithm
♣ Try it Out ::
#include<cstdio> #include<cstdlib> #include<iostream> #define FOR(i,N) for(int i=1; i<=N; i++) #define MAX 51 using namespace std; class CacheMemory { public: string process; int burst_time, arrival_time; int waiting_time, turn_arround_time; } P[MAX]; int n, start_time, end_time; void setInput(); void sortByArrivalTime(int); void FCFS(); void generateTime(); void waitingTime(); void turnAroundTime(); int main() { // freopen("fcfs.in", "r", stdin); setInput(); cout<<"\nGrantt chart of FCFS:"<<endl<<endl; cout<<"Process\t\tTime (milliseconds)"<<endl; cout<<"-------\t\t-------------------"<<endl; FCFS(); cout<<endl; generateTime(); waitingTime(); cout<<endl; turnAroundTime(); return(0); } void setInput() { cin>>n; FOR(i,n) cin>>P[i].process>>P[i].burst_time>>P[i].arrival_time; } void sortByArrivalTime(int nop) { FOR(i,nop) for(int j=i+1; j<=nop; j++) if(P[i].arrival_time > P[j].arrival_time) swap(P[i], P[j]); } void FCFS() { sortByArrivalTime(n); start_time = end_time = 0; FOR(i,n) { start_time = end_time; end_time = start_time + P[i].burst_time; cout<<P[i].process<<"\t\t"<<start_time<<" - "<<end_time<<endl; } } void generateTime() { start_time = end_time = 0; FOR(i,n) { start_time = end_time; end_time = start_time + P[i].burst_time; P[i].waiting_time = start_time - P[i].arrival_time; P[i].turn_arround_time = end_time - P[i].arrival_time; } } void waitingTime() { int twt = 0; cout<<"Total Waiting Time = "; FOR(i,n) { twt += P[i].waiting_time; cout<<P[i].waiting_time; if(i < n) cout<<" + "; } cout<<" = "<<twt<<" ms."<<endl; cout<<"Average Waiting Time = "<<(twt/n)<<" ms."<<endl; } void turnAroundTime() { int ttat = 0; cout<<"Total Turn Around Time = "; FOR(i,n) { ttat += P[i].turn_arround_time; cout<<P[i].turn_arround_time; if(i < n) cout<<" + "; } cout<<" = "<<ttat<<" ms."<<endl; cout<<"Average Turn Around Time = "<<(ttat/n)<<" ms."<<endl; }
♣ The Input ::
The input contains on the first line the number of processes (n). Next n lines consists of three entries. The first one is the name of process (process). The second entry contains an integer burst_time, determining the Burst Time of the process and the third entry contains another integer arrival_time, determining the Arrival Time of the process.
♣ Sample Input ::
6 P1 4 0 P2 1 1 P3 2 2 P4 3 4 P5 4 6 P6 6 7
♣ Sample Output ::
Grantt chart of FCFS: Process Time (milliseconds) ------- ------------------- P1 0 - 4 P2 4 - 5 P3 5 - 7 P4 7 - 10 P5 10 - 14 P6 14 - 20 Total Waiting Time = 0 + 3 + 3 + 3 + 4 + 7 = 20 ms. Average Waiting Time = 3 ms. Total Turn Around Time = 4 + 4 + 5 + 6 + 8 + 13 = 40 ms. Average Turn Around Time = 6 ms.
♣ Source Codes ::
- First-Come, First-Served Scheduling Algorithm.cpp
- First-Come, First-Served Scheduling Algorithm.java
আবূ হুরাইরাহ (রাঃ) হতে বর্ণিত আছে, তিনি বলেন, নাবী (সাল্লাল্লাহু আলাইহি ওয়াসাল্লাম) বলেছেনঃ কবিরা যা বলেছে তার মধ্যে কবি লাবীদ যা বলেছে, তা ধ্রুব সত্যঃ "জেনে রেখো, আল্লাহ ছাড়া সব কিছুই মিথ্যা।" [বুখারীঃ ৩৮৪১, মুসলিমঃ ২২৫৬]