1

I am trying to implement a queue in xv6. In proc.c ,I have a structure named

struct pqueue_n
{
    struct proc *head;
    struct proc *tail;
    int lenght;
};

I have also create an array of structure

struct pqueue_n queue[MAX_LL];

The initialization I have looks like this

struct pqueue_n* queue = {0,0,0};

I have written two methods as well enqueue and dequeue void enqueue_n(void)

{
    struct proc *p = myproc();
    struct pqueue_n* pq;


    if(p->priority < MAX_PRIORITY || p->priority> MIN_PRIORITY)
    {
        return;
    }

    if (pq->tail == ((void *)0))
    {
        return;
    }
    if (pq->head == ((void *)0))
    {
        p->next = pq->head;
        pq->head = p;
        pq->tail = p;
    }
    else if(pq->head != ((void*)0))
    {
        if(pq->head->priority == p->priority)
        {
            p->next = pq->head;
            pq->head = p;
        }
    }
    pq->lenght++;
}
void dequeue_n(void)
{
    struct proc *p; 
    struct pqueue_n* pq;
    { 
        if(pq->head == ((void *)0))
        {
            return ;
        }
        p = pq->head;
        pq->head = pq->head->next ;
        p->next = 0;
    }
}

I am trying to initialize the structure as a globe structure.so that I can used the structure in my methods. Whenever I compile. I receive this error "excess elements in scalar initializer" and "‘pq’ is used uninitialized in this function ".

Cœur
  • 37,241
  • 25
  • 195
  • 267
  • is this truly what you mean: if(p->priority < MAX_PRIORITY || p->priority> MIN_PRIORITY) ... or do you mean: && instead of || ....? Also, in enqueue_n() "pq" is never initialized... so your pq->tail and pq->head tests are invalid. – TonyB Dec 11 '17 at 00:09
  • thanks for catching that. and yes I am trying to initialize it with the struct but I have to initialize that struct to 0 or NULL first.I can't seem to do that. I have tried everything – Kobina Tetteh Dadzie Dec 11 '17 at 00:19
  • you realize that "struct pqueue_n* pq;" in enqueue_n() is an automatic variable, which only exists during the call to enqueue_n()... and as soon as you return from it, its value is lost... so the next time enqueue_n() is called, it has to be initialized again... how will it be initialized??? You could pass in the address of a "struct pqueue_n " pointer, or you could call a function that returns a "struct pqueue_n " pointer... or you could have a global "struct pqueue_n" pointer and use it instead... – TonyB Dec 11 '17 at 00:28
  • how do I create a global struct ?? – Kobina Tetteh Dadzie Dec 11 '17 at 00:35
  • @Kobina Tetteh Dadzie queue have a front and rear , second thing you can define integer array inside structure , and allocate dynamic memory. After that in enqueue and deque function pass queue as parameter – Tushar Sharma Dec 11 '17 at 01:32

0 Answers0