Skip site navigation (1)Skip section navigation (2)
Date:      Wed, 19 Jun 2002 22:54:49 +1100 (edt)
From:      Andrew MacIntyre <andymac@bullseye.apana.org.au>
To:        <freebsd-hackers@freebsd.org>
Subject:   signals in apps built with -pthread
Message-ID:  <Pine.OS2.4.32.0206192244550.74-200000@tenring.andymac.org>

next in thread | raw e-mail | index | archive | help

[-- Attachment #1 --]
[originally posted to -questions on June 2, with no response.  If this
doesn't belong on this list, please say so.]

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 (ie gcc -o sigp sigp.c), the attached code
behaves as expected like so:

$ ./sigp
1,
run 2
$

When compiled with -pthread (ie gcc -pthread -o sigp sigp.c):

$ ./sigp


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

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;
}

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