0

I want to delete an Event struct from my priority queue and return that event in my pqueue.c file. I then want to save the returned Event in a struct Event variable in my run.c file. It's giving me a seg fault when I try to save the returned Event in a variable in run.c. Here is the code:

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:

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

Event struct in run.h:

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;
};
blob12
  • 141
  • 2
  • 12
  • 1
    Please post a [Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve). – R Sahu Oct 31 '15 at 06:49
  • Also: http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc – user4520 Oct 31 '15 at 07:34
  • Are you saying I should have struct Event *event = malloc(sizeof(struct Event)); instead of struct Event *event = (struct Event*)malloc(sizeof(struct Event));? I tried that but I'm still getting a seg fault. – blob12 Oct 31 '15 at 07:42
  • This is unrelated to your issue, so it won't help, but it is a better practice than using explicit casts, which is why I linked you to that question. – user4520 Oct 31 '15 at 09:02

1 Answers1

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);
}
A.S.H
  • 29,101
  • 5
  • 23
  • 50
  • Just to be clear, the only difference I see between your code and mine is that you removed "event = temp -> type;". I did that but it's still giving me a seg fault. The code works fine until the I try to use the returned Event. What is happening between returning the event and calling the deletePQ() method in run.c that is making my program return a seg fault? This is literally killing me. – blob12 Oct 31 '15 at 07:40
  • @blob12 no, that is not the only difference. There are many flaws; I was willing to explain them but then I found it would be simpler to provide some correct approach. For sake of example, in run.c, why do malloc the `event` pointer while you immediately assign it to something else? Second, when your queue is empty, what do you return from deletePQ? you just return something malloc'ed and non-initialized... I sincerely advice you to dig more deeply into it and understand the correct approach. – A.S.H Oct 31 '15 at 07:52
  • And btw, you might need to post the code of your insertion into the PQ, the error might be there as well. – A.S.H Oct 31 '15 at 07:54
  • @blob12 that looks to me like you make an error before that. Post more code please. – Magisch Oct 31 '15 at 08:08
  • @blob12 from seeing your function add() it is almost sure that the creation of the queue is wrong. Post the creation code (the place where you call add()) and the error will be hopefully clear. – A.S.H Oct 31 '15 at 14:45