Operating System - HP-UX
1834450 Members
2557 Online
110067 Solutions
New Discussion

Re: need immediate help about semaphore synchronization.

 

need immediate help about semaphore synchronization.

I can't solve the problem in this producer consumer problem for printer spooling.

void *printer(void *args) //producer
{
int holder_1,i;

while (general_counter < n)
{
sem_wait(&queues);

sem_wait(&mutex);
holder_1=dequeue(&q);
dequeue_counter++;
sem_post(&mutex);

printf("Job(%d) starts printing with %dK. (%.3f seconds left.)\n", dequeue_counter, holder_1, (float)holder_1*0.01);

for(i=0; i {
usleep(10000);
time_complete=time_complete-0.01;
sem_post(&full);
}

printf("Job(%d) completed printing.\n", dequeue_counter);

sem_wait(&mutex);
general_counter++;
sem_post(&mutex);

}

return NULL;
}



void *consumer_job(void *args) //consumer
{
int consume_value, i;

consume_value=rand()%1024 + 50;

printf("A new job arrived with %dKb.\n", consume_value);

for(i=0; i sem_wait(&full);

time_complete=time_complete + (float)(consume_value * 0.01);

sem_wait(&mutex);
enqueue(&q, consume_value);
enqueue_counter++;
printf("Job(%d) with %dKb enqued. %.3f seconds left to start.\n", enqueue_counter, consume_value, time_complete-(float)(consume_value * 0.01));
sem_post(&queues);
sem_post(&mutex);

return NULL;
}


Problem is that when the queue is empty and and the FULL semaphore value is 0, it enters to a deadlock. I can't think anything. :(

If anyone has any idea about how I can fix it, I would be appreciate...
2 REPLIES 2

Re: need immediate help about semaphore synchronization.

semaphore FULL is initialized to 2048. (source)
semaphore MUTEX is initialized to 1.
semaphore QUEUE is initialized to 0.
Dennis Handly
Acclaimed Contributor

Re: need immediate help about semaphore synchronization.

Should be be doing a post of queues before the post of mutex?

You also seem to be doing a random number of wait on full, is this passed to the producer to do that number of posts?

(Any reason you are casting to float instead of double?)