I am attempting to read different values from standard input and store them in variables located in a structure I created called the process structure.Then I will take the info in this structure and store it in a pointer variable in a priority queue structure. This process structure looks as follows:
typedef struct proc{
int creationTime;
int pid;
int runTime;
int ioTime;
int repeat;
}proc;
The Structure for my Priority queue is as follows:
// type of one element in a queue
typedef struct _Queue_element {
void *info; //<- THIS IS WHERE ALL INFO FROM THE PROCESS STRUCTURE WILL BE STORED WHEN ADDED TO THE QUEUE
int priority;
struct _Queue_element *next;
} *Queue_element;
//basic queue type
typedef struct Queue {
Queue_element queue;
Queue_element tail;
Queue_element current;
Queue_element previous;
unsigned long queuelength;
unsigned int elementsize;
unsigned int duplicates;
int (*compare) (const void *e1, const void *e2);
pthread_mutex_t lock;
int priority_is_tag_only;
} Queue;
Lastly This is the add element to queue function in a readymade queue package I am using.
static void nolock_add_to_queue(Queue *q, void *element, int priority) {
Queue_element new_element, ptr, prev = NULL;
if (! q->duplicates && ! q->compare) {
fprintf(stderr, "If duplicates are disallowed, the comparison function must be specified in init_queue().\n");
exit(1);
}
if (!q->queue ||
(q->queue && (q->duplicates || !nolock_element_in_queue(q, element)))) {
new_element = (Queue_element) malloc(sizeof(struct _Queue_element));
if (new_element == NULL) {
fprintf(stderr, "malloc() failed in function add_to_queue()\n");
exit(1);
}
new_element->info = (void *)malloc(q->elementsize);
if (new_element->info == NULL) {
fprintf(stderr, "malloc() failed in function add_to_queue()\n");
exit(1);
}
memcpy(new_element->info, element, q->elementsize);
new_element->priority = priority;
(q->queuelength)++;
if (q->queue == NULL) { // first element
new_element->next = NULL;
q->queue = new_element;
q->tail = new_element;
}
else if (q->priority_is_tag_only) { // FIFO queue
q->tail->next = new_element;
q->tail = new_element;
new_element->next = NULL;
}
else { // priority queue
ptr=q->queue;
while (ptr != NULL && priority <= ptr->priority) {
prev = ptr;
ptr = ptr->next;
}
if (! prev) { // queue only had one element and new element
// has higher priority
new_element->next=q->queue;
q->tail = q->queue;
q->queue=new_element;
}
else { // insert new element
new_element->next = prev->next;
prev->next = new_element;
if (new_element->next == NULL) {
// new tail
q->tail = new_element;
}
}
}
nolock_rewind_queue(q);
}
}
What I am confused about Is how this add to queue function will correctly store the info from typedef struct proc;
to the void *info
variable in typedef struct _Queue_element;
. Please let me know if this question is lacking any information such as other functions Included in my package or any other points I may clarify. Even if you are unable to provide specific solutions to my problem any materials I may read up on that could help me understand these topics will certainly be useful.
UPDATE: I have realized that this function does not need to be changed whatsoever. But am still confused as to how it will receive the info from proc.