Repository navigation
Expand file tree
/
Copy pathfcfs.cpp
More file actions
123 lines (84 loc) · 2.39 KB
/
Copy pathfcfs.cpp
File metadata and controls
123 lines (84 loc) · 2.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
//Function to Implement First come first server algorithm.
#include<iostream>
#include<fstream>
#include<sys/types.h>
using namespace std;
#include<sys/socket.h>
#include<netinet/in.h>
#include<unistd.h>
#include<arpa/inet.h>
#include<stdlib.h>
#include<errno.h>
#include "../inc/class.h"
#include <algorithm>
#include <iomanip>
#define MAX 100
bool comparisonAT(struct process a, struct process b)//Compare Arrival Time
{
int x = a.ArrivalTime;
int y = b.ArrivalTime;
return x < y;
}
bool comparisonPID(struct process a, struct process b)//Compare Pid
{
int x = a.pid;
int y = b.pid;
return x < y;
}
struct process* fcfs()//Structure contains FCFS scheduler
{
string st,BurstTime,pid,ArrivalTime;
int flag=0,k,j=0;
fstream file1;
file1.open("../data/dt.txt",ios::in);
//here we are opening the file dt.txt in read mode
struct process *p=new struct process[MAX];
//we are fetching each line of dt.txt file for storing Arrival Time Brust Time
while(getline(file1,st)){
for(int i=0;i<st.size();i++)
{
if(st[i]==','){
flag++;
}
else if(flag==1){
pid+=st[i];
}
else if(flag==2){
ArrivalTime+=st[i];
}
else if(flag==3){
BurstTime+=st[i];
}
}
//value for pid arrival time Brust time in structure process define in class.h file
p[j].pid=stoi(pid);
p[j].ArrivalTime=stoi(ArrivalTime);
p[j].BurstTime=stoi(BurstTime);
pid.clear();
ArrivalTime.clear();
BurstTime.clear();
j++;
flag=0;
}
file1.close();
//close
float sum_tat = 0, sum_wt = 0, sum_rt = 0;
int length_cycle, total_idle_time = 0;
sort(p, p + j, comparisonAT);//sorting of Arrival time
for (int i = 0; i < j; i++)
{
p[i].StartTime = (i == 0) ? p[i].ArrivalTime : max(p[i].ArrivalTime, p[i - 1].ComplitionTime);
p[i].ComplitionTime = p[i].StartTime + p[i].BurstTime;
p[i].TurnAroundTime = p[i].ComplitionTime - p[i].ArrivalTime;
p[i].WaitingTime = p[i].TurnAroundTime - p[i].BurstTime;
p[i].ResponseTime = p[i].StartTime - p[i].ArrivalTime;
//Calculation Of Complition Time, Turn Around Time, Waiting time, Response Time.
sum_tat += p[i].TurnAroundTime;
sum_wt += p[i].WaitingTime;
sum_rt += p[i].ResponseTime;
total_idle_time += (i == 0) ? 0 : (p[i].StartTime - p[i - 1].ComplitionTime);
}
length_cycle = p[j - 1].ComplitionTime - p[0].StartTime;
sort(p, p + j, comparisonPID);//Sorting Of pid
return p;
}