From 24658a5b6db7a606bc5c3c79058b95476586e60c Mon Sep 17 00:00:00 2001 From: Archana Saroj Date: Tue, 21 Nov 2023 22:08:24 +0530 Subject: [PATCH 1/7] Added Preemptive Priority Scheduling Algorithm in Process Scheduling Folder --- .../preemptive_priority_scheduling.c | 112 ++++++++++++++++++ 1 file changed, 112 insertions(+) create mode 100644 process_scheduling_algorithms/preemptive_priority_scheduling.c diff --git a/process_scheduling_algorithms/preemptive_priority_scheduling.c b/process_scheduling_algorithms/preemptive_priority_scheduling.c new file mode 100644 index 0000000000..1851b2091d --- /dev/null +++ b/process_scheduling_algorithms/preemptive_priority_scheduling.c @@ -0,0 +1,112 @@ +#include + +// Process structure containing ID, arrival time, burst time, priority, executed time, and completion time +struct Process { + int id; + int arrival_time; + int burst_time; + int priority; + int executed_time; + int completion_time; +}; + +// Function to perform preemptive priority scheduling +void preemptivePriority(struct Process processes[], int n) { + int currentTime = 0; + int executedProcesses = 0; + int currentProcess = -1; + float totalTurnaroundTime = 0; + float totalWaitingTime = 0; + + while (executedProcesses < n) { + int highestPriority = 9999; // A large number as initial priority + int nextProcess = -1; + + for (int i = 0; i < n; i++) { + if (processes[i].arrival_time <= currentTime && processes[i].executed_time < processes[i].burst_time) { + if (processes[i].priority < highestPriority) { + highestPriority = processes[i].priority; + nextProcess = i; + } + } + } + + if (nextProcess != -1) { + if (currentProcess != -1 && currentProcess != nextProcess) { + printf("Time %d: Process %d -> Process %d\n", currentTime, currentProcess + 1, nextProcess + 1); + } + currentProcess = nextProcess; + processes[currentProcess].executed_time++; + currentTime++; + + if (processes[currentProcess].executed_time == processes[currentProcess].burst_time) { + executedProcesses++; + processes[currentProcess].completion_time = currentTime; + printf("Time %d: Process %d completed\n", currentTime, currentProcess + 1); + totalTurnaroundTime += (float)processes[currentProcess].completion_time - processes[currentProcess].arrival_time; + totalWaitingTime += (float)processes[currentProcess].completion_time - processes[currentProcess].arrival_time - processes[currentProcess].burst_time; + currentProcess = -1; + } + } else { + currentTime++; + } + } + + float avgTurnaroundTime = totalTurnaroundTime / n; + float avgWaitingTime = totalWaitingTime / n; + + printf("\nAverage Turnaround Time: %.2f\n", avgTurnaroundTime); + printf("Average Waiting Time: %.2f\n", avgWaitingTime); +} +int main() { + struct Process testCase1[] = { + {1, 0, 3, 2, 0}, + {2, 1, 5, 1, 0}, + {3, 2, 2, 3, 0}, + {4, 3, 4, 4, 0}, + {5, 4, 6, 2, 0} + }; + + struct Process testCase2[] = { + {1, 0, 8, 2, 0}, + {2, 1, 6, 1, 0}, + {3, 2, 4, 3, 0}, + {4, 3, 2, 4, 0}, + {5, 4, 5, 2, 0} + }; + + struct Process testCase3[] = { + {1, 0, 4, 3, 0}, + {2, 1, 3, 2, 0}, + {3, 2, 7, 1, 0}, + {4, 3, 5, 4, 0}, + {5, 4, 2, 5, 0} + }; + + struct Process testCase4[] = { + {1, 0, 5, 4, 0}, + {2, 1, 4, 3, 0}, + {3, 2, 6, 2, 0}, + {4, 3, 3, 1, 0}, + {5, 4, 7, 5, 0} + }; + + int n1 = sizeof(testCase1) / sizeof(testCase1[0]); + int n2 = sizeof(testCase2) / sizeof(testCase2[0]); + int n3 = sizeof(testCase3) / sizeof(testCase3[0]); + int n4 = sizeof(testCase4) / sizeof(testCase4[0]); + + printf("Test Case 1:\n"); + preemptivePriority(testCase1, n1); + + printf("\nTest Case 2:\n"); + preemptivePriority(testCase2, n2); + + printf("\nTest Case 3:\n"); + preemptivePriority(testCase3, n3); + + printf("\nTest Case 4:\n"); + preemptivePriority(testCase4, n4); + + return 0; +} From 5c3617e2b29acb072f7a6f2c70929fc6bdfb41e7 Mon Sep 17 00:00:00 2001 From: Archana Saroj Date: Sat, 25 Nov 2023 00:46:31 +0530 Subject: [PATCH 2/7] added comments and created a separate function for test cases. Additionally, also provided the explanation of the algorithm used. --- .../preemptive_priority_scheduling.c | 72 +++++++++++++++---- 1 file changed, 59 insertions(+), 13 deletions(-) diff --git a/process_scheduling_algorithms/preemptive_priority_scheduling.c b/process_scheduling_algorithms/preemptive_priority_scheduling.c index 1851b2091d..2177a1faeb 100644 --- a/process_scheduling_algorithms/preemptive_priority_scheduling.c +++ b/process_scheduling_algorithms/preemptive_priority_scheduling.c @@ -1,16 +1,52 @@ -#include - -// Process structure containing ID, arrival time, burst time, priority, executed time, and completion time +/** + * @file + * @brief + * [Preemptive Priority + * Scheduling] + * Preemptive Priority Scheduling is a CPU scheduling algorithm + that assigns priorities to processes and selects the process + with the highest priority to execute. If a higher-priority + process becomes available, it preempts the currently executing + process. This algorithm ensures that higher-priority processes + are given preference, but it may lead to starvation of lower-priority + processes if not handled properly.* + * + * @details + Algorithm Overview: + 1. Initialize a set of processes with attributes such as ID, arrival time, + burst time, priority, executed time, and completion time. + 2. Start with an empty queue of ready processes and initialize variables + to keep track of the current time, executed processes, and the currently + executing process. + 3. Repeat the following steps until all processes are executed: + a. Find the process with the highest priority that has arrived and + not completed yet. + b. Execute the selected process for one time unit, updating its + executed time and the current time. + c. If the selected process completes, calculate its completion time, + print a completion message, and update turnaround and waiting times. + d. If no suitable process is found to execute, increment the current time. + 4. Calculate and print the average turnaround time and average waiting time. + * @author [Archana Saroj](https://github.com/SarojArchana) + */ +#include /// for IO operations (`printf`) +/** + * @brief Structure to represent a process + */ struct Process { - int id; - int arrival_time; - int burst_time; - int priority; - int executed_time; - int completion_time; + int id; /**< Process ID */ + int arrival_time; /**< Arrival time of the process */ + int burst_time; /**< Burst time of the process */ + int priority; /**< Priority of the process */ + int executed_time; /**< Time the process has been executed */ + int completion_time;/**< Completion time of the process */ }; - -// Function to perform preemptive priority scheduling +/** + * @brief Performs preemptive priority scheduling for a set of processes. + * @param processes An array of Process structures representing the processes to be scheduled. + * @param n The number of processes in the array. + * @return void + */ void preemptivePriority(struct Process processes[], int n) { int currentTime = 0; int executedProcesses = 0; @@ -58,7 +94,11 @@ void preemptivePriority(struct Process processes[], int n) { printf("\nAverage Turnaround Time: %.2f\n", avgTurnaroundTime); printf("Average Waiting Time: %.2f\n", avgWaitingTime); } -int main() { +/** + * @brief Runs test cases for preemptive priority scheduling. + * @return void + */ +static void test() { struct Process testCase1[] = { {1, 0, 3, 2, 0}, {2, 1, 5, 1, 0}, @@ -107,6 +147,12 @@ int main() { printf("\nTest Case 4:\n"); preemptivePriority(testCase4, n4); - +} +/** + * @brief Main function to run the scheduling algorithm with test cases. + * @return Returns 0 to indicate successful execution. + */ +int main() { + test(); return 0; } From c1140bc337f6dc20a55c1abd58f2129199eaff8a Mon Sep 17 00:00:00 2001 From: SarojArchana <93010546+SarojArchana@users.noreply.github.com> Date: Wed, 29 Nov 2023 23:33:41 +0530 Subject: [PATCH 3/7] Update process_scheduling_algorithms/preemptive_priority_scheduling.c Added the changes Co-authored-by: realstealthninja <68815218+realstealthninja@users.noreply.github.com> --- .../preemptive_priority_scheduling.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/process_scheduling_algorithms/preemptive_priority_scheduling.c b/process_scheduling_algorithms/preemptive_priority_scheduling.c index 2177a1faeb..28fa0fc986 100644 --- a/process_scheduling_algorithms/preemptive_priority_scheduling.c +++ b/process_scheduling_algorithms/preemptive_priority_scheduling.c @@ -3,13 +3,13 @@ * @brief * [Preemptive Priority * Scheduling] - * Preemptive Priority Scheduling is a CPU scheduling algorithm - that assigns priorities to processes and selects the process - with the highest priority to execute. If a higher-priority - process becomes available, it preempts the currently executing - process. This algorithm ensures that higher-priority processes - are given preference, but it may lead to starvation of lower-priority - processes if not handled properly.* + * Preemptive Priority Scheduling is a CPU scheduling algorithm + * that assigns priorities to processes and selects the process + * with the highest priority to execute. If a higher-priority + * process becomes available, it preempts the currently executing. + * This algorithm ensures that higher-priority processes + * are given preference, but it may lead to starvation of lower-priority + * if not handled properly.* * * @details Algorithm Overview: From 3726abcf8b40d95abccc48fecf981d0fa4c0ada8 Mon Sep 17 00:00:00 2001 From: SarojArchana <93010546+SarojArchana@users.noreply.github.com> Date: Wed, 29 Nov 2023 23:34:16 +0530 Subject: [PATCH 4/7] Update process_scheduling_algorithms/preemptive_priority_scheduling.c Added the suggestion Co-authored-by: realstealthninja <68815218+realstealthninja@users.noreply.github.com> --- .../preemptive_priority_scheduling.c | 28 +++++++++---------- 1 file changed, 13 insertions(+), 15 deletions(-) diff --git a/process_scheduling_algorithms/preemptive_priority_scheduling.c b/process_scheduling_algorithms/preemptive_priority_scheduling.c index 28fa0fc986..85f9e783c2 100644 --- a/process_scheduling_algorithms/preemptive_priority_scheduling.c +++ b/process_scheduling_algorithms/preemptive_priority_scheduling.c @@ -12,21 +12,19 @@ * if not handled properly.* * * @details - Algorithm Overview: - 1. Initialize a set of processes with attributes such as ID, arrival time, - burst time, priority, executed time, and completion time. - 2. Start with an empty queue of ready processes and initialize variables - to keep track of the current time, executed processes, and the currently - executing process. - 3. Repeat the following steps until all processes are executed: - a. Find the process with the highest priority that has arrived and - not completed yet. - b. Execute the selected process for one time unit, updating its - executed time and the current time. - c. If the selected process completes, calculate its completion time, - print a completion message, and update turnaround and waiting times. - d. If no suitable process is found to execute, increment the current time. - 4. Calculate and print the average turnaround time and average waiting time. + * Algorithm Overview: + * 1. Initialize a set of processes with such as ID, arrival time, + * burst time, priority, executed time, and completion time. + * 2. Start with an empty queue of ready processes and initialize variables + * to keep track of the current time, executed processes, and the currently + * executing process. + * 3. Repeat the following steps until all processes are executed: + * a. Find the process with the highest priority that has arrived and + * not completed yet. + * b. Execute the selected process for one time unit, updating its executed time and the current time. + * c. If the selected process completes, calculate its completion time, print a completion message, and update turnaround and waiting times. + * d. If no suitable process is found to execute, increment the current time. + * 4. Calculate and print the average turnaround time and average waiting time. * @author [Archana Saroj](https://github.com/SarojArchana) */ #include /// for IO operations (`printf`) From 4d4cfa53bd50b7811d6e8bea1f8dfab6256c05f7 Mon Sep 17 00:00:00 2001 From: SarojArchana <93010546+SarojArchana@users.noreply.github.com> Date: Wed, 29 Nov 2023 23:35:03 +0530 Subject: [PATCH 5/7] Update process_scheduling_algorithms/preemptive_priority_scheduling.c Added the suggestion Co-authored-by: realstealthninja <68815218+realstealthninja@users.noreply.github.com> --- .../preemptive_priority_scheduling.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/process_scheduling_algorithms/preemptive_priority_scheduling.c b/process_scheduling_algorithms/preemptive_priority_scheduling.c index 85f9e783c2..8c31b3012a 100644 --- a/process_scheduling_algorithms/preemptive_priority_scheduling.c +++ b/process_scheduling_algorithms/preemptive_priority_scheduling.c @@ -32,12 +32,12 @@ * @brief Structure to represent a process */ struct Process { - int id; /**< Process ID */ - int arrival_time; /**< Arrival time of the process */ - int burst_time; /**< Burst time of the process */ - int priority; /**< Priority of the process */ - int executed_time; /**< Time the process has been executed */ - int completion_time;/**< Completion time of the process */ + int id; ///< Process ID + int arrival_time; ///< Arrival time of the process + int burst_time; ///< Burst time of the process + int priority; ///< Priority of the process + int executed_time; ///< Time the process has been executed + int completion_time;///< Completion time of the process }; /** * @brief Performs preemptive priority scheduling for a set of processes. From b6726d38419e7db732dde39ff376cc9665a22671 Mon Sep 17 00:00:00 2001 From: SarojArchana <93010546+SarojArchana@users.noreply.github.com> Date: Wed, 29 Nov 2023 23:37:22 +0530 Subject: [PATCH 6/7] Update process_scheduling_algorithms/preemptive_priority_scheduling.c Applied the changes suggested Co-authored-by: realstealthninja <68815218+realstealthninja@users.noreply.github.com> --- .../preemptive_priority_scheduling.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/process_scheduling_algorithms/preemptive_priority_scheduling.c b/process_scheduling_algorithms/preemptive_priority_scheduling.c index 8c31b3012a..41c6c12cdd 100644 --- a/process_scheduling_algorithms/preemptive_priority_scheduling.c +++ b/process_scheduling_algorithms/preemptive_priority_scheduling.c @@ -46,8 +46,8 @@ struct Process { * @return void */ void preemptivePriority(struct Process processes[], int n) { - int currentTime = 0; - int executedProcesses = 0; + uint_t currentTime = 0; + uint_t executedProcesses = 0; int currentProcess = -1; float totalTurnaroundTime = 0; float totalWaitingTime = 0; From 2eb8eb6230f9d0b132250125f8582bd717aadf49 Mon Sep 17 00:00:00 2001 From: Archana Saroj Date: Wed, 29 Nov 2023 23:57:58 +0530 Subject: [PATCH 7/7] Converted the int to unsigned int --- .../preemptive_priority_scheduling.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/process_scheduling_algorithms/preemptive_priority_scheduling.c b/process_scheduling_algorithms/preemptive_priority_scheduling.c index 41c6c12cdd..21aeadb0e0 100644 --- a/process_scheduling_algorithms/preemptive_priority_scheduling.c +++ b/process_scheduling_algorithms/preemptive_priority_scheduling.c @@ -46,8 +46,8 @@ struct Process { * @return void */ void preemptivePriority(struct Process processes[], int n) { - uint_t currentTime = 0; - uint_t executedProcesses = 0; + unsigned int currentTime = 0; + unsigned int executedProcesses = 0; int currentProcess = -1; float totalTurnaroundTime = 0; float totalWaitingTime = 0;