#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;
    }
}

编辑:
这是我现在拥有的。。。
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;
}
}

输出为:
列表为空!
在这里成功了1

最佳答案

pr1是指向prcmd_t struct的未初始化指针,取消对未初始化指针的引用将导致undefined behavior
您需要为堆/堆栈上的结构分配空间(取决于它将被使用的位置),因此一个选项是:

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

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

将main方法(仅限main)修改为:
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;
    }
}

输出
List is empty!
1

09-30 12:02