Hi there,
i m working on dining philosopher problem , i wrote some codes but i keeps giving some error like :
"/tmp/ccMEjOtr.o: In function `philosopherRun':
betterDiningPhilosophers.c:(.text+0x7a): undefined reference to `pthread_mutex_trylock'
/tmp/ccMEjOtr.o: In function `main':
betterDiningPhilosophers.c:(.text+0x54d): undefined reference to `pthread_create'
collect2: ld returned 1 exit status
"
even i try to declare all undefine things it s same, WHAT AM I DOING WRONG HERE ?

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <pthread.h>
#include <stdbool.h>
#include <errno.h>

#define NUM_PHILOSOPHERS 5;

typedef struct philos {
    char *status;
    pthread_mutex_t *right, *left;
} Philosopher;

void philosopherSetup(Philosopher *philosopher, pthread_mutex_t *left, pthread_mutex_t *right) {
    philosopher->left = left;
    philosopher->right = right;
}

void *philosopherRun(void *phil) {
    Philosopher *philosopher = phil;
    while (true) {
        philosopher->status = "thinking";
        philosopher->status = "waiting";
        bool both = false;
        do {
            if (pthread_mutex_lock(philosopher->left) != 0)
                fprintf(stderr, "Left lock error.\n");
            int right_lock_result = pthread_mutex_trylock(philosopher->right);
            switch (right_lock_result) {
                case 0:
                    both = true;
                    break;
                case EBUSY:
                    pthread_mutex_unlock(philosopher->left); // try again
                    both = false;
                    break;
                default:
                    fprintf(stderr, "Right lock error\n.");
            }
        } while (!both);
        printf("."); // this won't appear if blocked
        philosopher->status = "eating";
        sleep(1);
        pthread_mutex_unlock(philosopher->left);
        pthread_mutex_unlock(philosopher->right);
    }
}

/*
 * Starts all of the philosopher threads and reports on their progress.
 */
int main(int argc, char** argv) {
    int number = NUM_PHILOSOPHERS;
    if (argc > 1) {
        number = atoi(argv[1]);
    }
    printf("Creating %d philosophers.\n", number);
    Philosopher philosophers[number];
    pthread_mutex_t forks[number];
    pthread_t threads[number];

    int i;
    for (i = 0; i < number; i++) { // create the locks
        if (pthread_mutex_init(&forks[i], NULL)) {
            fprintf(stderr, "Unable to create locks.\n");
            return (EXIT_FAILURE);
        }
    }
    for (i = 0; i < number; i++) { // setup the philosophers
        philosopherSetup(&philosophers[i], &forks[i], &forks[(i + 1) % number]);
    }

    for (i = 0; i < number; i++) { // create and start the threads
        pthread_create(&threads[number], NULL, philosopherRun, &philosophers[i]);
    }

    while (true) {
        sleep(1);
        for (i = 0; i < number; i++) {
            usleep(1000);
            printf("%d: %-8s ", i, philosophers[i].status);
        }
        printf("\n");
    }

    return (EXIT_SUCCESS);
}

i don t know still not working ,after update linux most of them don t work

what are you asking? you said you had compile errors. i'm saying you need to include the

-lpthread

switch to link the pthread library. if you follow my commandline instruction in my previous post, your program will compile and run successfully, without any compile or runtime errors.

now is it a successful solution to the classic resource problem? i don't know, you need to figure that part out.

Edited 6 Years Ago by jephthah: n/a

This article has been dead for over six months. Start a new discussion instead.