gpt4 book ai didi

c - 数据被模拟的酒店叫醒服务覆盖

转载 作者:太空宇宙 更新时间:2023-11-04 03:51:01 24 4
gpt4 key购买 nike

我今天早些时候发布了这个,但我意识到我之前放了一个旧版本的未编译代码,但是这个代码在 gcc 中编译,我必须创建一个酒店唤醒调用注册表,当一个新的警报生成时它被打印出来,正确地放入列表中(按照最近的警报的顺序),然​​后打印出调用发生的时间。

问题是,当插入节点(insertNode())时,添加到列表中的addedNode会覆盖头部,并写入下一个节点,而不是只将alarmData写入头节点。

#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
#include <pthread.h>
#include <time.h>
#include <signal.h>

#define ETIMEDOUT 110

pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;

const int MAX_ROOM_NUMBER = 1000;
const int MAX_SECOND = 100;
static int NotCancelled = 0;
int REGISTER = 0;

typedef struct wakeUp
{

int room; //Structure that has the room to awake
time_t alarm;//and the time that the room wishes to wake
}wakeUp;

typedef struct node
{

wakeUp awake; // Nodes used as a list to hold the of alarms ordered earliest node first
struct node* nextNode;// it contains the struct WakeUp and a pointer to the next node
}node;

node * AlarmData;
pthread_t insert,wakeRoom;


static void *
printAlarmsData(node* first)
{
if (first == NULL )
{
printf("This List Of Alarms is empty!\n");
return;
}
else
{
node* p = first;
int count = 0;
while (p != NULL )
{
printf("%d %s, \n", p->awake.room, ctime(&p->awake.alarm));
count++;
if (p->nextNode == NULL || count == 10)
{
return;
}

}
}
}

static int
removeAlarm(wakeUp * first)
{ //When the alarm occurs remove the first Node

pthread_mutex_lock(&lock); // Hold the lock

time_t current_time;
time(&current_time);

if (current_time < first->alarm)
{
printf("Waiting...\n");
pthread_cond_signal(&cond);
pthread_mutex_unlock(&lock);
return 0;
}
else
{
AlarmData = AlarmData->nextNode;
pthread_cond_signal(&cond);
pthread_mutex_unlock(&lock);
return 1;
}
}

int
length(node* first)
{ // Shows how many Alarms are left.
node* p = first;
int count = 0;

while (p != NULL )
{
count++;
p = p->nextNode;

}
return count;
}

// insert Node function

void
insertNode(node* addedNode)
{ //this method here
pthread_mutex_lock(&lock); // head keeps getting overwritten instead
node *head, *temp, *prev, *next; // of writing data into AlarmData
temp = (node*) malloc(sizeof(node));
head = AlarmData;
temp = addedNode;
temp->nextNode = NULL;
if (!head)
{
printf("Head is null\n");
head = temp;
}
else
{
printf("Head is not null\n");
prev = NULL;
next = head;
printf("Existing Data Showing Below\n");
while (next && (next->awake.alarm) <= (addedNode->awake.alarm))
{
printf("----> %d %s\n", next->awake.room, ctime(&next->awake.alarm));
prev = next;
next = next->nextNode;
}
if (!next)
{
prev->nextNode = temp;
}
else
{
if (prev)
{
temp->nextNode = prev->nextNode;
prev->nextNode = temp;
}
else
{
temp->nextNode = head;
head = temp;
}
}

}
AlarmData = (node*) malloc(sizeof(node));
AlarmData = head;
pthread_cond_signal(&cond);
pthread_mutex_unlock(&lock);
}

static void
cancelHandler(int signo)
{
printf("\nAre you sure you want to exit this program Y/N:");
char input;
scanf("%c", &input);
if (input == 'y')
{
NotCancelled = 1;
wakeUp n = AlarmData->awake;
while (AlarmData != NULL )
{
removeAlarm(&n);
}

pthread_cancel(insert);

pthread_cancel(wakeRoom);

pthread_join(insert, NULL );
printf("Goodbye from insert thread");
pthread_join(wakeRoom, NULL );
printf("Goodbye from wakeUp thread");
printf("Program Exited");
return;
}
}

static void *
insertHandler()
{
printf("\nwent to insertHandler\n");
while (NotCancelled == 0)
{
sleep(rand() % 2);
int roomId = rand() % MAX_ROOM_NUMBER + 1;

time_t timeToWake = time(NULL ) + (rand() % MAX_SECOND + 1);
wakeUp wake;
wake.room = roomId;
wake.alarm = timeToWake;

node newAlarm;
newAlarm.awake = wake;
REGISTER++;
insertNode(&newAlarm);

printf("After Registering:\t%d\t%s\n\n", roomId, ctime(&timeToWake));
}
return;
}
static void *
wakeRoomHandler()
{
printf("\nwent to wakeRoomHandler\n");
while (NotCancelled == 0)
{
sleep(rand() % 15);

if (AlarmData != NULL )
{
wakeUp wake = AlarmData->awake;
time_t timeToWake = AlarmData->awake.alarm;
int roomId = AlarmData->awake.room;
printf("Checking:\t%d\t%s\n\n", roomId, ctime(&timeToWake));
int status = removeAlarm(&wake);
if (status == 1)
{
printf("Wake Up:\t%d\t%s\n\n", roomId, ctime(&timeToWake));
}
}
}
return;
}

int
main(void)
{
sigset_t set;
sigemptyset(&set);
sigaddset(&set, SIGINT);
sigset(SIGINT, cancelHandler);
pthread_create(&insert, NULL, insertHandler, NULL );
pthread_create(&wakeRoom, NULL, wakeRoomHandler, NULL );
while (1)
{
}

return (0);

}

最佳答案

您的代码存在一些问题。

temp = (node*)malloc(sizeof(node));
head = AlarmData;
temp = addedNode;

在上面的代码片段中,您为一个新节点分配了内存并将 temp 指向它。然后向下两行将 temp 指向 addedNode。您在第一行中分配的内存现在丢失了,因为您已经覆盖了指向该内存的指针。您可能打算使用 *temp = *addedNodeaddedNode 中的数据复制到 temp 中。

您不需要在函数结束时为AlarmData 分配新节点。您只需更新 AlarmData 指针以指向新的磁头(如果磁头确实已更改)。

关于c - 数据被模拟的酒店叫醒服务覆盖,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20507325/

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