Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Can some experts please fix my code!!?!? please.. I am constructing codes by fol

ID: 3842102 • Letter: C

Question

Can some experts please fix my code!!?!? please..

I am constructing codes by following the below question using Queue singly linked list!!

You are tasked with writing a program that will manage storage lockers from a terminal program (locker). Each locker is represented as a process in the system. Signals can be sent to the process to change the state of the locker outside of the management program.

A locker process is made up of:

•Process ID (pid) : (unique to process creation)

•Locker ID : Creation order, Unsigned integer, starts at 1

•User Id : Unsigned integer

•Locked/Unlocked state

•Owned/Free state

•Two signal handlers (SIGUSR1 and SIGUSR2)

A locker can be in 4 states, {Locked, Free}, {Locked, Owned}, {Unlocked, Free} and {Unlocked, Owned} states. Free lockers are kept in a queue for reuse. Your program should also report on how many processes are unlocked and free so your management program can prevent them from being vandalized.

Each locker needs to handle a SIGUSR1 and SIGUSR2 signals. SIGUSR1 locks the locker and SIGUSR2 unlocks the locker.

When a locker is damaged or needs to be repaired/recycled your program will receive a request to decomission a locker (DELETE). You should not utilise SIGTERM to terminate a locker. This should be part of your protocol to decomission a locker.

Please note! If heap data has been allocated then newly spawned lockers should immediately free this upon execution.

The list of commands and arguments your program can receive:
CREATE - Creates a locker
DELETE <id : locker id> - Decommissions a locker
QUERY <id : locker id> - Queries a locker and retrieves information
QUERYALL - Queries all lockers and retrieve their information
LOCK <id : locker id> - Locks a locker
UNLOCK <id : locker id> - Unlocks a locker
ATTACH <owner> - Adds an owner to a locker, gets locker at head of the queue
DETACH <id : locker id> - Removes an owner from a locker
QUIT - Deletes all lockers and quits the program

The default states of a locker when created:

•Locked

•No Owner (Detached)

When the management process queries a locker it will print out information in this form (QUERY or QUERYALL):

Locker ID: <id>

Output format for ATTACH:

Output format for DETACH:

Output format for LOCK and UNLOCK:

Output format for CREATE:

Output format for DELETE:

Important:

You will have to define a simple communication protocol to communicate between these prcesses. This can be text messages over the pipe and parsing them.

In the event that you cannot create a new locker, your program needs to output:

In the event that a command utilises a locker id that does not exist, your program needs to output:

If Attach is called and no lockers are unowned your program needs to output:

Recommended functions to use: pipe(), kill(), signal()/sigaction, fork(), wait()/waitpid() and dynamic memory functions. Potentially bitfields could be useful for the protocol.

Assumptions/Clarifications:

•If you attempt to lock a locked locker or unlock an unlocked locker, you will just need to relay the status regardless. You do not have to output that the operation did nothing.

•Owner id's are just inputted by your application and can be completely arbitrary but you can assume the maximum owner id is 255.

•When a DELETE operation occurs, unless the locker does not exist, it should remove the locker, regardless if it is owned by someone.

•QUIT should clean up and terminate all locker processes and quit without any output.

•You will need to ensure that you can create two way communication between processes.

•When executing query all, ordering is based on locker id order (Ascending)

Example:

Example 1 Create Locker:

Example 2 Query Locker 1:

Example 3 Unlock/Lock:

Example 4 Attach:

Example 5 DELETE:

Example 6 QUERALL example:

#include<stdlib.h>
#include<stdio.h>
#include<pthread.h>
#include <string.h>

typedef struct node_elm* node;
struct node_elm {
int item;
int id;
int owner;
node next;
pthread_mutex_t lock;
};

typedef struct list_queue* listq;
struct list_queue {
node head, tail;
int size;
};

void findnode(listq Q, int node_id);

listq queueInit(void){
listq Q = (listq) malloc( sizeof(struct list_queue) );
Q->head = Q->tail = NULL;
Q->size = 0;
return Q;
}

node insertAtHead(node head, int item){
node new_node = (node)malloc( sizeof(struct node_elm));
new_node->item = item;
new_node->next=head;

return new_node;
}

void enqueue(listq Q,int item) {

Q->head= insertAtHead(Q->head, item);
}

void enqueue1(listq Q,int id)
{

Q->head->id = id;
}

void attachowner (listq Q, int owner)
{
   int node_id;
printf ("Enter the node to attach owner--");

scanf("%s", &node_id);

findnode(Q, node_id);
}

void findnode(listq Q, int node_id)
{
   node prev = Q->head;

while(prev->next != NULL)

prev = prev->next;

//attach the owner to a particular node ID

if(prev->next == Q->head)
{
Q->head->owner = node_id;
}
// Check if node really exists in Linked List

if(prev->next == NULL)
{
printf(" Given node is not present in Linked List");
return;
}
}

void Lock(listq Q)
{
pthread_mutex_lock(&Q->head->id);
}

void Unlock (listq Q)
{
pthread_mutex_unlock(&Q->head->id);
}

