/
main.go
211 lines (178 loc) · 5.08 KB
/
main.go
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
package main
import (
"encoding/csv"
"errors"
"fmt"
"io"
"log"
"os"
"strconv"
"strings"
"github.com/olekukonko/tablewriter"
)
func main() {
// CLI args
f, closeFile, err := openProcessingFile(os.Args...)
if err != nil {
log.Fatal(err)
}
defer closeFile()
// Load and parse processes
processes, err := loadProcesses(f)
if err != nil {
log.Fatal(err)
}
// First-come, first-serve scheduling
FCFSSchedule(os.Stdout, "First-come, first-serve", processes)
//SJFSchedule(os.Stdout, "Shortest-job-first", processes)
//
//SJFPrioritySchedule(os.Stdout, "Priority", processes)
//
//RRSchedule(os.Stdout, "Round-robin", processes)
}
func openProcessingFile(args ...string) (*os.File, func(), error) {
if len(args) != 2 {
return nil, nil, fmt.Errorf("%w: must give a scheduling file to process", ErrInvalidArgs)
}
// Read in CSV process CSV file
f, err := os.Open(args[1])
if err != nil {
return nil, nil, fmt.Errorf("%v: error opening scheduling file", err)
}
closeFn := func() {
if err := f.Close(); err != nil {
log.Fatalf("%v: error closing scheduling file", err)
}
}
return f, closeFn, nil
}
type (
Process struct {
ProcessID int64
ArrivalTime int64
BurstDuration int64
Priority int64
}
TimeSlice struct {
PID int64
Start int64
Stop int64
}
)
//region Schedulers
// FCFSSchedule outputs a schedule of processes in a GANTT chart and a table of timing given:
// • an output writer
// • a title for the chart
// • a slice of processes
func FCFSSchedule(w io.Writer, title string, processes []Process) {
var (
serviceTime int64
totalWait float64
totalTurnaround float64
lastCompletion float64
waitingTime int64
schedule = make([][]string, len(processes))
gantt = make([]TimeSlice, 0)
)
for i := range processes {
if processes[i].ArrivalTime > 0 {
waitingTime = serviceTime - processes[i].ArrivalTime
}
totalWait += float64(waitingTime)
start := waitingTime + processes[i].ArrivalTime
turnaround := processes[i].BurstDuration + waitingTime
totalTurnaround += float64(turnaround)
completion := processes[i].BurstDuration + processes[i].ArrivalTime + waitingTime
lastCompletion = float64(completion)
schedule[i] = []string{
fmt.Sprint(processes[i].ProcessID),
fmt.Sprint(processes[i].Priority),
fmt.Sprint(processes[i].BurstDuration),
fmt.Sprint(processes[i].ArrivalTime),
fmt.Sprint(waitingTime),
fmt.Sprint(turnaround),
fmt.Sprint(completion),
}
serviceTime += processes[i].BurstDuration
gantt = append(gantt, TimeSlice{
PID: processes[i].ProcessID,
Start: start,
Stop: serviceTime,
})
}
count := float64(len(processes))
aveWait := totalWait / count
aveTurnaround := totalTurnaround / count
aveThroughput := count / lastCompletion
outputTitle(w, title)
outputGantt(w, gantt)
outputSchedule(w, schedule, aveWait, aveTurnaround, aveThroughput)
}
//func SJFPrioritySchedule(w io.Writer, title string, processes []Process) { }
//
//func SJFSchedule(w io.Writer, title string, processes []Process) { }
//
//func RRSchedule(w io.Writer, title string, processes []Process) { }
//endregion
//region Output helpers
func outputTitle(w io.Writer, title string) {
_, _ = fmt.Fprintln(w, strings.Repeat("-", len(title)*2))
_, _ = fmt.Fprintln(w, strings.Repeat(" ", len(title)/2), title)
_, _ = fmt.Fprintln(w, strings.Repeat("-", len(title)*2))
}
func outputGantt(w io.Writer, gantt []TimeSlice) {
_, _ = fmt.Fprintln(w, "Gantt schedule")
_, _ = fmt.Fprint(w, "|")
for i := range gantt {
pid := fmt.Sprint(gantt[i].PID)
padding := strings.Repeat(" ", (8-len(pid))/2)
_, _ = fmt.Fprint(w, padding, pid, padding, "|")
}
_, _ = fmt.Fprintln(w)
for i := range gantt {
_, _ = fmt.Fprint(w, fmt.Sprint(gantt[i].Start), "\t")
if len(gantt)-1 == i {
_, _ = fmt.Fprint(w, fmt.Sprint(gantt[i].Stop))
}
}
_, _ = fmt.Fprintf(w, "\n\n")
}
func outputSchedule(w io.Writer, rows [][]string, wait, turnaround, throughput float64) {
_, _ = fmt.Fprintln(w, "Schedule table")
table := tablewriter.NewWriter(w)
table.SetHeader([]string{"ID", "Priority", "Burst", "Arrival", "Wait", "Turnaround", "Exit"})
table.AppendBulk(rows)
table.SetFooter([]string{"", "", "", "",
fmt.Sprintf("Average\n%.2f", wait),
fmt.Sprintf("Average\n%.2f", turnaround),
fmt.Sprintf("Throughput\n%.2f/t", throughput)})
table.Render()
}
//endregion
//region Loading processes.
var ErrInvalidArgs = errors.New("invalid args")
func loadProcesses(r io.Reader) ([]Process, error) {
rows, err := csv.NewReader(r).ReadAll()
if err != nil {
return nil, fmt.Errorf("%w: reading CSV", err)
}
processes := make([]Process, len(rows))
for i := range rows {
processes[i].ProcessID = mustStrToInt(rows[i][0])
processes[i].BurstDuration = mustStrToInt(rows[i][1])
processes[i].ArrivalTime = mustStrToInt(rows[i][2])
if len(rows[i]) == 4 {
processes[i].Priority = mustStrToInt(rows[i][3])
}
}
return processes, nil
}
func mustStrToInt(s string) int64 {
i, err := strconv.ParseInt(s, 10, 64)
if err != nil {
_, _ = fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
return i
}
//endregion