我想從我的pqueue.c文件中刪除我的優先級隊列中的事件struct
和return
事件。然後,我想將返回的事件保存在我的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;
};
請張貼[最小,完整的,並且可驗證示例](http://stackoverflow.com/help/mcve)。 –
另請參見:http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc – szczurcio
你是說我應該有結構Event * event = malloc(sizeof(struct Event));而不是struct Event * event =(struct Event *)malloc(sizeof(struct Event));?我嘗試過,但我仍然遇到seg故障。 – blob12