node deleteFromTail(node head, int *item){
node tail, tmp = NULL;
if (head == NULL){
printf("List is empty. ");
return NULL;
}
for (tail=head; tail->next != NULL; tail=tail->next)
tmp=tail;
if (tmp!=NULL) tmp->next=NULL;
*item=tail->item;
free(tail);
if(head ==tail)
return NULL;
else return head;
}

int dequeue(listq Q)
{
node temp = (node)malloc(sizeof(struct node_elm));
int a = Q->head->item;
temp=deleteFromTail(Q->head,&a);
if(temp==NULL)
{
printf(" Queue is Empty. ");
}
return a;
}

void printList(listq Q)
{
node v = Q->head;
while(v != NULL){
printf("Locker: %d ", v->item);
v = v->next;
}
}

int main()
{
int temp = 1;
listq Q;
node head = NULL;

Q = queueInit();

char input[50];
char command[20];

int val1;
int count = 0;
  
while(1)
{
scanf(" %49[^ ]s", input);
sscanf(input, "%s %d", &command, &val1);

if(strcmp(command, "CREATE") == 0)
{
enqueue(Q,temp);
printf("New Locker created: %d ", temp);
temp++;
} else if(strcmp(command, "DELETE") == 0)
{
printf("Deleted the locker, %d ",dequeue(Q));
} else if(strcmp(command, "ATTACH") == 0)
{

   attachowner(Q,val1);
printf("New owner created");


} else if(strcmp(command, "QUERYALL") == 0)
{
printList(Q);
  
} else if(strcmp(command, "QUIT") == 0)
{
printf("Good Bye! ");
exit(0);
}

else if(strcmp(command, "LOCK") == 0)
{
Lock(Q);
  
} else if(strcmp(command, "UNLOCK") == 0)
{
Unlock(Q);
}
}
}

Explanation / Answer

Here is the below program:

#include<stdlib.h>

#include<stdio.h>

#include<pthread.h>

#include <string.h>

typedef struct node_elm* node;

struct node_elm {

int item;

int id;

int owner;

node next;

pthread_mutex_t lock;

};

typedef struct list_queue* listq;

struct list_queue {

node head, tail;

int size;

};

void findnode(listq Q, int node_id);

listq queueInit(void){

listq Q = (listq) malloc( sizeof(struct list_queue) );

Q->head = Q->tail = NULL;

Q->size = 0;

return Q;

}

node insertAtHead(node head, int item){

node new_node = (node)malloc( sizeof(struct node_elm));

new_node->item = item;

new_node->next=head;

return new_node;

}

void enqueue(listq Q,int item) {

Q->head= insertAtHead(Q->head, item);

}

void enqueue1(listq Q,int id)

{

Q->head->id = id;

}

void attachowner (listq Q, int owner)

{

int node_id;

printf ("Enter the node to attach owner--");

scanf("%s", &node_id);

findnode(Q, node_id);

}

void findnode(listq Q, int node_id)

{

node prev = Q->head;

while(prev->next != NULL)

prev = prev->next;

if(prev->next == Q->head)

{

Q->head->owner = node_id;

}

if(prev->next == NULL)

{

printf(" Given node is not present in Linked List");

return;

}

}

void Lock(listq Q)

{

pthread_mutex_lock(&Q->head->id);

}

void Unlock (listq Q)

{

pthread_mutex_unlock(&Q->head->id);

}

node deleteFromTail(node head, int *item){

node tail, tmp = NULL;

if (head == NULL){

printf("List is empty. ");

return NULL;

}

for (tail=head; tail->next != NULL; tail=tail->next)

tmp=tail;

if (tmp!=NULL) tmp->next=NULL;

*item=tail->item;

free(tail);

if(head ==tail)

return NULL;

else return head;

}

int dequeue(listq Q)

{

node temp = (node)malloc(sizeof(struct node_elm));

int a = Q->head->item;

temp=deleteFromTail(Q->head,&a);

if(temp==NULL)

{

printf(" Queue is Empty. ");

}

return a;

}

void printList(listq Q)

{

node v = Q->head;

while(v != NULL){

printf("Locker: %d ", v->item);

v = v->next;

}

}

int main()

{

int temp = 1;

listq Q;

node head = NULL;

Q = queueInit();

char input[50];

char command[20];

int val1;

int count = 0;

while(1)

{

scanf(" %49[^ ]s", input);

sscanf(input, "%s %d", &command, &val1);

if(strcmp(command, "CREATE") == 0)

{

enqueue(Q,temp);

printf("New Locker created: %d ", temp);

temp++;

} else if(strcmp(command, "DELETE") == 0)

{

printf("Deleted the locker, %d ",dequeue(Q));

} else if(strcmp(command, "ATTACH") == 0)

{

attachowner(Q,val1);

printf("New owner created");

} else if(strcmp(command, "QUERYALL") == 0)

{

printList(Q);

} else if(strcmp(command, "QUIT") == 0)

{

printf("Good Bye! ");

exit(0);

}

else if(strcmp(command, "LOCK") == 0)

{

Lock(Q);

   } else if(strcmp(command, "UNLOCK") == 0)

   {

       Unlock(Q);

   }

}

}