Enter process 1 details (pid, arrival, burst, priority, color R, G, B):): 1 0 8 31 0 0Enter process 2 details (pid, arrival, burst, priority, color R, G, B):): 2 1 2 4 1 1 0Enter process 3 details (pid, arrival, burst, priority, color R, G, B):): 3 3 4 4 0 0 1Enter process 4 details (pid, arrival, burst, priority, color R, G, B):): 4 4 1 5 0 1 1Enter process 5 details (pid, arrival, burst, priority, color R, G, B):): 5 5 6 2 1 0 1Enter process 6 details (pid, arrival, burst, priority, color R, G, B):): 6 6 5 6 0 1 0Enter process 7 details (pid, arrival, burst, priority, color R, G, B):): 7 10 1 1 0 0 0
void UpdatePreemptivePriority() { // Sort processes based on arrival time if not already sorted sort(processes.begin(), processes.end(), [](const Process& a, const Process& b) { // If arrival time is 0, prioritize it regardless of priority if (a.arrivalTime == 0 && b.arrivalTime != 0) { return true; } else if (a.arrivalTime != 0 && b.arrivalTime == 0) { return false; } else if (a.arrivalTime == 0 && b.arrivalTime == 0) { // If both arrival times are 0, compare priorities if (a.priority != b.priority) { return a.priority < b.priority; } // If priorities are the same, compare burst times return a.burstTime < b.burstTime; } else { // If arrival times are not 0, compare based on priority if (a.arrivalTime != b.arrivalTime) { return a.arrivalTime < b.arrivalTime; } // If priorities are the same, compare arrival times if (a.priority != b.priority) { return a.priority < b.priority; } // If arrival times and priorities are the same, compare burst times return a.burstTime < b.burstTime; } }); // Check if any process has arrived and add it to the ready queue // Check if any process has arrived and add it to the ready queue while (nextProcessIndex < processes.size()&& processes[nextProcessIndex].arrivalTime <= currentTime) { Process p = processes[nextProcessIndex]; p.x = -200 - nextProcessIndex * 20; // Initial x position p.y = 100; // Initial y position p.radius = 10; // Radius of the process circle p.isExecuting = false; p.id = nextProcessIndex + 1; // Set process ID using index // Update response time if it's not set if (p.responseTime == 0) { p.responseTime = currentTime - p.arrivalTime; } processes[nextProcessIndex] = p; readyQueue.push(p); nextProcessIndex++; } // Check if no process is currently executing or context switch is needed if (prevExecutingProcessIndex == -1 && !readyQueue.empty()) { Process highestPriorityProcess = readyQueue.front(); // Initialize with the first process in the queue int highestPriorityIndex = 0; // Index of the highest priority process vector<Process> tempVector; while (!readyQueue.empty()) { tempVector.push_back(readyQueue.front()); readyQueue.pop(); } // Iterate over the vector int index = 0; for (const auto& p : tempVector) { // Check if the current process has higher priority than the highest priority process found so far if (p.burstTime > 0 && p.priority < highestPriorityProcess.priority) { highestPriorityProcess = p; highestPriorityIndex = p.id-1; } else if (p.burstTime > 0 && p.priority > highestPriorityProcess.priority) { highestPriorityProcess = p; highestPriorityIndex = p.id - 1; break; } index++; } for (const auto& process : tempVector) { readyQueue.push(process); } // Execute the highest priority process highestPriorityProcess.isExecuting = true; prevExecutingProcessIndex = highestPriorityIndex; // Update current time currentTime++; // Update response time if it's not set if (highestPriorityProcess.responseTime == 0) { highestPriorityProcess.responseTime = currentTime - highestPriorityProcess.arrivalTime; } } // Move the process towards the CPU if (prevExecutingProcessIndex != -1) { Process& p = processes[prevExecutingProcessIndex]; float dx = cpu.x - p.x; float dy = cpu.y - p.y; float dist = sqrt(dx * dx + dy * dy); p.x += dx / dist * 1; p.y += dy / dist * 1; // Check if the process has reached the CPU if (p.x >= cpu.x - 20 && p.x <= cpu.x + 20 && p.y >= cpu.y - 20 && p.y <= cpu.y + 20) { // Execute the process for one time unit (preemptive) p.burstTime--; cout << p.burstTime << endl; if (p.burstTime != 0) { ganttChart.chart.push_back({ p.pid, make_tuple(p.arrivalTime, currentTime, p.color) }); readyQueue.pop(); readyQueue.push(p); p.x = -200 - prevExecutingProcessIndex * 20; // Initial x position p.y = 100; // Initial y position prevExecutingProcessIndex = -1; } // Update Gantt chart and remove the process if completed // ... else if (p.burstTime == 0) { ganttChart.chart.push_back({ p.pid, make_tuple(p.arrivalTime, currentTime, p.color) }); readyQueue.pop(); p.turnaroundTime = currentTime - p.arrivalTime; p.waitingTime = p.turnaroundTime - p.originalBurstTime; // Remove the process from the processes vector p.x = -200 - prevExecutingProcessIndex * 20; // Initial x position p.y = 100; // Initial y position prevExecutingProcessIndex = -1; // Reset the currently executing process index to allow context switching cpu.color[0] = 0.0; cpu.color[1] = 1.0; cpu.color[2] = 0.0; } } } // Update the display glutPostRedisplay(); }
i tried to sort the process based on priority ,it is sorting but if burst time becomes 0 of the highestpriorityprocess then the code fails