Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdlib.h>
- #include "queue.h"
- #include <pthread.h>
- /* Remember to initilize the queue before using it */
- void initialize_queue(struct pqueue_t * q) {
- q->head = q->tail = NULL;
- pthread_mutex_init(&q->lock, NULL);
- }
- // PCB = process control block
- /* Return non-zero if the queue is empty */
- int empty(struct pqueue_t * q) {
- return (q->head == NULL);
- }
- /* Get PCB of a process from the queue (q).
- * Return NULL if the queue is empty */
- struct pcb_t * de_queue(struct pqueue_t * q) {
- struct pcb_t * proc = NULL;
- // TODO: return q->head->data and remember to update the queue's head
- // and tail if necessary. Remember to use 'lock' to avoid race
- // condition
- // YOUR CODE HERE
- if (q->head == NULL) return NULL;
- pthread_mutex_lock(&q->lock);
- proc = (struct pcb_t *) malloc(sizeof(struct pcb_t));
- proc->arrival_time = q->head->data->arrival_time;
- proc->burst_time = q->head->data->burst_time;
- proc->pid = q->head->data->pid;
- struct qitem_t* ptr = q->head;
- struct pcb_t* ptr_data = q->head->data;
- q->head = q->head->next;
- if (q->head == NULL) q->tail = NULL;
- free(ptr_data);
- free(ptr);
- pthread_mutex_unlock(&q->lock);
- return proc;
- }
- /* Put PCB of a process to the queue. */
- void en_queue(struct pqueue_t * q, struct pcb_t * proc) {
- // TODO: Update q->tail.
- // Remember to use 'lock' to avoid race condition
- // YOUR CODE HERE
- if (q->head == NULL)
- {
- pthread_mutex_lock(&q->lock);
- struct qitem_t *newNode = (struct qitem_t* )malloc(sizeof(struct qitem_t));
- newNode->data = proc;
- newNode->next = NULL;
- q->head = newNode;
- q->tail = newNode;
- pthread_mutex_unlock(&q->lock);
- return;
- }
- pthread_mutex_lock(&q->lock);
- struct qitem_t *newNode = (struct qitem_t*)malloc(sizeof(struct qitem_t));
- newNode->data = proc;
- newNode->next = NULL;
- q->tail->next = newNode;
- q->tail = newNode;
- pthread_mutex_unlock(&q->lock);
- return;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement