views:

72

answers:

2

Hello, I am writing a program that is solving the producer/consumer problem, specifically the bounded-buffer version(i believe they mean the same thing). The producer will be generating x number of random numbers, where x is a command line parameter to my program. At the current moment, I believe my program is entering an infinite loop, but I'm not sure why it is occurring. I believe I am executing the semaphores correctly.

A: 
 sem_init(&empty,0,BUFF_SIZE);
 sem_init(&full,0,0);

Try initializing the "mutex" semaphore here as well.

shagv
Alright, I made that edit. No luck.
ray2k
You initialized it to 1 right? Not zero?
torak
sem_init(this is what torak is talking about. If you initialize it to zero then when "Producer" tries to acquire the semaphore it gets blocked (the "infinite loop" behavior that you saw).
shagv
A: 

Initialise the mutex to 1. Try commenting out the calls to sleep (i don't have an explanation, except "it works for me"). Edit : sleep takes a number of seconds, it may look blocked because the random number is too high.

Call srand in your main (otherwise you'll get identical values, as you seed again with the same time).

Leiaz
It seems to be working now, thanks everyone! The sleep was way too high I believe. When my assignment said to sleep between 1 and 10 seconds.
ray2k