生成单链表冻结的程序

时间:2013-11-27 00:20:50

标签: c pointers

我有一个程序,除了其他东西需要生成一个单链表...但是我创建列表后程序冻结(它没有给出分段错误错误)...我试图找出什么问题是如此,我评论了生成列表但程序一直冻结的for循环...

之后我还评论了代码行,我将内存分配给列表的头部(这是一个全局指针)程序工作,所以它让我相信内存分配的问题是错误的列表(我仍然无法弄清楚什么是错的)

有人能解释一下这个问题吗?

    #include <semaphore.h>
#include <pthread.h>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>

struct elem{
    int value;
    int worker;
    struct elem* next;
} ;

typedef struct elem Element;

Element* head;//the head of the list
pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER;//mutex for protecting the global counter variable

pthread_cond_t condition_var = PTHREAD_COND_INITIALIZER;

pthread_rwlock_t rwlock_var;

sem_t semafor;


int counter;// the variable which wll count the nber of elements with the value less than 2`

int nrElements(Element* ptr);
void* thread_function(void*);


int main( int argc, char** argv)
{

    int nrEl,nrW; //nrEL- nr el of list, nrW - nr of workers
    int i;

    Element *tmp, *prev;
    pthread_t tid[ atoi(argv[2]) ];
    int orderNbers[atoi(argv[2])];//the order number for the threads

    printf("This is the main thread with thread no %u\n",pthread_self());
    /*create the read/write lock with default attributes(NULL) */
    pthread_rwlock_init(&rwlock_var,NULL);

    sem_init(&semafor,0,3);  //initialize the semaphore
    nrEl = atoi(argv[1]);
    nrW = atoi(argv[2]);


    /* we now populate the worker field  */
    for (i=0; i<= nrW -1 ; i++) {
        orderNbers[i] = i;
    }



    /*allocate memory for the head(1st el of the list) */
    head  = (Element*) malloc(sizeof(Element));
    head->next = NULL;  
    /* generate the list with nrEl and
       assign random values( between 0-30) to its
       value field
    */
    tmp = head;

    for (i = 0; i <= nrEl -1 ; i++)
    {

        tmp->value = rand() % 30;
        /*at first we assign each element to a diff thread
            when the number of el > nr of threads
            we assign each element to the first threads     */
        if (i > nrW - 1){

            tmp->worker = i-nrW;
        }   
        else
        {
            tmp->worker = i;
        }


        printf("\nInitialize element number %d with value %d and worker %d",i,tmp->value,tmp->worker);
        /* if we reach the last element of the list
            we don't allocate memory for the next element
            so we assign it to NULL
            tmp , will now point to the last element of the list
        */
        if (i == nrEl -1 ){

            tmp->next = NULL;
            printf("\n%d\n ",i);
            break;

        }
        else {

        tmp->next  = (Element*) malloc(sizeof(Element) );
        tmp  = tmp->next;
        printf("\n%d\n",i);
        }
    }

    /* this is the part that doesnt execute anymore after the list creation*/    
    printf("\nCreating the threads");
    for (i=0; i<nrW ; i++)
    {
        pthread_create(&tid[i],NULL,thread_function,&orderNbers[i]);
    }
/* .... */
}

编辑:我已经编辑了完整的问题,因为看起来列表是正确生成的

0 个答案:

没有答案