C++ Fix my code
This code is for imitating the round robin cpu scheduling algorithim using linked lists. Currently I am able to input processes and store / display them properly. The issue begins somewhere after I have displayed the processlist (I get a segmentation fault (core dumped) or the code doesnt seem to run). I have marked the location of where I think the issue begins with a comment. Please fix the code so that it is working properly. Note: sigalarm is used in the code so a linux terminal is probably required to compile and run this.
#include
#include
#include
#include
#include
#include
using namespace std;
volatile sig_atomic_t print_flag = false;
struct NodeType
{
string value1; //process name
int value2; //process time
NodeType* next;
void DisplayLinkedList(NodeType* head)
{
NodeType* p;
p = head; //initialize pointer p to point to the first node in the linked list
cout << "Displaying the
list of processes: " << endl;
cout <<
"^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^" << endl;
while (p != NULL)
{
cout <<
p->value1 << " " << p->value2 <<
endl;
p = p->next;
//update p to point to next node in the linked list ...
}
cout <<
"^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^" << endl;
cout << " " <<
endl;
}
void createnode(NodeType*& head, string x, int
y)
{
NodeType* p = new NodeType;
if (head == NULL)
{
p->value1 =
x;
p->value2 =
y;
p->next =
NULL;
head = p;
}
else
{
p = head;
while
(p->next != NULL)
p = p->next;
p->next = new
NodeType;
p =
p->next;
p->value1 =
x;
p->value2 =
y;
p->next =
NULL;
}
}
bool IsEmpty(NodeType* h) const
{
return h == NULL;
}
bool IsZero(NodeType* h) const
{
return h->value2 == 0;
}
void DeleteNode(NodeType*& head)
{
NodeType* p = head;
head = head->next;
delete p;
}
void roundrobin(NodeType*& head)
{
head->value2 -= 3;
if (head->value2 == 0) // no
time remaining
{
cout <<
head->value1 << " Finished" << endl;
DeleteNode(head);
}
else // time remaining
{
NodeType* p =
head;
p->next =
NULL;
head =
head->next;
NodeType* q =
head;
while
(q->next != NULL)
q = q->next;
q->next =
p;
}
}
};
void handle_alarm(int sig) // interrupt handler, manipulates
flags to allow roundrobin to run
{
print_flag = true;
}
int main()
{
NodeType* head = NULL;
NodeType a;
string v, x, y;
int argc = 0;
int z = 0;
while(true)
{
cout << "Enter your
processes, to end press '^d' " << endl;
getline(cin, v);
if (v == "^d")
break;
//cin >> a;
int index = v.find(" "); // value
is -1 when input is 6/21/1997 aka no desc
x = v.substr(0, index); // -1
indicates end of string pos
y = v.substr(index + 1,
v.length());
z = stoi(y);
a.createnode(head, x, z);
argc++;
}
a.DisplayLinkedList(head);
// ISSUE BEGINS SOMEWHERE AFTER HERE
signal(SIGALRM, handle_alarm);
alarm(3);
while (print_flag) //when I used while(true) I get the
core dumped issue
{
if
(a.IsEmpty(head) == false) // list not empty
{
a.roundrobin(head);
a.DisplayLinkedList(head);
}
else
{
cout << "No more processes left" <<
endl;
print_flag = false;
}
//print_flag = false;
alarm(3);
}
return 0;
}
#include<iostream>
#include<signal.h>
#include<unistd.h>
// #include
// #include
// #include
using namespace std;
volatile sig_atomic_t print_flag = false;
struct NodeType
{
string value1; //process name
int value2; //process time
NodeType* next;
void DisplayLinkedList(NodeType* head)
{
NodeType* p;
p = head; //initialize pointer p to point to the first node in the linked list
cout << "Displaying the list of processes: " << endl;
cout << "^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^" << endl;
while (p != NULL)
{
cout << p->value1 << " " << p->value2 << endl;
p = p->next; //update p to point to next node in the linked list ...
}
cout << "^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^" << endl;
cout << " " << endl;
}
//refactored
void createNode(NodeType* &head, string x, int y)
{
NodeType* p = new NodeType;
p->value1 = x;
p->value2 = y;
p->next = NULL;
if (head == NULL)
{
head = p;
}
else
{
NodeType* temp = head;
while (temp->next != NULL)
temp = temp->next;
temp->next = p;
}
}
bool IsEmpty(NodeType* h) const
{
return h == NULL;
}
bool IsZero(NodeType* h) const
{
return h->value2 == 0;
}
void DeleteNode(NodeType*& head)
{
NodeType* p = head;
head = head->next;
delete p;
}
void roundrobin(NodeType*& head)
{
head->value2 -= 3;
//changed condition "== 0" to "<= 0"
if (head->value2 <= 0) // no time remaining
{
cout << head->value1 << " Finished" << endl;
DeleteNode(head);
}
else // time remaining
{
NodeType* p = head;
head = head->next; //this line and the following line have been swaped
p->next = NULL;
if(head){
NodeType* q = head;
while (q && q->next != NULL){
q = q->next;
}
q->next = p;
}
}
}
};
void handle_alarm(int sig) // interrupt handler, manipulates flags to allow roundrobin to run
{
print_flag = true;
}
int main()
{
NodeType* head = NULL;
NodeType a;
string v, x, y;
int argc = 0;
int z = 0;
while(true)
{
cout << "Enter your processes, to end press '^d' " << endl;
getline(cin, v);
if (v == "^d")
break;
//cin >> a;
int index = v.find(" "); // value is -1 when input is 6/21/1997 aka no desc
x = v.substr(0, index); // -1 indicates end of string pos
y = v.substr(index + 1, v.length());
z = stoi(y);
a.createNode(head, x, z);
argc++;
}
a.DisplayLinkedList(head);
// ISSUE BEGINS SOMEWHERE AFTER HERE
signal(SIGALRM, handle_alarm);
alarm(3);
while (true) //when I used while(true) I get the core dumped issue
{
if (a.IsEmpty(head) == false) // list not empty
{
a.roundrobin(head);
a.DisplayLinkedList(head);
}
else
{
cout << "No more processes left" << endl;
break;
//print_flag = false;
}
//print_flag = false;
alarm(3);
}
return 0;
}
Sample Output:
There error was in void roundrobin(NodeType*& head); method
Original code:
NodeType* p = head;
p->next = NULL; //This was setting head->next to NULL
head = head->next;
Modified code:
NodeType* p = head;
head = head->next; //this line
and the following line have been swaped
p->next = NULL;
Also I have refactored createNode method to avoid duplicate code.
Also note the header files were missing from the code you have provided so in my code only necessary header files are present to run the code.
If you need any futher clarification then write a comment down beolw. Thanks!
Get Answers For Free
Most questions answered within 1 hours.