-
Notifications
You must be signed in to change notification settings - Fork 0
/
FCFS.go
91 lines (75 loc) · 2 KB
/
FCFS.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
package main
import (
"bufio"
"fmt"
"os"
"sort"
"strings"
"sync"
)
func FCFS() {
processChan := make(chan string)
inputChan := make(chan []Process)
var averageWaitingTime, averageTurnaroundTime float64
var wg sync.WaitGroup
wg.Add(2)
go func() {
reader := bufio.NewReader(os.Stdin)
// Read the counter from the user
fmt.Print("Enter the number of processes: ")
var count int
_, err := fmt.Scanln(&count)
if err != nil {
fmt.Println("Error reading input:", err)
return
}
var processes []Process
for i := 0; i < count; i++ {
//fmt.Printf("Enter process %d of %d: ", i+1, count)
line, err := reader.ReadString('\n')
if err != nil {
fmt.Println("Error reading input:", err)
return
}
line = strings.TrimSuffix(line, "\n")
parts := strings.Split(line, " ")
if len(parts) != 3 {
fmt.Println("Invalid input")
return
}
//name := parts[0]
execTimeStr := parts[1]
arrivalTimeStr := parts[2]
var xValue float64 = 0
var yValue float64 = 0
fmt.Sscanf(execTimeStr, "%f", &xValue)
fmt.Sscanf(arrivalTimeStr, "%f", &yValue)
processes = append(processes, Process{executionTime: xValue, arrivalTime: yValue})
}
sort.Sort(ByArrivalTime(processes))
inputChan <- processes
close(inputChan)
wg.Done()
}()
go func() {
for processes := range inputChan {
//fmt.Println(processes)
var waitingTime, turnaroundTime float64 = 0, 0
var finishingTime float64 = 0
for i, item := range processes {
finishingTime += item.executionTime
i = i
turnaroundTime += finishingTime - item.arrivalTime
waitingTime += finishingTime - item.executionTime - item.arrivalTime
}
averageWaitingTime, averageTurnaroundTime = waitingTime/float64(len(processes)), turnaroundTime/float64(len(processes))
result := fmt.Sprintf("FCFS average WT and TAT: %0.3f, %0.3f", averageWaitingTime, averageTurnaroundTime)
processChan <- result
close(processChan)
wg.Done()
}
}()
for result := range processChan {
fmt.Println(result)
}
}