#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include "locker.h"
void QueueInit(Queue* p)
{
p->front = NULL;
p->rear = NULL;
}
int QIsEmpty(Queue* p)
{
if(p->front == NULL)
{
return 1;
}
return 0;
}
void Enqueue(Queue* p, int data)
{
Node* newNode = (Node*)malloc(sizeof(Node));
newNode->next = NULL;
newNode->id = data;
if(QIsEmpty(p))
{
p->front = newNode;
p->rear = newNode;
} else {
p->rear->next = newNode;
p->rear = newNode;
}
}
void attachEnqueue(Queue* p, int user_id)
{
Node* temp = p->front;
temp->user_id = user_id;
p->front = temp;
printf("Locker %d Owned By %d\n", temp->id, temp->user_id);
temp->owned = 1;
temp = temp->next;
}
int Dequeue(Queue* p)
{
Node* temp = p->front;
uint16_t item;
if(QIsEmpty(p))
{
printf("No element exists!");
exit(0);
} else {
item = temp->id;
p->front = temp->next;
free(temp);
if(temp == NULL)
{
p->rear = NULL;
}
return (item);
}
}
void printList(Queue* p)
{
Node* v = p->front;
while(v != NULL){
printf("Locker: %d\n", v->id);
v = v->next;
}
}
int count (Queue p)
{
int c = 0 ;
Node* temp = p.front ;
while ( temp != NULL )
{
temp = temp->next;
c++ ;
}
return c ;
}
void SearchQueue(Queue* p, int val1)
{
Node* v = p->front;
int sw = 0;
while( v != NULL)
{
if(v->id == val1)
{
printf("Locker ID: %d\n", val1);
printf("Lock Status: locked\n");
if(v->owned == 0){
printf("unowned\n");
} else if(v->owned == 1)
{
printf("owned by %d\n", v->user_id);
}
sw = 1;
}
v = v->next;
}
if(!sw)
{
printf("locker %d does not exists\n", val1);
}
}
int main(int argc, char* argv[])
{
Queue queue;
QueueInit(&queue);
char input[50];
char command[20];
int val1;
uint16_t id = 1;
while(1)
{
scanf(" %49[^\n]s", input);
sscanf(input, "%s %d", &command, &val1);
if(strcmp(command, "CREATE") == 0)
{
printf("New Locker created: %d\n", id);
Enqueue(&queue, id);
id++;
} else if(strcmp(command, "DISPLAY") == 0)
{
SearchQueue(&queue, val1);
} else if(strcmp(command, "ATTACH") == 0)
{
attachEnqueue(&queue, val1);
} else if(strcmp(command, "DISPLAYALL") == 0)
{
printList(&queue);
}else if(strcmp(command, "DELETE") == 0)
{
printf("Deleted the locker, %d\n",Dequeue(&queue));
}else if(strcmp(command, "QUIT") == 0)
{
printf("Good Bye!\n");
exit(0);
}
continue;
}
return 0;
}
这是我到目前为止的内容,“locker.h”的内容是:
#ifndef LOCKER_H
#define LOCKER_H
#include <stdint.h>
typedef struct locker_t {
uint16_t id;
uint16_t user_id;
uint8_t locked;
uint8_t owned;
int write_fd;
int read_fd;
struct locker_t* next;
}Node;
typedef struct queue_t {
Node* front;
Node* rear;
size_t size;
}Queue;
#endif
除了attachEnqueue部分外,一切正常。 目的是,当我创建储物柜1和储物柜2并输入ATTACH 20时, 如果我输入ATTACH 30,储物柜1的拥有者应为20岁,储物柜2的拥有者应为30岁。
然而,当我创建2个储物柜并且首先是ATTACH 20然后再次输入ATTACH 30时,储物柜1的所有者的值仅从20变为30,而不是将30所有者分配给储物柜2.
我100%确定attachEnqueue函数涉及错误的内容,但我真的不确定如何修改它..
另外,我需要包含一个“LOCK”命令来使锁定器是锁定还是解锁,但问题是,学校希望我通过使用信号SIGUSR来做到这一点。我该如何使用信号功能锁定或解锁储物柜?请问pthread.mutex.lock并解锁吗?
任何帮助或建议都会非常感谢!
答案 0 :(得分:0)
评论是对的。 attachEnqueue
的最后一行是temp = temp->next;
也许你假设temp
在你下次调用函数时会记住这些信息,但是现在,绝对不会。此行没有用,下次temp
将被分配到队列的前面
要解决这个问题,您可以在结构中创建一个标志,如上所述,向函数添加一个计数器参数,以跟踪队列中要附加的元素,或使temp
成为静态参数,因此其状态在电话之间保持不变。