2015-10-31 54 views
0

我想從我的pqueue.c文件中刪除我的優先級隊列中的事件structreturn事件。然後,我想將返回的事件保存在我的run.c文件中的struct事件變量中。當我嘗試將返回的事件保存在run.c中的變量中時,它給了我一個seg錯誤。下面是代碼:返回結構並保存在不同的.c文件的結構變量中

pqueue.c:

#include <stdio.h> 
#include <stdlib.h> 
#include "pqueue.h" 
#include "run.h" 

//declare the head Node of the Queue and set it to null 
struct Event* head = NULL; 
int size = 0; 

//insert into the Queue by giving the element and it's priority as the   

    parameters 
//declare two temp Nodes, temp1 and temp2 
//allocate memory to temp1 and set it's data and priority to be the same as the element to be inserted 
//if the Queue is empty or if the priority of the element to be inserted is higher, add temp1 before the head and set it as the head 
//if the Queue is not empty and the priority of the new element is lower than the head's, then traverse through the Queue until the correct position for its priority is found 
void add(struct Event* newEvent) 
{ 
    //int newElement; 
    //int eventPriority; 
    struct Event* temp1; 
    struct Event* temp2; 

    printf("newEvent type %d\n", newEvent -> type); 

    temp1 = (struct Event*)malloc(sizeof(struct Event)); 

    //newEvent->type = newElement; 
    //newEvent->timeStamp = eventPriority; 

    //if the Queue is empty of if the element that is being inserted has a higher priority 
    if (isEmptyPQ() || newEvent->timeStamp < head->timeStamp) 
    { 

     temp1 = head; 
     head = newEvent; 
     head -> next = temp1; 
     size = size +1; 
    } 

    else { 
     temp1 = head; 

     while (temp1->next != NULL && temp1->next->timeStamp <= newEvent->timeStamp) 
     { 
      temp1 = temp1->next; 
     } 

     temp2 = temp1->next; 
     temp1->next = newEvent; 
     newEvent -> next = temp2; 
     size = size +1; 
    } 
} 

int getFirst() 
{ 
    //printf("%s %d", "First:", head->data); 
    return head->type; 
} 

//to delete an element, first check whether the Queue is empty. If it isn't, declare a temp Node and save the head Node to it 
//declare an int variable and save head's data to it 
//set the second Node in the Queue (the one after the head) as the head and free temp's memory to get rid of the previous head in memory 
//return the data of the deleted head 
struct Event *deletePQ() 
{ 
    struct Event *temp = NULL; 
    int event; 

    if (isEmptyPQ()) 
    { 
     printf("EMPTY\n"); 
    } 

    else 
    { 
     temp = head; 
     //event = temp -> type; 
     head = head->next; 
     // free(temp); 

      size = size -1; 

    } 

    printf("%s %d %s", "Deleted:", event, "\n"); 
    printf("hello\n"); 
    printf("temp Type %d\n", temp -> type); 
    return temp; 
} 


int sizePQ() 
{ 
    return size; 
} 

//check to see whether a Queue is empty or not by returning 1 if it is and 0 if it isn't 
int isEmptyPQ() 
{ 
    if (head == NULL) 
     return 1; 
    else 
     return 0; 
} 

//to print the Queue, first check whether it's empty. If it isn't, then traverse through the Queue and print out each 
//Node's data and priority in the Queue 
void printPQ() { 
    struct Event* temp; 
    temp = head; 

    if (isEmptyPQ()) 
    { 
     return; 
    } 

    else { 
     while (temp != NULL) 
     { 
      printf("%s %d %s %d", "Data:", temp->type, "Priority:",temp->timeStamp); 
      printf("\n"); 
      temp = temp->next; 
     } 
    } 
} 

run.c:在run.h

while (clock_time <= runtime) { 
printf("In the while loop \n"); 

// if there are events in the queue... 
    // DEQUEUE an event from the event queue 
    if(!isEmptyPQ()) { 
    struct Event *event = malloc(sizeof(struct Event)); 
    printf("queue is not empty\n"); 
    struct Event *event = deletePQ(); 
    printf("Event type %d\n", event -> type); 
    // UPDATE clock to the priority of the dequeued event 
    clock_time = event -> timeStamp; 

    if(event -> type == 1) { 
     printf("event type is to create a new process\n"); 
     // create a new process 
     createNewProcess(event, clock_time, stats, event -> process_type); 
    } else if (event -> type == 2) { 
     printf("event type is to make a decision\n"); 
     schedulingDecision(event, contextSwitch, cpu_array, numCPUS, quantum, stats); 
    } else if (event -> type == 4 || event -> type == 5 || event -> type == 6) { 
     printf("event type is to remove something from the CPU\n"); 
     removeProcess(event -> type, event, cpu_array, contextSwitch, stats); 
    } else { 
     return; 
    } 
    } else { 
     if(beginning == 1) { 
     // struct Event* newEvent = (struct Event*)malloc(sizeof(struct Event)); 
     // createNewProcess(newEvent, clock_time, stats, newEvent -> process_type); 
     beginning = 0; 
     } 
    } 
    } 

事件結構:

struct Event { 
    int timeStamp; 
    int type; 
    struct Event *next; 
    // create a process of x type == 1 
    // schedulingDecision = 2 
    // remove a process 
     // terminate == 4 
     // go to IO == 5 
     // quantum expire == 6 
    // return from IO == 7 
    struct Process *process; 
    int process_type; 
}; 
+1

請張貼[最小,完整的,並且可驗證示例](http://stackoverflow.com/help/mcve)。 –

+0

另請參見:http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc – szczurcio

+0

你是說我應該有結構Event * event = malloc(sizeof(struct Event));而不是struct Event * event =(struct Event *)malloc(sizeof(struct Event));?我嘗試過,但我仍然遇到seg故障。 – blob12

回答

0

pqueue.c:

Event *popFromPQ() 
{ 
    struct Event *temp = NULL; 
    if(isEmptyPQ()) printf("EMPTY\n"); 
    else 
    { temp = head; 
    head = head->next; 
    --size; 
    printf("popped from queue: %d\n", temp->type); 
    } 
    return temp; 
} 

run.c:

struct Event *event = popFromPQ(); 
if(event != NULL) 
{ 
    do stuff, process the event... 
    free(event); 
} 
+0

爲了清楚起見,我在你的代碼和我的代碼之間唯一的區別是你刪除了「event = temp - > type;」。我這樣做了,但它仍然給我一個seg故障。代碼工作正常,直到我嘗試使用返回的事件。在返回事件和調用run.c中的deletePQ()方法之間發生了什麼,使我的程序返回seg故障?這實際上是在殺我。 – blob12

+0

@ blob12不,這不是唯一的區別。有許多缺陷;我願意解釋他們,但後來我發現提供一些正確的方法會更簡單。舉例來說,在run.c中,爲什麼malloc指向'event'指針,而你立即把它分配給別的東西呢?其次,當你的隊列爲空時,你從deletePQ返回什麼?你只是返回malloc'ed和非初始化的東西......我真誠地建議你深入挖掘並理解正確的方法。 –

+0

順便說一句,您可能需要將插入的代碼發佈到PQ中,錯誤也可能在那裏。 –