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