简体   繁体   English

生产者和消费者中的sem_wait()

[英]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(). 我测试程序,发现问题可能与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? sem_open()函数是对的吗? I commented the following line and the program could run...maybe the problem is about sem_open? 我评论了以下行,程序可以运行...也许问题是关于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 可能是emptyfull是相同的实例为你使用相同的名称都在你的sem_open功能

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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