/* include main */
int
main( int argc, char **argv)
{
int i, prodcount[MAXNTHREADS], conscount[MAXNTHREADS];
pthread_t tid_produce[MAXNTHREADS], tid_consume[MAXNTHREADS];
if (argc != 4)
err_quit( "usage: prodcons4 <#items> <#producers> <#consumers>" );
nitems = atoi (argv[1]);
nproducers = min( atoi (argv[2]), MAXNTHREADS);
nconsumers = min( atoi (argv[3]), MAXNTHREADS);
/* 4initialize three semaphores */
Sem_init(&shared.mutex, 0, 1);
Sem_init(&shared.nempty, 0, NBUFF);
Sem_init(&shared.nstored, 0, 0);
/* 4create all producers and all consumers */
Set_concurrency(nproducers + nconsumers);
for (i = 0; i < nproducers; i++)
{
prodcount[i] = 0;
Pthread_create(&tid_produce[i], NULL, produce, &prodcount[i]);
}
for (i = 0; i < nconsumers; i++)
{
conscount[i] = 0;
Pthread_create(&tid_consume[i], NULL, consume, &conscount[i]);
}
/* 4wait for all producers and all consumers */
for (i = 0; i < nproducers; i++)
{
Pthread_join(tid_produce[i], NULL);
printf ( "producer count[%d] = %d\n" , i, prodcount[i]);
}
for (i = 0; i < nconsumers; i++)
{
Pthread_join(tid_consume[i], NULL);
printf ( "consumer count[%d] = %d\n" , i, conscount[i]);
}
Sem_destroy(&shared.mutex);
Sem_destroy(&shared.nempty);
Sem_destroy(&shared.nstored);
exit (0);
}
/* end main */
|