简体   繁体   中英

sem_wait() in producer and consumer

This is my code for producer and consumer problem. It can be compiled, but when I run the program it print nothing. I test the program and found the problem maybe about sem_wait(). Why the program could be compiled but the output is wrong? Thank you.

int main(int argc, char *argv[]) {
    pthread_mutex_init(&mutex, NULL);
    empty = sem_open("/mysem", O_CREAT, 0644, BUFFER_SIZE);
    full = sem_open("/mysem", O_CREAT, 0644, 0);

    pthread_create(&pro_thread, NULL, producer, NULL);
    pthread_create(&con_thread, NULL, consumer, NULL);

    pthread_exit(NULL);

    return 0;
}

Is the sem_open() function right? I commented the following line and the program could run...maybe the problem is about sem_open?

empty = sem_open("/mysem", O_CREAT, 0644, BUFFER_SIZE);

It seems that your producer never produces anything

Check the semaphore names. May be empty and full are the same instance as you're using the same name for both in your sem_open function

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM