SophiaPretty

(5)

$14/per page/Negotiable

About SophiaPretty

Levels Tought:
Elementary,Middle School,High School,College,University,PHD

Expertise:
Accounting,Algebra See all
Accounting,Algebra,Applied Sciences,Architecture and Design,Art & Design,Biology,Business & Finance,Calculus,Chemistry,Communications,Computer Science,Economics,Engineering,English,Environmental science,Essay writing Hide all
Teaching Since: Jul 2017
Last Sign in: 304 Weeks Ago, 4 Days Ago
Questions Answered: 15833
Tutorials Posted: 15827

Education

  • MBA,PHD, Juris Doctor
    Strayer,Devery,Harvard University
    Mar-1995 - Mar-2002

Experience

  • Manager Planning
    WalMart
    Mar-2001 - Feb-2009

Category > Computer Science Posted 30 Nov 2017 My Price 10.00

manage storage lockers from a terminal program (locker).

hi, can you please fix my code. thanks

 

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>
Lock Status: <locked|unlocked>
Owner: <owner number | unowned>

Output format for ATTACH:

Locker <id> Owned By <owner id>

Output format for DETACH:

Locker <id> Unowned

Output format for LOCK and UNLOCK:

Locker <id> <locked|unlocked>

Output format for CREATE:

New Locker Created: <id>

Output format for DELETE:

Locker <id> Removed

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:

Cannot Build Locker

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

Locker Does Not Exist

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

No Lockers Available

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:

CREATE
New Locker Created: 1

Example 2 Query Locker 1:

CREATE
New Locker Created: 1

QUERY 1
Locker ID: 1
Lock Status: locked
Owner: unowned

Example 3 Unlock/Lock:

CREATE
New Locker Created: 1

QUERY 1
Locker ID: 1
Lock Status: locked
Owner: unowned

UNLOCK 1
Locker 1 Unlocked

QUERY 1
Locker ID: 1
Lock Status: unlocked
Owner: unowned

LOCK 1
Locker 1 Locked

QUERY 1
Locker ID: 1
Lock Status: locked
Owner: unowned

Example 4 Attach:

CREATE
New Locker Created: 1

CREATE
New Locker Created: 2

ATTACH 67
Locker 1 Owned By 67

ATTACH 20
Locker 2 Owned By 20

Example 5 DELETE:

CREATE
New Locker Created: 1

DELETE 1
Locker 1 Removed

Example 6 QUERALL example:

CREATE
New Locker Created: 1

CREATE
New Locker Created: 2

CREATE
New Locker Created: 3

QUERYALL
Locker ID: 1
Lock Status: locked
Owner: unowned

Locker ID: 2
Lock Status: locked
Owner: unowned

Locker ID: 3
Lock Status: locked
Owner: unowned#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("%d", &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("\n 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.\n");
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("\nQueue is Empty.\n");
}
return a;
}

void printList(listq Q)
{
node v = Q->head;
while(v != NULL){
printf("Locker: %d\n", 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[^\n]s", input);
sscanf(input, "%s %d", &command, &val1);
if(strcmp(command, "CREATE") == 0)
{
enqueue(Q,temp);
printf("New Locker created: %d\n", temp);
temp++;
}
else if(strcmp(command, "DELETE") == 0)
{
printf("Deleted the locker, %d\n",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!\n");
exit(0);
}
else if(strcmp(command, "LOCK") == 0)
{
Lock(Q);

}
else if(strcmp(command, "UNLOCK") == 0)
{
Unlock(Q);
}
}
}
#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("%d", &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("\n 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.\n");
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("\nQueue is Empty.\n");
}
return a;
}

void printList(listq Q)
{
node v = Q->head;
while(v != NULL){
printf("Locker: %d\n", 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[^\n]s", input);
sscanf(input, "%s %d", &command, &val1);
if(strcmp(command, "CREATE") == 0)
{
enqueue(Q,temp);
printf("New Locker created: %d\n", temp);
temp++;
}
else if(strcmp(command, "DELETE") == 0)
{
printf("Deleted the locker, %d\n",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!\n");
exit(0);
}
else if(strcmp(command, "LOCK") == 0)
{
Lock(Q);

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

Answers

(5)
Status NEW Posted 30 Nov 2017 11:11 AM My Price 10.00

-----------  ----------- H-----------ell-----------o S-----------ir/-----------Mad-----------am ----------- Th-----------ank----------- yo-----------u f-----------or -----------you-----------r i-----------nte-----------res-----------t a-----------nd -----------buy-----------ing----------- my----------- po-----------ste-----------d s-----------olu-----------tio-----------n. -----------Ple-----------ase----------- pi-----------ng -----------me -----------on -----------cha-----------t I----------- am----------- on-----------lin-----------e o-----------r i-----------nbo-----------x m-----------e a----------- me-----------ssa-----------ge -----------I w-----------ill----------- be----------- qu-----------ick-----------ly

Not Rated(0)