Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!mnetor!seismo!ll-xn!cit-vax!mangler From: mangler@cit-vax.Caltech.Edu (System Mangler) Newsgroups: comp.unix.wizards Subject: Re: No more signals Message-ID: <3103@cit-vax.Caltech.Edu> Date: Sun, 28-Jun-87 00:33:10 EDT Article-I.D.: cit-vax.3103 Posted: Sun Jun 28 00:33:10 1987 Date-Received: Sun, 28-Jun-87 05:25:45 EDT References: <48300005@hcx1> <1714@munnari.oz> <492@its63b.ed.ac.uk> Organization: California Institute of Technology Lines: 52 Summary: safe way to wait for a signal In article <492@its63b.ed.ac.uk>, simon@its63b.ed.ac.uk (Simon Brown) writes: > One really nasty bug that really needs fixing fast is that the so-called > "safe signal" mechanism is anything but! If I send 5 SIGINT signals to a > process, there is no guarentee that that process will receive them all - ok, Signals weren't designed to be used like that, but if you have to use them for IPC, there's really only about one safe yet portable way to wait for a signal, and that's to use longjmp. int ready, caught; jmp_buf jbuf; catch() { caught = 1; if (ready) longjmp(jbuf, 1); } main() { /* do a bunch of work, then we want to wait for a signal to proceed */ if (!caught && setjmp(jbuf) == 0) { ready = 1; /* must be atomic */ while (!caught) pause(); } ready = caught = 0; /* can now tell outside world that we're ready for another signal */ This longjmp stuff is necessary because the signal could arrive between the time you check for it and the time you pause(). (If anyone knows another way in the absence of "reliable signals", PLEASE tell me). You can only reliably deal with one signal (of any type) at a time; if you get two at once, the longjmp of one will abort the signal catch routine of the other (if getting a signal in the middle of longjmp doesn't screw up the stack). Despite the limitations, signals can be useful for IPC, because they're a lot faster than pipes, and they set runrun, which gets the signaled process off to a running start. On 4.[23] BSD, flock() is just slightly faster, but you really have to go through contortions to use it for IPC. 4.3 BSD dump uses it, and it's a mess. If you want multiple occurances of a signal to be distinct, what you really need is message passing. Think of a signal as a zero- length message with a 4- or 5-bit type field, with a one-entry queue for each type. Don Speck speck@vlsi.caltech.edu {seismo,rutgers,ames}!cit-vax!speck