-
Notifications
You must be signed in to change notification settings - Fork 19.9k
/
Copy pathLotteryScheduling.java
141 lines (119 loc) · 4.81 KB
/
LotteryScheduling.java
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
package com.thealgorithms.scheduling;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
/**
* The LotteryScheduling class implements the Lottery Scheduling algorithm, which is
* a probabilistic CPU scheduling algorithm. Processes are assigned tickets, and
* the CPU is allocated to a randomly selected process based on ticket count.
* Processes with more tickets have a higher chance of being selected.
*/
public final class LotteryScheduling {
private LotteryScheduling() {
}
private List<Process> processes;
private Random random;
/**
* Constructs a LotteryScheduling object with the provided list of processes.
*
* @param processes List of processes to be scheduled using Lottery Scheduling.
*/
public LotteryScheduling(final List<Process> processes) {
this.processes = processes;
this.random = new Random();
}
/**
* Constructs a LotteryScheduling object with the provided list of processes and a Random object.
*
* @param processes List of processes to be scheduled using Lottery Scheduling.
* @param random Random object used for generating random numbers.
*/
public LotteryScheduling(final List<Process> processes, Random random) {
this.processes = processes;
this.random = random;
}
/**
* Schedules the processes using the Lottery Scheduling algorithm.
* Each process is assigned a certain number of tickets, and the algorithm randomly
* selects a process to execute based on ticket count. The method calculates the
* waiting time and turnaround time for each process and simulates their execution.
*/
public List<Process> scheduleProcesses() {
int totalTickets = processes.stream().mapToInt(Process::getTickets).sum();
int currentTime = 0;
List<Process> executedProcesses = new ArrayList<>();
while (!processes.isEmpty()) {
int winningTicket = random.nextInt(totalTickets) + 1;
Process selectedProcess = selectProcessByTicket(winningTicket);
if (selectedProcess == null) {
// This should not happen in normal circumstances, but we'll handle it just in case
System.err.println("Error: No process selected. Recalculating total tickets.");
totalTickets = processes.stream().mapToInt(Process::getTickets).sum();
continue;
}
selectedProcess.setWaitingTime(currentTime);
currentTime += selectedProcess.getBurstTime();
selectedProcess.setTurnAroundTime(selectedProcess.getWaitingTime() + selectedProcess.getBurstTime());
executedProcesses.add(selectedProcess);
processes.remove(selectedProcess);
totalTickets = processes.stream().mapToInt(Process::getTickets).sum();
}
return executedProcesses;
}
/**
* Selects a process based on a winning ticket. The method iterates over the
* list of processes, and as the ticket sum accumulates, it checks if the
* current process holds the winning ticket.
*
* @param winningTicket The randomly generated ticket number that determines the selected process.
* @return The process associated with the winning ticket.
*/
private Process selectProcessByTicket(int winningTicket) {
int ticketSum = 0;
for (Process process : processes) {
ticketSum += process.getTickets();
if (ticketSum >= winningTicket) {
return process;
}
}
return null;
}
/**
* The Process class represents a process in the scheduling system. Each process has
* an ID, burst time (CPU time required for execution), number of tickets (used in
* lottery selection), waiting time, and turnaround time.
*/
public static class Process {
private String processId;
private int burstTime;
private int tickets;
private int waitingTime;
private int turnAroundTime;
public Process(String processId, int burstTime, int tickets) {
this.processId = processId;
this.burstTime = burstTime;
this.tickets = tickets;
}
public String getProcessId() {
return processId;
}
public int getBurstTime() {
return burstTime;
}
public int getTickets() {
return tickets;
}
public int getWaitingTime() {
return waitingTime;
}
public void setWaitingTime(int waitingTime) {
this.waitingTime = waitingTime;
}
public int getTurnAroundTime() {
return turnAroundTime;
}
public void setTurnAroundTime(int turnAroundTime) {
this.turnAroundTime = turnAroundTime;
}
}
}