CSjunkie
CSjunkie

Reputation: 555

Managing a linked list with pthreads

I am attempting to simulate a bank with a line of customers represented by a linked list with a thread calculating the probability that a customer walks in (with a preset probability) every unit of time and adds the customer to the end of the line while several teller threads remove a customer from the beginning of the line and I keep getting segmentation faults at seemingly unpredictable points during each simulation. Below is the code I use for adding too and removing from the list. The error seems to occur because the customer thread is attempting to add while a teller is removing.

void addToQ(int x, void *arg){
    printf("ADD ATTEMPT\n");
    struct shiftInfo *info = (struct shiftInfo *) arg;
    if (head -> ID == -1){ //No clients in line
        head -> ID = x;
        head -> next = dummy;
        tail = head;
    }

    else{ //clients already in line
        tail -> next = (NODE *)malloc(sizeof(NODE));
        tail = tail -> next;
        tail -> ID = x;
        tail -> next = dummy;
        info -> inQ++;
    }
    printf("ADD SUCCESS\n");
}
//Removes node closest to root
//Assumes: root != end
void removeFromQ(void *arg){
    printf("REMOVE ATTEMPT\n");
    struct shiftInfo *info = (struct shiftInfo *) arg;
    if (head -> next == dummy){ //only one element
        printf("SCENARIO 1\n");
        head -> ID = -1;
    }
    else{
        printf("SCENARIO 2\n");
        curr = head -> next;
        head = curr -> next;
        printf("Halfway\n");
        if ((head -> next == NULL) || (head -> next == dummy))
            tail = head;
    }
    info -> inQ--;
    printf("REMOVE SUCCESS\n");
}

Here are the thread functions:

void *customerT(void *arg){
    int custID = 100;
    float newCust = 0;
    struct shiftInfo *info = (struct shiftInfo *) arg;
    float aRate = info -> arrivalRate;
    pthread_mutex_lock (&start); //Ensures Customers don't start before Tellers get to work
    pthread_mutex_unlock (&start);
    while(0<info -> simTime){
        newCust = (double)rand() / (double)RAND_MAX;
        if (newCust <= aRate){
            pthread_mutex_lock (&start);
            addToQ(custID,(void *)info);
            pthread_mutex_unlock (&start);
            custID++;
            }
        sleep(1/60);
    }
    pthread_exit(NULL);
 }


void *teller(void *arg){
    struct shiftInfo *info = (struct shiftInfo *) arg;
    int clientID;
    int clients = info -> inQ;
    pthread_mutex_lock (&start); //Ensures tellers dont start before customers arrive
    pthread_mutex_unlock (&start);
    while(0<info -> simTime){
        pthread_mutex_lock (&accessQ);
        clients = info -> inQ;
        if(head -> ID > 0){
            pthread_mutex_lock (&start);
            removeFromQ((void *)info);
            pthread_mutex_unlock (&start);
            printf("CLIENT OBTAINED\n");
        }
        printf("In Q %d\n", clients);
        pthread_mutex_unlock (&accessQ);
        sleep((info -> serviceTime)/60);
        }
    pthread_exit(NULL);
}

and in case it is relevant, here is the manager thread function:

//Creates Customer thread, Teller threads, and timer thread
void *mainThread(void *info){
    head = (NODE *)malloc(sizeof(NODE));
    head -> ID = -1;
    head -> next = dummy;
    tail = head;
    int status;
    struct shiftInfo *I = info; 
    pthread_mutex_init(&start, NULL);
    pthread_mutex_init(&removeID, NULL);
    pthread_mutex_init(&accessQ, NULL);
    pthread_mutex_lock (&start);

    printf("Scheduling Tellers\n");
    pthread_t threads[(I -> tellers)];
    for (int i = 0; i < (I -> tellers); i++){
        I -> threadID = i;
        status = pthread_create(&threads[i], NULL, teller, (void *)info);
        if (status){
            printf("ERROR CODE: %d\n", status);
            exit(-1);
        }
    }

    printf("Preparing Customers\n");
    pthread_t customer;
    status = pthread_create(&customer, NULL, customerT, (void *)info);
        if (status){
            printf("ERROR CODE: %d\n", status);
            exit(-1);
        }

    printf("Making Timer\n");
    pthread_t time;
        status = pthread_create(&time, NULL, timer, (void *)info);
        if (status){
            printf("ERROR CODE: %d\n", status);
            exit(-1);
        }

    pthread_mutex_unlock (&start);
}

Upvotes: 0

Views: 1717

Answers (1)

JS1
JS1

Reputation: 4767

This code right here looks wrong:

curr = head -> next;
head = curr -> next;

Consider the case where your list looks like: head -> node -> dummy. After running the above code, head will be pointing at dummy, which you surely do not want. Try changing the code to this:

curr = head;
head = curr -> next;

Edit: The reason you get negative customers is that in addToQ, you don't increment info->inQ when the list starts empty (the top half of your if statement). You need info->inQ++ always, just like you have info->inQ-- always in removeFromQueue.

Upvotes: 2

Related Questions