[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

pthreads and kernel 2.6 problem



-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Running Debian unstable with the 2.6.7 kernel from kernel.org, no
patches but only support for my hardware is compiled in (I can make the
config available if required).

Programs using pthread_cancel() crash with segmentation fault depending
upon the kernel, I'm trying to determine whether the fault lies in the
kernel, pthreads, my pc's configuration or some other idiocy on my part.

The following short program crashes with a seg fault:

#include <pthread.h>    /* include file for pthreads - the 1st */
#include <stdio.h>      /* include file for printf()           */
#include <unistd.h>     /* include file for sleep()            */

void *Thread(void *string)
{
~        int i;
~        int o_state;

~        /* disables cancelability */
~        pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &o_state);
~        /* writes five messages */
~        for (i=0; i<5; i++)
~                printf("%s\n", (char *)string);
~        /* restores cancelability */
~        pthread_setcancelstate(o_state, &o_state);
~        /* writes further */
~        while (1)
~                printf("%s\n", (char *)string);
~        pthread_exit(NULL);
}

int main()
{
~        char *e_str = "Hello!";
~        char *f_str = "Bonjour !";
~        pthread_t e_th;
~        pthread_t f_th;
~        int rc;

~        /* creates both threads */
~        rc = pthread_create(&e_th, NULL, Thread, (void *)e_str);
~        if (rc)
~                return -1;
~        rc = pthread_create(&f_th, NULL, Thread, (void *)f_str);
~        if (rc)
~                return -1;
~        /* sleeps a while */
~        sleep(10);
~        /* requests cancellation */
~        pthread_cancel(e_th);
~        pthread_cancel(f_th);
~        /* sleeps a bit more */
~        sleep(10);
~        pthread_exit(NULL);
}

Output from running it under gdb is :

#--- Output start ---
Hello!
Hello!

Program received signal SIG32, Real-time event 32.
[Switching to Thread 1083710384 (LWP 2625)]
0x40114f51 in write () from /lib/tls/libc.so.6
(gdb) bt
#0  0x40114f51 in write () from /lib/tls/libc.so.6
#1  0x400b53f9 in _IO_file_write () from /lib/tls/libc.so.6
#2  0x400b461f in _IO_do_write () from /lib/tls/libc.so.6
#3  0x400b45b8 in _IO_do_write () from /lib/tls/libc.so.6
#4  0x400b4ad9 in _IO_file_overflow () from /lib/tls/libc.so.6
#5  0x400b550e in _IO_file_xsputn () from /lib/tls/libc.so.6
#6  0x4008f2eb in vfprintf () from /lib/tls/libc.so.6
#7  0x40095ac2 in printf () from /lib/tls/libc.so.6
#8  0x40176d40 in _IO_2_1_stdin_ () from /lib/tls/libc.so.6
#9  0x080487b0 in _IO_stdin_used ()
#10 0x40981ab8 in ?? ()
#11 0x40043b18 in __JCR_LIST__ () from /lib/tls/libpthread.so.0
#12 0x080485c9 in Thread ()
#13 0x4003b9b4 in start_thread () from /lib/tls/libpthread.so.0
#14 0x00000000 in ?? ()
(gdb)
#--- Output end ---

The program was compiled simply with "gcc -lpthread thrd2.c"

It seems to run perfectly well under the 2.6.7-1-k7 kernel.

I also wrote a very small program which simply starts two threads,
cancels them, joins them and quits. I don't have the actual code I wrote
to hand, but the gist is :

thread_func()
{
	wait();
}

main()
{
	pthread_t t1;
	pthread_t t2;

	pthread_create(t1, NULL, &thread_func, NULL);
	pthread_create(t2, NULL, &thread_func, NULL);
	pthread_cancel(t1);
	pthread_cancel(t2);
	pthread_join(t1, NULL);
	pthread_join(t2, NULL);
}

Removing the pthread_cancel calls makes the program work fine, but of
course I have to wait for each thread to complete.

- --
Andy Ruddock
- ------------
Senior Software Developer (andy.ruddock@minnesund.net)
GPG Key IDs : DSA/EIGamal=0x4E509520 RSA=0x5C38FD43 DSA=0x8B428591
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.5 (GNU/Linux)
Comment: Using GnuPG with Debian - http://enigmail.mozdev.org

iD8DBQFBEWFsRVHR7k5QlSARAlRBAKD2diznBtUF3y34AiOlgXSeZvauiQCgpD6o
vGNFerGbMljeVsN5LMWWOQA=
=FNm6
-----END PGP SIGNATURE-----



Reply to: