Received: from PACIFIC-CARRIER-ANNEX.MIT.EDU by po7.MIT.EDU (5.61/4.7) id AA16481; Tue, 20 Feb 96 16:30:20 EST
Received: from YAZ-PISTACHIO.MIT.EDU by MIT.EDU with SMTP
	id AB18825; Tue, 20 Feb 96 16:29:57 EST
Received: by yaz-pistachio.MIT.EDU (5.57/4.7) id AA11497; Tue, 20 Feb 96 16:30:25 -0500
Message-Id: <9602202130.AA11497@yaz-pistachio.MIT.EDU>
To: Anthony Green <green@cygnus.com>
Cc: proven@MIT.EDU
Subject: Re: more pthreads questions 
In-Reply-To: Your message of "Tue, 20 Feb 1996 12:05:01 PST."
             <199602202005.MAA09885@cygnus.com> 
Date: Tue, 20 Feb 1996 16:30:24 EST
From: Christopher Provenzano  <proven@MIT.EDU>


> 
> Thanks - it doesn't hang anymore, however, when I run it, it spits out
> "0", but I think it should say "1".

Hmmm, I assumed that if wait() was called that the SIGCHLD wasn't 
delivered. I'll verify this and make sure pthreads is correct.

> 
> On another note, signals seem deferred during fgetc(). Is this right?
> I have an interactive cmdline program that should react to a Ctrl-C
> during user input, but it is postponed until after the user has
> entered the command.
> 

Right, that is because the thread the signal is delivered to is blocked
waiting for a read to complete. Currently pthreads delivers the signal
to the thread but the signal handler isn't executed until the read()
returns. 

If the signal is one where the process terminates then I believe the
correct solution is to not bother to deliver it and terminate the process
so long as the current thread doesn't have it blocked. I'll implement
that behavior in the great signal code rewrite. Otherwise it isn't
clear that the signal can't be held pending.

A work around that I suggect it to create a thread from main() and have
it call sigwait() to process all asynchronous signals. This should do the
right thing on all implementations of pthreads. Note that signal masks
are inherited by the child thread from the parent thread.

void * sigwaiter(void * arg)
{
	int sig;

	while(sigwait((sigset_t *)arg, &sig) == 0) {
	switch (sig) {
	case SIGINT:
		/* Do signal processing here */
		break;
	default:
		/* Unexpected signal */
		exit(1);
		break;
	}
	/* Unexpected error */
	exit(1);
	
}

main()
{
	pthread_t thread_id;
	sigset_t set;

	sigemptyset(&set);
	sigaddset(&set, SIGINT);
	pthread_sigmask(SIG_BLOCK, &set, NULL);
	pthread_create(&thread_id, NULL, sigwaiter, (void *)&set);
}

CAP
