gpt4 book ai didi

c - 返回结构并保存在不同 .c 文件中的结构变量中

转载 作者:行者123 更新时间:2023-11-30 17:09:00 24 4
gpt4 key购买 nike

我想从我的优先级队列中删除一个事件struct,并在我的 pqueue.c 文件中返回该事件。然后,我想将返回的事件保存在 run.c 文件的 struct 事件变量中。当我尝试将返回的事件保存在 run.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;
}
}
}

运行.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;
}
}
}

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

最佳答案

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

运行.c:

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

关于c - 返回结构并保存在不同 .c 文件中的结构变量中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33448822/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com