#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"입니다 C :단독 내가 지금까지 가지고있는 이것은 큐
#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 부분을 제외하고 잘 작동합니다. I 첨부 20 로커 1의 소유자 (20)이어야하고 I 입력 (30)을 부착하는 경우 때 다시 로커 2`s 소유자, 그러나 30
되어야 로커 1 로커 2 입력을 생성 할 때 목적이며 2 개의 로커를 만들고 첫 번째로 ATTACH 20을 작성한 다음 ATTACH 30을 다시 입력하면 라커 1의 소유자 값은 20에서 30으로 변경되며 30 소유자는 라커 2로 할당되지 않습니다.
attachEnqueue 함수 잘못된 내용이 포함되어 있지만 수정 방법을 잘 모르고 있습니다 ..
또한 잠금 장치를 잠금 또는 잠금 해제 할 수 있도록 "잠금"명령을 포함해야하지만 문제는 학교에서 SIGUSR 신호를 사용하여이 작업을 수행하기를 원합니다. 신호 기능을 사용하여 사물함을 잠 그거나 잠금을 해제해야합니까? pthread.mutex.lock을 작동시키고 잠금을 해제합니까?
어떤 도움이나 조언도 매우 감사 할 것입니다.
'attachEnqueue'는 항상 목록의 헤드를 업데이트합니다. 설명 된 논리를 구현하려고 시도하지 않았습니다. 'user_id' 필드를 유효하지 않은/사용되지 않은 값으로 초기화하거나 별도의 "첨부되지 않은"플래그 필드를 사용해야합니다. 그런 다음'attachEnqueue'는 그러한 값을 찾을 때까지 목록을 탐색해야합니다. – kaylum