Hi,
I have a question about POSIX threads programming regarding
the program below. Basically, I have 3 threads that are doing nothing
but printing a message and a 4th thread which continuously loop until
all the other 3 threads terminate. To check that the other 3 threads
terminate properly, the 4th thread make calls to
pthread_getschedparam(). I tested the code on a machine running FreeBSD
5.3 and everything works fine. However, the behavior of the program is
completely different on machines running Linux or Cygwin. The problem
on those machines is that pthread_getschedparam() does not return the
proper error code (ESRCH) even when the thread id that was passed to it
is no longer running. I did notice however, that when I moved the code
for the 4th process into main(), everything works fine, which seems to
suggest that only parent process can access their child threads'
scheduling state. So, my questions are:
1) In the POSIX thread
implementation on Linux/Cygwin, can threads running within the same
process access the scheduling state of their peers via
pthread_getschedparam()?
2) Am I just doing something stupid by calling pthread_getschedparam() within my 4th thread ?
Below is the code in question:
#include
#include
#include
#include
#define NUM_RECEIVERS 3
pthread_t rxThread[NUM_RECEIVERS];
pthread_t computeThread ;
void *rxThreadFunc(void *arg)
{
int *i ;
i = (int *)arg ;
printf("Inside Thread %d\n", *i);
return NULL ;
}
void *computeThreadFunc(void *arg)
{
int p[NUM_RECEIVERS] ;
int k;
int flag ;
struct sched_param s[NUM_RECEIVERS] ;
while(1)
{
flag = 1 ;
for( k=0; k < NUM_RECEIVERS; k++ )
{
if( pthread_getschedparam(rxThread[k], &(p[k]), &(s[k])) == ESRCH )
continue ;
else
{
printf("Policy = %d\n", p[k]);
flag = 0 ;
break ;
}
}
if( flag == 1 )
{
printf("ALL THREADS HAVE STOPPED\n") ;
break ;
}
else
{
printf("SOME THREADS ARE STILL RUNNING\n");
}
}
return NULL ;
}
int main()
{
int j, k;
int p ;
struct sched_param s ;
for( j=0; j < NUM_RECEIVERS; j++ )
{
if( pthread_create(&(rxThread[j]), NULL, rxThreadFunc, &j) != 0 )
{
printf("Thread #%d was not created\n", j);
for( k=0; k < j; k++ )
pthread_cancel(rxThread[j]);
}
pthread_join(rxThread[j], NULL) ;
}
if( pthread_create(&computeThread, NULL, computeThreadFunc, NULL) != 0 )
{
printf("computeThread not created :-(\n");
for( k=0; k < NUM_RECEIVERS; k++ )
pthread_cancel(rxThread[j]);
}
pthread_join(computeThread, NULL);
return 0 ;
}
____________________________________________________________________________________
Get the free Yahoo! toolbar and rest assured with the added security of spyware protection.
http://new.toolbar.yahoo.com/toolbar/features/norton/index.php
---------------------------------------------------------------------
To unsubscribe, e-mail: opensuse-programming+unsubscribe@opensuse.org
For additional commands, e-mail: opensuse-programming+help@opensuse.org