Hi There,
I have Potato 2.2.19 kernel and the following packages installed
libc6-dev 2.2.5-4,
libpth-dev 1.4.1-2 and glibc-doc 2.2.5-4. I was trying to understand the
concepts of threads on linux. I executed a program from Stevens book and is
attached to this mail. On the commandline I passed the following arguments.
The first argument being the executable , then no of items
to be processed , no of threads that are used to process these items.
Following is the output. From the this output , It seems to me that only one
thread is performing and rest of them are not allowed to run. The
Set_concurrency() method uses pthread_setconcurrency() internally, so I
donot where the problem is and I am linking the obj with pthread library.
~/unpv22e/mutex$ ./prodcons1 100 10
In void Set_concurrency(int level)
count[0] = 100
count[1] = 0
count[2] = 0
count[3] = 0
count[4] = 0
count[5] = 0
count[6] = 0
count[7] = 0
count[8] = 0
count[9] = 0
Can anybody suggest me where the problem is?
_________________________________________________________________
Get your FREE download of MSN Explorer at http://explorer.msn.com/intl.asp.
/* include main */
#include "unpipc.h"
#define MAXNITEMS 1000000
#define MAXNTHREADS 100
int nitems; /* read-only by producer and consumer */
struct {
pthread_mutex_t mutex;
int buff[MAXNITEMS];
int nput;
int nval;
} shared = { PTHREAD_MUTEX_INITIALIZER };
void *produce(void *), *consume(void *);
int
main(int argc, char **argv)
{
int i, nthreads, count[MAXNTHREADS];
pthread_t tid_produce[MAXNTHREADS], tid_consume;
if (argc != 3)
err_quit("usage: prodcons1 <#items> <#threads>");
nitems = min(atoi(argv[1]), MAXNITEMS);
nthreads = min(atoi(argv[2]), MAXNTHREADS);
Set_concurrency(nthreads);
for (i = 0; i < nthreads; i++) {
count[i] = 0;
Pthread_create(&tid_produce[i], NULL, produce, &count[i]);
}
for (i = 0; i < nthreads; i++) {
Pthread_join(tid_produce[i], NULL);
printf("count[%d] = %d\n", i, count[i]);
}
Pthread_create(&tid_consume, NULL, consume, NULL);
Pthread_join(tid_consume, NULL);
exit(0);
}
/* end main */
/* include produce */
void *
produce(void *arg)
{
for ( ; ; ) {
if (shared.nput >= nitems) {
return(NULL); /* array is full, we're done */
}
shared.buff[shared.nput] = shared.nval;
shared.nput++;
shared.nval++;
*((int *) arg) += 1;
}
}
void *
consume(void *arg)
{
int i;
for (i = 0; i < nitems; i++) {
if (shared.buff[i] != i)
printf("buff[%d] = %d\n", i, shared.buff[i]);
}
return(NULL);
}
/* end produce */