Question

C++ Fix my code This code is for imitating the round robin cpu scheduling algorithim using...

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;
}

Homework Answers

Answer #1
#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!

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
my code has several functions; delete and backward functions are not working, rewrite the code for...
my code has several functions; delete and backward functions are not working, rewrite the code for both functions and check them in the main: #include<iostream> #include<cassert> using namespace std; struct nodeType {    int info;    nodeType *link; }; class linkedList { public:    void initializeList();    bool isEmptyList();    void print();    int length();    void destroyList();    void insertFirst(int newItem);    void insertLast(int newItem);    int front();    linkedList();    void copyList(const linkedList otherList);    void insertNewValue(int value);...
c++ Language Fix this code to have it concatenate two strings st1 and st2 and put...
c++ Language Fix this code to have it concatenate two strings st1 and st2 and put the result in st3 with a space separator. it has to be done using array representation of strings #include <iostream> using namespace std; #include <string> int main() { string st1,st2, st3; int c = 0, i =0;    cout << "Enter a string: "; cin >> st1; cout << "Enter another string: "; cin >> st2;    while (st1[c] != '\0'){ st3[c] = st1[c];...
This code it's not working, fix it for me please #include <iostream> using namespace std; class...
This code it's not working, fix it for me please #include <iostream> using namespace std; class People {    string name;    double height; public:    void setName(string name)    {        this->name = name;    }    void setHeight(double height)    {        this->height = height;    }    double getHeight() {        return height;    }    string getName()    {        return name;    } }; int main() {    const int size...
can someone edit my c++ code where it will output to a file. I am currently...
can someone edit my c++ code where it will output to a file. I am currently using xcode. #include <iostream> #include <cctype> #include <cstring> #include <fstream> using namespace std; bool inputNum(int [],int&,istream&); void multiply(int[],int,int[],int,int[],int&); void print(int[],int,int,int); int main() {ifstream input; int num1[35],num2[35],len1,len2,num3[60],len3=10,i; input.open("multiplyV2.txt"); //open file if(input.fail()) //is it ok? { cout<<"file did not open please check it\n"; system("pause"); return 1; }    while(inputNum(num1,len1,input)) {inputNum(num2,len2,input); multiply(num1,len1,num2,len2,num3,len3); print(num1,len1,len3,1); print(num2,len2,len3,2); for(i=0;i<len3;i++) cout<<"-"; cout<<endl; print(num3,len3,len3,1); //cout<<len1<<" "<<len2<<" "<<len3<<endl; cout<<endl;    } system("pause"); } void...
Need someone to fix my code: #include<iostream> using namespace std; struct student { double firstQuizz; double...
Need someone to fix my code: #include<iostream> using namespace std; struct student { double firstQuizz; double secondQuizz; double midTerm; double finalTerm; string name; }; int main() { int n; cout<<"enter the number of students"<<endl; cin>>n; struct student students[n]; int i; struct student istudent; for(i=0;i<n;i++) {    cout<<"Student name?"; cin >> istudent.name; cout<<"enter marks in first quizz , second quizz , mid term , final term of student "<<i+1<<endl; cin>>students[i].firstQuizz>>students[i].secondQuizz>>students[i].midTerm>>students[i].finalTerm; } for(i=0;i<n;i++) { double marks=0; double score=students[i].firstQuizz+students[i].secondQuizz+students[i].midTerm+students[i].finalTerm; marks=(students[i].firstQuizz*0.25)+(students[i].secondQuizz*0.25)+(students[i].midTerm*0.25)+(students[i].finalTerm*0.50); double totalArrgegateMarks =...
How to stop the program from exiting after display detail. When there is food detail, it...
How to stop the program from exiting after display detail. When there is food detail, it will display and exit the program. What can i do to make it not exit the program and back to main menu. #include <iostream> #include <iomanip> #include<string.h> using namespace std; struct food{ int order_id; string food_code,flavor,customer_id; string address,name; int weight,unit_price,qty,contact_number; struct food *next; };    class Foodsystem{ food *head,*temp,*temp2,*end; static int id;    public: Foodsystem(){ head=NULL;end=NULL;} void Place_Order(); void View_food_details(); void Modify_food_details(); void Delete_food_details();...
No matter what I do I cannot get this code to compile. I am using Visual...
No matter what I do I cannot get this code to compile. I am using Visual Studio 2015. Please help me because I must be doing something wrong. Here is the code just get it to compile please. Please provide a screenshot of the compiled code because I keep getting responses with just code and it still has errors when I copy it into VS 2015: #include <iostream> #include <conio.h> #include <stdio.h> #include <vector> using namespace std; class addressbook {...
i want to complete this code to insert a new node in the middle of list...
i want to complete this code to insert a new node in the middle of list (take a node data from user, search the node and insert new node after this node). this is the code #include <iostream> #include <stdlib.h> using namespace std ; struct Node{                int data;                Node *link ;}; struct Node *head=NULL, *tail=NULL; /* pointers to Node*/ void InsertFront(); void InsertRear(); void DeleteFront(); void DeleteRear(); int main(){                int choice;                do{                               cout << "1:...
It is about C++linked list code. my assignment is making 1 function, in below circumstance,(some functions...
It is about C++linked list code. my assignment is making 1 function, in below circumstance,(some functions are suggested for easier procedure of making function.) void search_node(struct linked_list* list, int find_node_ value) (The function to make) This function finds the node from the list that value is same with find_node_value and count the order of the node. This function should print message “The order of (node_value) is (order).” and error message “Function search_node : There is no such node to search.”....
/* Write a function that looks for a particular person in their respective linked list The...
/* Write a function that looks for a particular person in their respective linked list The only place you need to write code is the "find" method in the linked list class */ #include <bits/stdc++.h> using namespace std; string ltrim(const string &); string rtrim(const string &); #define BUFFLEN 10 /* Each "person" is defined by their name, zipcode, and their pet's name. Persons are hashed by their zipcode. */ //---------------------------------------------------------------------------- /* function declarations ------------------------*/ int computeKey(int); void add_to_buffer(string,int,string); void find_in_buffer(int);...