Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

This code is in C language. Please explain what each line of the code MEAN/DOES

ID: 3720497 • Letter: T

Question

This code is in C language. Please explain what each line of the code MEAN/DOES by commenting at the end of the line?

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <unistd.h>
#include <malloc.h>


typedef struct node{

int processID, executionTimer;
struct node *next;

}node; //make a type node

node* CreateList();
void Add(int PID,int exeTime);
node* ScanList(node *end);
node* RemoveCurrent(node *Current);
bool isEmpty();
void PrintList();

node *head = NULL, *Back;

int main(int argc, char **argv){
free(ScanList(CreateList(argv[1])));
return 0;
}

node* CreateList(char *fileName){

/* char fileName[256];
printf("Enter a file to read:");
scanf("%s", fileName);*/

FILE *fP;
if(!(fP = fopen(fileName, "r"))){
printf("File doesn't exist ");
exit(0);
}

int PID, runTime;
while(!feof(fP)){
if(!(fscanf(fP, "%*4s%d%*1s%d%*2s ", &PID, &runTime))){
printf("CORROPTED FILE DATA ");//scan for the correct data we want
exit(0);
}
if(runTime <= 0){
printf("Run time error ");
exit(0);//Exit if runtime is less than 0
}
Add(PID, runTime);
}
fclose(fP);
return Back = (Back->next = head); //--> initializes the circular linked list
}

void Add(int PID, int exeTime){

node *newNode = malloc(sizeof(node));
newNode->processID = PID;
newNode->executionTimer = exeTime;
newNode->next = NULL;
if(!head){
head = newNode;
}
else{
Back->next=newNode;
}
Back = newNode;
}

node *ScanList(node *End){
int stoppingPoint, changed; //flag to stop program
while(isEmpty()){//while true
PrintList();
sleep(1);
stoppingPoint = End->processID;
do{
changed = 0;
End->executionTimer--;
if(!(End->executionTimer)){
if(End->processID == stoppingPoint){//activates flag if process removed is stopping point
changed = stoppingPoint = End->next->processID;
}
Back = End = RemoveCurrent(End);
if(!(End->next)){
Back = End;
changed = 0;
}
}
else{
Back = End = End->next;
}
}while((End->processID != stoppingPoint && Back->next) || changed);//runs while changed is true or while next isn't null
printf(" ");
}
return NULL;
}

node *RemoveCurrent(node *Current){

printf(" PID being removed: %d", Current->processID);

if(Current->processID == Current->next->processID){
Current->next = NULL;
return Current;
}

node *temp = Current->next;
do{
temp = temp->next;
}while(temp->next->executionTimer);

node *nextNode = temp->next->next;
free(temp->next);
return temp->next = nextNode;
}

bool isEmpty(){
return !(Back->next) ? 0 : 1;
}

void PrintList(){
int stoppingPoint = Back->processID;
do{
printf("PID: %d Runtime: %d ", Back->processID, Back->executionTimer);
Back = Back->next;
}while(Back->processID != stoppingPoint);
}

Explanation / Answer

Please find the commented code below:-

#include <stdio.h>

#include <stdlib.h>

#include <stdbool.h>

#include <unistd.h>

#include <malloc.h>

typedef struct node{ // create a structure called node

int processID, executionTimer; // create 2 member variables of type int in the structure

struct node *next; // create a pointer of type node called next that will point to the next structure element

}node; //make a type node

node* CreateList();

void Add(int PID,int exeTime);

node* ScanList(node *end);

node* RemoveCurrent(node *Current);

bool isEmpty();

void PrintList();

node *head = NULL, *Back; // define two pointer variables of type node which is a structure

int main(int argc, char **argv){ // main is the entry point of executionTimer--

free(ScanList(CreateList(argv[1])));

return 0; // return SUCCESS_CODE for main after execution

}

node* CreateList(char *fileName){ // create a list of elements of type node structure

/* char fileName[256];

printf("Enter a file to read:");

scanf("%s", fileName);*/

FILE *fP; // create a File pointer for doing FILE Handling

if(!(fP = fopen(fileName, "r"))){ // open the file in Read Mode and check if the file pointer is null or not

printf("File doesn't exist "); // if the file cannot be opened File Pointer will return null and display the error

exit(0); // Exit the application

}

int PID, runTime;

while(!feof(fP)){ // Loop until the End of File is reached which we are currently parsing

if(!(fscanf(fP, "%*4s%d%*1s%d%*2s ", &PID, &runTime))){ // Check the Data Format to be read for PID and runTime

printf("CORROPTED FILE DATA ");//scan for the correct data we want

exit(0); // Exit the application

}

if(runTime <= 0){ // check if Runtime <= 0

printf("Run time error ");

exit(0);//Exit if runtime is less than 0

}

Add(PID, runTime);

}

fclose(fP); // close the File Pointer after reading all the data from file

return Back = (Back->next = head); //--> initializes the circular linked list

}

void Add(int PID, int exeTime){

node *newNode = malloc(sizeof(node)); // Dynamically allocate memory of size node and return the memeory to newNode

newNode->processID = PID; // initialize the processID member variable of node with PID

newNode->executionTimer = exeTime; // initialize the executionTimer member variable of node with exeTime

newNode->next = NULL; // initailly the next pointer is set to NULL

if(!head){ // if head is not NULL

head = newNode; // make this new node as head (ie make this as the starting node of the list)

}

else{

Back->next=newNode; // add this node after the last node

}

Back = newNode; // make this new node as the last node by assigning it to Back

}

node *ScanList(node *End){

int stoppingPoint, changed; //flag to stop program

while(isEmpty()){//while true

PrintList();

sleep(1);

stoppingPoint = End->processID;

do{

changed = 0;

End->executionTimer--;

if(!(End->executionTimer)){

if(End->processID == stoppingPoint){//activates flag if process removed is stopping point

changed = stoppingPoint = End->next->processID;

}

Back = End = RemoveCurrent(End);

if(!(End->next)){

Back = End;

changed = 0;

}

}

else{

Back = End = End->next;

}

}while((End->processID != stoppingPoint && Back->next) || changed);//runs while changed is true or while next isn't null

printf(" ");

}

return NULL;

}

node *RemoveCurrent(node *Current){

printf(" PID being removed: %d", Current->processID);

if(Current->processID == Current->next->processID){

Current->next = NULL;

return Current;

}

node *temp = Current->next;

do{

temp = temp->next;

}while(temp->next->executionTimer);

node *nextNode = temp->next->next;

free(temp->next);

return temp->next = nextNode;

}

bool isEmpty(){

return !(Back->next) ? 0 : 1; // check if the list contains any next node if yes return 1 else return 0

}

void PrintList(){ // Print the list values

int stoppingPoint = Back->processID; // take the stopping point from the Back pointer

do{

printf("PID: %d Runtime: %d ", Back->processID, Back->executionTimer); // print the values

Back = Back->next; // move the pointer to the next node

}while(Back->processID != stoppingPoint); // continue till we reach that node from where we started(i.e till we reach stopping point)

}

Please let me know in case of any clarifications reached. Thanks!