1
#include <stdio.h>
#include <pthread.h>
#include <time.h>
#include <stdlib.h>

typedef struct pr_struct{
    int owner;
    int burst_time;
    struct pr_struct *next_prcmd;
} prcmd_t;

static prcmd_t *pr_head = NULL;
static prcmd_t *pr_tail = NULL;
static int pending_request = 0;
static pthread_mutex_t prmutex = PTHREAD_MUTEX_INITIALIZER;


int add_queue(prcmd_t *node)
{       
    pthread_mutex_lock(&prmutex);
    //code
    prcmd_t *curNode = pr_head;
    if(pr_head == NULL) { pr_head = node; return;}
    while(curNode->next_prcmd)
    {
         curNode->next_prcmd = (prcmd_t*)malloc(sizeof(prcmd_t));   
         curNode = curNode->next_prcmd;
    }
    curNode->next_prcmd = node;

    //
    pending_request++;
    pthread_mutex_unlock(&prmutex);
    return(0);
}



int main()
{
    if (pr_head == NULL)
    {
        printf("List is empty!\n");
    }

    prcmd_t *pr1;
    pr1->owner = 1;
    pr1->burst_time = 10;
    add_queue(pr1);
    prcmd_t *curNode = pr_head;
    while(curNode->next_prcmd)
    {
        printf("%i\n", curNode->owner);
        curNode = curNode->next_prcmd;
    }
}

EDIT:

Here's what I have now...

int main()
{


prcmd_t *pr1;
pr1 = (prcmd_t*)malloc(sizeof(prcmd_t));
pr1->owner = 1;
pr1->burst_time = 10;



if (pr_head == NULL)
{

    printf("List is empty!\n");
}

add_queue(pr1);


prcmd_t *curNode = pr_head;

printf("made it here 1\n");
while(curNode->next_prcmd)
{
    printf("in the while loop\n");

    printf("%i\n", curNode->owner);
    curNode = curNode->next_prcmd;
}
}

output is: List is empty! made it here 1

Pulseczar
  • 67
  • 6

2 Answers2

4

pr1 is an uninitialized pointer to prcmd_t struct, dereferencing uninitialized pointer causes undefined behavior.

You need to allocate space for the structure on the heap / stack (depending on where it's gonna be used), so one option is:

// Allocate on stack
prcmd_t pr1;
pr1.owner = 1;
pr1.burst_time = 10;
add_queue(&pr1);

and second is:

//Allocae on heap
prcmd_t *pr1;
pr = (prcmd_t*)malloc(sizeof(prcmd_t));
pr1->owner = 1;
pr1->burst_time = 10;
add_queue(pr1);

Modifying your main method (and main only) to:

int main()
{
    if (pr_head == NULL)
    {
        printf("List is empty!\n");
    }

    prcmd_t *pr1;   
    pr1 = (prcmd_t*)malloc(sizeof(prcmd_t));
    pr1->owner = 1;
    pr1->burst_time = 10;
    add_queue(pr1);
    prcmd_t *curNode = pr_head;
    while(curNode && curNode->owner)
    {
        printf("%i\n", curNode->owner);
        curNode = curNode->next_prcmd;
    }
}

Outputs

List is empty!
1
MByD
  • 135,866
  • 28
  • 264
  • 277
  • So you have another place with the same problem. look at the add_queue function as well. – MByD May 06 '11 at 04:27
  • I think you mixed the two options I gave you. Since `add_queue` expects to a pointer to `prcmd_t` (`prcmd_t *`), if you declare `pr1` as `prcmd_t`, then you need to pass its pointer `(&pr1)` to `add_queue`, but if you declare it as `prcmd_t *` then it is already a pointer to `prcmd_t` and you don't need to pass its pointer, but itself (`pr`) to add_queue. – MByD May 06 '11 at 04:47
  • list is still not printing int main() { prcmd_t *pr1; pr1 = (prcmd_t*)malloc(sizeof(prcmd_t)); pr1->owner = 1; pr1->burst_time = 10; add_queue(pr1); if (pr_head == NULL) { printf("List is empty!\n"); } prcmd_t *curNode = pr_head; printf("made it here 1\n"); while(curNode->next_prcmd) { printf("in the while loop\n"); printf("%i\n", curNode->owner); curNode = curNode->next_prcmd; } } – Pulseczar May 06 '11 at 04:51
  • Again, you declared `pr1` as struct, but than malloc to it for no reason and treat it like a pointer all along. – MByD May 06 '11 at 04:57
  • @Pulseczar - by editing your answer, but please add it at the bottom, and mention that it's an edit – MByD May 06 '11 at 04:58
  • I'm so confused. I thought I needed to allocate it. When I comment out that line, seg fault comes back. – Pulseczar May 06 '11 at 04:58
  • @MByD I made an edit. Is there an easy way to format code or add the 4 spaces in front? Very annoying to have to manually add 4 space to each line. – Pulseczar May 06 '11 at 05:04
  • @Pulseczar, first, see my edited answer, second - yes, mark the code that you want to format and press `ctrl+k` or click the brackets in the editing bar. – MByD May 06 '11 at 05:06
  • @MByD Thanks so much. You got me on the right track now. I'll sure I'll be back for more but thank you so much. – Pulseczar May 06 '11 at 05:12
  • @Pulseczar - no problem, if this answer satisfied you, please mark it as accepted by checking the tick to the left of it. – MByD May 06 '11 at 05:14
2

It's hard to tell without you tellung us where...

But

  • you have to initialize node->next_prcmd to null
  • why do you malloc in the while loop? you are thereby destroing your current->next, which in the next iteration is pretty bad...

Mario

Mario The Spoon
  • 4,799
  • 1
  • 24
  • 36