I've implemented a PSJF algorithm, however for some reason the code here:
if (!tasks[i].completed && tasks[i].arrival_time <= current_time && tasks[i].burst_time < shortest_job_burst)
{
shortest_job_index = i;
shortest_job_burst = tasks[i].burst_time;
found_job = 1;
}
and all code following it does not function. I figure it may be some type of error with the if statement logic, but still unsure about what specifically is causing the block.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#define MAX_TASKS 50
#define TIME_QUANTUM 4
typedef struct
{
char name[10];
int arrival_time;
int burst_time;
int remaining_time;
int waiting_time;
int completed;
} Task;
Task tasks[MAX_TASKS];
Task ready_queue[MAX_TASKS];
int front = -1, rear = -1;
void enqueue(Task task)
{
if (front == -1)
{
front = 0;
}
rear++;
ready_queue[rear] = task;
}
Task dequeue()
{
Task task = ready_queue[front];
if (front == rear)
{
front = -1;
rear = -1;
}
else
{
front++;
}
return task;
}
int is_empty()
{
return front == -1;
}
int main()
{
FILE *input_file = fopen("TaskSpec.txt", "r");
FILE *output_file = fopen("Output.txt", "w");
// load variables here (not displayed)
// PSJF
// Initialize variables
int current_time = 0;
int total_waiting_time = 0;
fprintf(output_file, "PSJF\nExecution Sequence\n");
int remaining_tasks_psjf = num_tasks;
// Check for proper initialization and bounds
if (num_tasks > 0)
{
// PSJF
while (remaining_tasks_psjf > 0)
{
int shortest_job_index = -1;
int shortest_job_burst = INT_MAX; // Initialize to maximum possible value
int found_job = 0;
for (int i = 0; i < num_tasks; i++)
{
if (!tasks[i].completed && tasks[i].arrival_time <= current_time && tasks[i].burst_time < shortest_job_burst)
{
shortest_job_index = i;
shortest_job_burst = tasks[i].burst_time;
found_job = 1;
}
}
if (found_job)
{
fprintf(output_file, "%s\t%d\t", tasks[shortest_job_index].name, current_time);
if (tasks[shortest_job_index].burst_time <= TIME_QUANTUM)
{
current_time += tasks[shortest_job_index].burst_time;
fprintf(output_file, "%d\n", current_time);
tasks[shortest_job_index].waiting_time = current_time - tasks[shortest_job_index].arrival_time - tasks[shortest_job_index].burst_time; // waiting time calculation corrected
total_waiting_time += tasks[shortest_job_index].waiting_time; // accumulate total waiting time
tasks[shortest_job_index].completed = 1;
remaining_tasks_psjf--;
}
else
{
current_time += TIME_QUANTUM;
fprintf(output_file, "%d\n", current_time);
tasks[shortest_job_index].burst_time -= TIME_QUANTUM;
}
}
else
{
// No eligible tasks found, move to the next time unit
current_time++;
}
}
fprintf(output_file, "Waiting Time of Each Task\n");
for (int i = 0; i < num_tasks; i++)
{
fprintf(output_file, "%s\t%d\n", tasks[i].name, tasks[i].waiting_time);
}
float average_waiting_time = (float)total_waiting_time / num_tasks;
fprintf(output_file, "Average Waiting Time\n%.2f\n\n", average_waiting_time);
}
else
{
fprintf(stderr, "Error: No tasks to schedule.\n");
}
fclose(output_file);
return 0;
}
Tried to implement a PSJF algorithm from pseudocode, appears to not run after a particular segment of code. Program has to be quit forcefully, and does not complete.