No compilation error, no logical error (I believe). But the thing is, there is no output at all! If you see my code, I set up test print statements at places, including one at the extreme beginning of main(), even that is not getting printed. I am clueless.
I would be glad if someone could help me figure out what's going on.
Here is my code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
struct process
{
int PID;
int AT; // arrival time
int BT; // burst time
int ST; // start time
int CT; // completion time
int TAT; // turn-around time
int WT; // waiting time
int RT; // response time
int remaining_BT; // remaining burst time for round robin
};
#define PROCESS_LIMIT 100
#define TIME_QUANTUM 2
struct process processes[PROCESS_LIMIT]; // array to store processes
void display(int i);
void wait_time(int n, int m);
void create_process()
{
srand(time(NULL));
for (int i = 0; i < PROCESS_LIMIT; i++)
{
processes[i].PID = (i + 1);
// arrival time
int at = (rand() % 10) + 1;
if (i != 0)
{
processes[i].AT = processes[i-1].AT + at;
}
else
{
processes[i].AT = at;
}
// burst time
int bt = (rand() % 10) + 1;
processes[i].BT = bt;
processes[i].remaining_BT = bt;
processes[i].CT = 0;
processes[i].TAT = 0;
processes[i].WT = 0;
processes[i].RT = -1;
}
}
void dispatcher()
{
// FCFS algorithm
int current_time = 0;
int processes_completed = 0;
int i = 0;
printf("test 1");
while (processes_completed < PROCESS_LIMIT)
{
// check if the current process is ready to run
if (processes[i].remaining_BT > 0 && processes[i].AT <= current_time)
{
if (processes[i].RT == -1)
processes[i].RT = current_time - processes[i].AT;
int time_to_run = processes[i].remaining_BT <= TIME_QUANTUM ? processes[i].remaining_BT : TIME_QUANTUM;
processes[i].ST = current_time;
current_time += time_to_run;
processes[i].remaining_BT -= time_to_run;
if (processes[i].remaining_BT == 0) // check if the process has finished
{
processes[i].CT = current_time;
processes[i].TAT = processes[i].CT - processes[i].AT;
processes[i].WT = processes[i].TAT - processes[i].BT;
processes_completed++;
printf("Processes completed: %d\n", processes_completed);
}
}
i = (i + 1) % PROCESS_LIMIT;
}
}
void display(int i)
{
if (i == 0)
{
printf("PID AT BT ST CT TAT WT RT\n");
}
printf("%d %d %d %d %d %d %d %d\n", processes[i].PID, processes[i].AT, processes[i].BT, processes[i].ST, processes[i].CT, processes[i].TAT, processes[i].WT, processes[i].RT);
}
int main()
{
printf("test main 1");
create_process();
dispatcher();
printf("tester");
// display
for (int i = 0; i < PROCESS_LIMIT; i++)
{
display(i);
}
return 0;
}
Eithter add \n
to the printed buffer or use fflush(stdout);
after each printf statement.
The reason for that is because printf doesn't print immediately, until the stdout buffer is flushed.
stdout
buffer is flushed if :
Here's the updated code. Note that this solution only solves the printing issue you have. There's a bug in your program that causes the while loop to go infinite, but this question is focused on why it's not printing.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
struct process
{
int PID;
int AT; // arrival time
int BT; // burst time
int ST; // start time
int CT; // completion time
int TAT; // turn-around time
int WT; // waiting time
int RT; // response time
int remaining_BT; // remaining burst time for round robin
};
#define PROCESS_LIMIT 100
#define TIME_QUANTUM 2
struct process processes[PROCESS_LIMIT]; // array to store processes
void display(int i);
void wait_time(int n, int m);
void create_process()
{
srand(time(NULL));
for (int i = 0; i < PROCESS_LIMIT; i++)
{
processes[i].PID = (i + 1);
// arrival time
int at = (rand() % 10) + 1;
if (i != 0)
{
processes[i].AT = processes[i-1].AT + at;
}
else
{
processes[i].AT = at;
}
// burst time
int bt = (rand() % 10) + 1;
processes[i].BT = bt;
processes[i].remaining_BT = bt;
processes[i].CT = 0;
processes[i].TAT = 0;
processes[i].WT = 0;
processes[i].RT = -1;
}
}
void dispatcher()
{
// FCFS algorithm
int current_time = 0;
int processes_completed = 0;
int i = 0;
printf("test 1\n");
while (processes_completed < PROCESS_LIMIT)
{
// check if the current process is ready to run
if (processes[i].remaining_BT > 0 && processes[i].AT <= current_time)
{
if (processes[i].RT == -1)
processes[i].RT = current_time - processes[i].AT;
int time_to_run = processes[i].remaining_BT <= TIME_QUANTUM ? processes[i].remaining_BT : TIME_QUANTUM;
processes[i].ST = current_time;
current_time += time_to_run;
processes[i].remaining_BT -= time_to_run;
if (processes[i].remaining_BT == 0) // check if the process has finished
{
processes[i].CT = current_time;
processes[i].TAT = processes[i].CT - processes[i].AT;
processes[i].WT = processes[i].TAT - processes[i].BT;
processes_completed++;
printf("Processes completed: %d\n", processes_completed);
}
}
i = (i + 1) % PROCESS_LIMIT;
}
}
void display(int i)
{
if (i == 0)
{
printf("PID AT BT ST CT TAT WT RT\n");
}
printf("%d %d %d %d %d %d %d %d\n", processes[i].PID, processes[i].AT, processes[i].BT, processes[i].ST, processes[i].CT, processes[i].TAT, processes[i].WT, processes[i].RT);
}
int main()
{
printf("test main 1\n");
create_process();
dispatcher();
printf("tester\n");
// display
for (int i = 0; i < PROCESS_LIMIT; i++)
{
display(i);
}
return 0;
}
Add a printf statement after you update the i to see what I'm talking about. Hope this helped.