[linux-audio-dev] pthreads and signals

David Olofson david at olofson.net
Wed May 14 11:20:01 UTC 2003


On Wednesday 14 May 2003 16.12, David Olofson wrote:
> [...]
> I believe the SDL parachute traps segfaults as well, BTW. I'll have
> a look...

It does indeed, but I can't find any special treatment. Looks like 
just another signal.

However, according to 'man signal', it seems to me that SIGSEGV (and 
some others) *are* a bit special. I concluded that you have to make 
sure the thread that causes the segfault must be killed by the signal 
handler, or weird things will happen. (Zombie, endless loop calling 
the signal handler or something. My system seems to do the latter.)

This code Works Here(TM), or at least does what I think it should:

---8<------------------------------------------------------
#include <stdlib.h>
#include <stdio.h>
#include <signal.h>
#include <string.h>
#include <unistd.h>
#include <pthread.h>

void handler(int sig)
{
	signal(sig, SIG_DFL);
	switch (sig)
	{
	  case SIGSEGV:
		puts("We got a segfault!");
		pthread_exit(NULL);
		break;
	  default:
		puts("Unknown");
		break;
	}
}

void *crasher(void *arg)
{
	long i = 42, *j = NULL;
	signal(SIGSEGV, handler);
	puts("crasher() started.");
	sleep(3);
	puts("crasher(): Let's crash!");
	*j = i;
	puts("crasher() ended. (How the f...!?)");
}

int main(int argc, char *argv[])
{
	pthread_t thread;
	puts("main() started.");
	pthread_create(&thread, NULL, crasher, NULL);
	while(1)
	{
		puts("main() is alive.");
		sleep(1);
	}
	return 0;
}
------------------------------------------------------>8---

//David Olofson - Programmer, Composer, Open Source Advocate

.- The Return of Audiality! --------------------------------.
| Free/Open Source Audio Engine for use in Games or Studio. |
| RT and off-line synth. Scripting. Sample accurate timing. |
`-----------------------------------> http://audiality.org -'
   --- http://olofson.net --- http://www.reologica.se ---




More information about the Linux-audio-dev mailing list