我今天早些时候发布了这个,但我意识到我之前提出了我的未编译代码的旧版本,但是此代码在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(¤t_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);
}
答案 0 :(得分:1)
您的代码存在一些问题。
temp = (node*)malloc(sizeof(node));
head = AlarmData;
temp = addedNode;
在上面的代码段中,您为新节点分配内存并在其上指向temp
。向下两行,然后在temp
指向addedNode
。您在第一行中分配的内存现在已丢失,因为您已覆盖指向该内存的指针。您可能打算使用*temp = *addedNode
将addedNode
中的数据复制到temp
。
您不需要在函数末尾为AlarmData
分配新节点。您只需更新AlarmData指针以指向新头(如果头确实已更改)。
答案 1 :(得分:0)
您为temp分配了一个内存,但是在函数inserNode的第三行中,您已将addedNode的地址分配给temp。
在函数insertNode的末尾,你对全局变量AlarmData做了同样的事情,你为它分配了一个内存,但是后来将头的地址分配给了AlarmData。最后,它始终是AlarmData最终的头部地址,而不是您想要的头部副本。
您应该将head的内容复制到AlarmData的已分配内存中。与代码开头的temp相同。
请注意,指针包含地址,而不是值。将指针地址分配给另一个指针与复制指针所指向的变量的值不同。