I am working on an assignment where I am to read an input file (input.txt) like
ID: 3881817 • Letter: I
Question
I am working on an assignment where I am to read an input file (input.txt) like so:
We are given the PCB structure: struct PCB {int ProcId; int ProcPR, int CPUburst; int Reg[8]; int queueEnterClock, waitingTime; /* other info */ struct PCB *next;}
and a CPU to be written as int CPUreg[8] = {0};;
Given linked list variables: struct PCB *Head=NULL; struct PCB *Tail=NULL;
Other variables: int CLOCK=0; int Total_waiting_time=0; int Total_turnaround_time=0; int Total_job=0.
That was what given, my question is how do I go about doing the following task:
For each input line, read a line consisting of three integers: Process Id, Process Priority, CPU Burst time. (The values in the input.txt from left to right)
Dynamically create a struct PCB pointed by PCB, save the given data into correponding fields of PCB, set all PCB->Reg[8] to the Process ID, set PCB->queueEnterClock and PCB->waitingTime to 0, then insert this PCB at the end of the link list.
Explanation / Answer
FILE* file = fopen("filename", "r");
if (file){
int ProcId, ProcPR, CPUburst;
// Read line by line till file end
while (fscanf(file, "%d %d %d", &ProcId, &ProcPR, &CPUburst) != EOF){
// Dynamically allocate memory for list node
struct PCB *node = (PCB*)malloc(sizeof(PCB));
node->ProcId = ProcId;
node->ProcPR = ProcPR;
node->CPUburst = CPUburst;
node->queueEnterClock = 0;
node->waitingTime = 0;
for(int i=0; i<8; i++){
node->Reg[i] = ProcId;
}
node->next = NULL;
// Insert node at end of linked list
if (Head == NULL){
Head = node;
Tail = node;
} else {
Tail->next = node;
Tail = node;
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.