涉及循环调度的代码无法运行或出现分段错误核心转储

问题描述

(C ++)我的代码应该模拟使用链接列表的循环cpu调度算法(因此接受时间为ex的进程名称列表:processA 10,从时间中减去3,如果结果大于0,则为移至列表末尾。此操作将一直持续到处理时间达到0为止,届时处理结束。

我的程序正确接受并显示进程列表及其时间。因此,我没有包含用于接受,创建和显示列表的代码显示用户输入的列表后,该程序由于某种原因突然终止。

我的输出

[John@fish lab2]$ ./a.out
Enter your processes,to end press '^d'
ProcessA 4
Enter your processes,to end press '^d'
ProcessB 10
Enter your processes,to end press '^d'
ProcessC 6
Enter your processes,to end press '^d'
^d
displaying the list of processes:
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
ProcessA 4
ProcessB 10
ProcessC 6
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^


我尝试修改while循环,因为我认为该标志存在问题,因此我将其从while(print_flag)更改为while(true)并将break语句置于else条件中。

我的输出

与最后一个输出相同,不同的是:“分段错误(核心已转储)”

我不知道如何解决根本问题。任何帮助表示赞赏。

#include <iostream>
#include <list>
#include <stdio.h>
#include <unistd.h>
#include <signal.h>
#include <string>

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

    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(" "); 
        x = v.substr(0,index); 
        y = v.substr(index + 1,v.length());
        
        z = stoi(y);
        a.createnode(head,z);

        argc++;
    }

    a.displayLinkedList(head);
    
    signal(SIgalRM,handle_alarm);
    alarm(3);
    while (print_flag)
    {
        
            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;
}

解决方法

我认为您需要对roundrobin函数进行一些小的更改才能使其正常工作:

您需要更新条件以检查过程是否完成 head->value2 <= 0而不是head->value2 == 0,因为head->value2 == 0似乎仅适用于value2被3整除的任何进程,并且会错过其他进程,因为它们将减为负数。通过这个表达式head->value2 -= 3

您还需要将p->next = NULL;行放在head = head->next;之后而不是之前。否则,head将始终为NULL,因为p当前为head

最后,您需要检查head是否是剩下的唯一进程(head->next != NULL),然后再切换到下一个进程。否则,如果headNULL,则会使head->next变成NULL,从而导致分段错误错误

  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;
      if (head->next != NULL) {
        head = head->next;
        p->next = NULL;
        NodeType* q = head;
        while (q->next != NULL)
          q = q->next;
        q->next = p;
      } 
    }
  }