Skip site navigation (1)Skip section navigation (2)
Date:      Sun, 2 Jun 2002 23:49:13 +1100 (edt)
From:      Andrew MacIntyre <andymac@bullseye.apana.org.au>
To:        <questions@freebsd.org>
Subject:   signals vs threads
Message-ID:  <Pine.OS2.4.32.0206022312270.1659-200000@tenring.andymac.org>

index | next in thread | raw e-mail

[-- Attachment #1 --]
This question is in the context of a scripting language interpreter
(Python) which is built with threads (gcc -pthread), but which is
used to run scripts which don't activate any threads.

In such instances, signal handling differs from an interpreter built
without threads.

The attached C code is a simple example of a signal handling situation
which works in the non-threaded interpreter, but fails in a threaded
interpreter.

When compiled without -pthread, the attached code behaves as expected like
so:

$ ./sigp
1,
run 2
$

When compiled with -pthread:

$ ./sigp


and then it hangs.  Sending it a SIGHUP from another terminal has no
effect, but other likely signals (eg SIGINT) kill it.

Indications are that this code functions as expected on Solaris (Linux
too FWIW) when the code is built with thread support enabled.

There appears to have been a thread on a closely related topic, found at
(long URL):
http://groups.google.com/groups?hl=en&lr=&threadm=fa.n97llov.1lict2d%40ifi.uio.no

However, no information from that thread has illuminated the situation at
hand.  Having two interpreter executables (with and without threads) is
unpalatable.

Is there any way that the thread enabled interpreter can be setup to
handle signals in the same way, in the absence of activated threads, as it
would without thread support? (without disturbing the signal handling when
threads _are_ activated, of course...)

Thanks for any advice,
Andrew.

--
Andrew I MacIntyre                     "These thoughts are mine alone..."
E-mail: andymac@bullseye.apana.org.au  | Snail: PO Box 370
        andymac@pcug.org.au            |        Belconnen  ACT  2616
Web:    http://www.andymac.org/        |        Australia


[-- Attachment #2 --]
#include <stdlib.h>
#include <signal.h>
#include <stdio.h>

volatile int run = 0;

#define RUN(i) if (run) printf("run %d\n",i)

void
do_nothing(int signum)
{
	run = 1;
	return;
}

void
print_sigset(const sigset_t* set)
{
	int sig;

	for (sig = 1; sig < NSIG; sig++) {
		if (sigismember(set, sig)) {
			printf("%d, ", sig);
		}
	}
	printf("\n");
}

int main(int argc, char** argv)
{
	sigset_t set;
	struct sigaction action;

	action.sa_handler = do_nothing;
	sigemptyset(&action.sa_mask);
	action.sa_flags = 0;

	sigemptyset(&set);

	sigaddset(&set, SIGHUP);

	sigprocmask(SIG_BLOCK, &set, NULL);

	sigaction(SIGHUP, &action, NULL);

	kill(getpid(), SIGHUP);

	RUN(0);
	
	sigpending(&set);

	print_sigset(&set);

	sigemptyset(&set);

	RUN(1);

	sigsuspend(&set);

	RUN(2);

	return 0;
}
help

Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?Pine.OS2.4.32.0206022312270.1659-200000>