What is wrong with pthread_kill on Redhat Linux ???

  • Follow


Hi,
I have redhat 9.0 Linux installed on my system with gcc 3.2.3.
This is the program i used, i compiled it with -lpthread option.

Here i am starting 3 threads and then i am calling pthread_kill on a
thread which is non-existent , the signal being passed is '0'.
According to the POSIX standard if the thread doesn't exist it should
set errno to ESRCH. But instead it causes a segmentation fault.
Why is that ???
Is there any work around to this problem ???
Is this a linux problem or just Redhat 9.0 problem???

#include <stdio.h>  
#include <unistd.h>
#include <pthread.h>
#include<signal.h>
#include <errno.h>

void* do_loop(void* data)
{
    long int i;		
    int j;
    int me = *((int*)data);

    while (1) // infinite loop
	{
 		i++;
		if( i%1000000== 0)
		printf("thread %d - i val %d\n", me, i); 	
		if(i==225000000)
   			pthread_exit(NULL);
	}
    pthread_exit(NULL); // There is no point of this actually
}

int main()
{
    int        thr_id;
    pthread_t  id;
    int        a = 1;
    int        b = 2;
    int        c = 3;
    int        d = 4;

    if(pthread_create(&id, NULL, do_loop, (void*)&a) != 0)
	printf(" Thread creation Failure \n");

    if(pthread_create(&id, NULL, do_loop, (void*)&c) != 0)
	printf(" Thread creation Failure \n");

    if(pthread_create(&id, NULL, do_loop, (void*)&d) != 0)
	printf(" Thread creation Failure \n");

    if(pthread_kill((pthread_t)19820, 0) == ESRCH)    
	{
		printf("Main Thread:  Thread not found \n");
	}

	else
	{
		printf(" Thread Found !!!.... Killing\n");
		sleep(10);
		pthread_kill(id,SIGKILL);
	}
    return 0;
}


//P.S : This very same program works on Solaris!!!
0
Reply anoop_kn (5) 1/23/2004 11:39:18 AM

anoop_kn@yahoo.com (Anoop Kumar) writes:

> Hi,
> I have redhat 9.0 Linux installed on my system with gcc 3.2.3.
> This is the program i used, i compiled it with -lpthread option.
>
> Here i am starting 3 threads and then i am calling pthread_kill on a
> thread which is non-existent , the signal being passed is '0'.
> According to the POSIX standard if the thread doesn't exist it should
> set errno to ESRCH. But instead it causes a segmentation fault.
> Why is that ???
> Is there any work around to this problem ???
> Is this a linux problem or just Redhat 9.0 problem???

It's a weakness in the new NPTL thread library used by redhat 9.
pthread_kill and a few other functions cast the pthread_t to a pointer
and attempt to dereference it.  Obviously, this will cause a
segmentation fault.  The only workaround I can think of is not to pass
invalid thread IDs to these functions.

-- 
M�ns Rullg�rd
mru@kth.se
0
Reply mru 1/23/2004 11:54:32 AM


M�ns Rullg�rd wrote:
> anoop_kn@yahoo.com (Anoop Kumar) writes:
> 
> 
>>Hi,
>>I have redhat 9.0 Linux installed on my system with gcc 3.2.3.
>>This is the program i used, i compiled it with -lpthread option.

BTW -- compile with `-pthread', not `-lpthread' (though it's not 
directly relevant here it will avoid other potential problems).

>>
>>Here i am starting 3 threads and then i am calling pthread_kill on a
>>thread which is non-existent , the signal being passed is '0'.
>>According to the POSIX standard if the thread doesn't exist it should
>>set errno to ESRCH. But instead it causes a segmentation fault.
>>Why is that ???
>>Is there any work around to this problem ???
>>Is this a linux problem or just Redhat 9.0 problem???
> 
> 
> It's a weakness in the new NPTL thread library used by redhat 9.
> pthread_kill and a few other functions cast the pthread_t to a pointer
> and attempt to dereference it.  Obviously, this will cause a
> segmentation fault.  The only workaround I can think of is not to pass
> invalid thread IDs to these functions.
> 

--ag
-- 
Artie Gold -- Austin, Texas
0
Reply Artie 1/23/2004 2:46:58 PM

2 Replies
570 Views

(page loaded in 0.153 seconds)

Similiar Articles:













7/23/2012 9:20:19 AM


Reply: