简体   繁体   English

使用信号量编译但未运行的生产者-消费者代码

[英]A producer-consumer code using semaphore compiling but not running

i am in a very sticky situation as i am unable to figure out the problem with my producer-consumer problem using pthreads. 我处于非常棘手的情况,因为我无法使用pthread来解决生产者-消费者问题。 the code compiles but when i run the code nothing happens and the expected output does not show. 代码可以编译,但是当我运行代码时,没有任何反应,预期的输出也不会显示。 i believe there is a probelem in my producer and consumer threads with the if/else statement but i may be something else. 我相信如果使用if / else语句,我的生产者和消费者线程中会有一个探针,但是我可能还有其他事情。 the code is as shown below: 代码如下所示:

To compile it on command line: 要在命令行上编译它:

gcc -pthread -lrt semaphore.c -o semaphore  

Once you run the program it dosent display the correct output 一旦运行程序,它就会显示正确的输出

You might want to consider some sample output to go with it; 您可能需要考虑一些示例输出。 but I noticed that your producer and consumer threads start by: 但是我注意到您的生产者和消费者线程的开始是:

int rNum = rand() / RAND_DIVISOR;
sleep(rNum);

Simply removing this from both threads made your program work. 只需从两个线程中删除它就可以使程序正常工作。 On my edition of linux, RAND_MAX is 2^31, so these could cause an up to 21 second sleep in each thread. 在我的Linux版本上,RAND_MAX为2 ^ 31,因此它们可能导致每个线程最多休眠21秒。

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

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