Page 1 of 1

How should signals be implemented

Posted: Sat Mar 31, 2012 10:46 pm
by thepowersgang
Continuing on from a point raised by Solar in Implementing Ctrl-C termination, signals can be implemented using a dedicated thread to handle the signals.
Has anyone tried doing this and could provide feedback on how effective it is? From memory, the linux way is to interrupt a thread in the process (possibly terminating a syscall with EINTR) when a signal arrives, and hijacks the thread to handle the signal. This technique seems a little more ugly to implement, and once caused me quite a few issues in my day job (a library didn't realise that EINTR means try again, and assumed that there was an IO failure, so opened a new log file and caused heaps of IO overhead)

So, has anyone implemented signals and could lend an opinion?

Re: How should signals be implemented

Posted: Sun Apr 01, 2012 12:16 am
by gerryg400
Some parts of signals are easy, for example the real-time extensions and the synchronous calls like sigwaitinfo(). But some parts of signal implementation are difficult and messy.

In my opinion the part of signal handling that I see as being difficult is killing (or interrupting) a thread that's in the kernel. Rdos touched on this but I think it's even more difficult than he hinted at. This is because it usually involves things like stopping I/O that in progress, dropping locks that might be held (or being waited on) and releasing kernel resources. Adding this to a completed monolithic kernel design (or worse an SMP monolithic kernel design) will require those 'hooks all over the place' that you mentioned your original post. Stopping the I/O may not be too difficult as Snake pointed out in the other thread but dropping all those locks and ripping threads out of wait queues will always be messy.

Of course if you have a microkernel this problem is smaller. There are shorter paths, and fewer (perhaps no) I/O wait states and with a little luck not so many kernel locks.

I'm not sure about the idea of a dedicated thread. I have a microkernel and I always knew I was going to have signals so I was ready for them. I don't have many long code paths through the kernel and the long syscall paths consult the scheduler regularly which tells the running thread whether it has been signalled. Threads that aren't running are signalled by the scheduler at the point it was about to run them.
thepowersgang wrote:So, has anyone implemented signals and could lend an opinion?
The truth ? No, not yet. My current signal implementation supports SIGSTOP, SIGCONT and SIGKILL but only in my single core kernel. I've yet to implement signals in my new multicore setup.

Re: How should signals be implemented

Posted: Sun Apr 01, 2012 3:14 am
by Rudster816
I'm not too awful familiar with signals, but bare with me.

Seems to me like executing signal handlers in its own thread would add a lot more complexity than simply "injecting" the handler into the main thread. Since a programmer would be unaware of this extra thread running, he would (rightfully) assume that his signal handlers would be executed synchronously to his program's main thread. This means that you would have to ensure that the process to be signaled doesn't make any progress on the primary thread while the signal handler is being executed. This would obviously involve removing the thread from the scheduling queue(s) before calling the handler, and than readding them after it's completed. You might also consider adding some sort of flag to tell the scheduler to skip over that thread if it encounters it, which would be more efficient as far as kernel execution time, but could decrease performance on the process being signaled depending on your scheduling algorithm. Also, depending on how you expose this thread, it could confuse anyone who goes to debug their program because any breakpoint set in a signal handler would break in a thread that they didn't create (and not the main thread), which would be weird considering it's their code.

The way you described Linux's approach seems like a good option to me, apart from deciding the kill an active syscall. Assuming the main thread isn't in a syscall, you could do something like this:

1. Interrupt the thread if its currently running (SMP only).
2. Push entire program state on to thread's usermode stack.
3. Push a return address to a function (maybe define it in crt0.o?) that will restore this state, and jump to the saved instruction pointer from the original state.
4. Push signal handlers parameters (or place them in correct registers, whatever's appropriate for the calling convention obviously).
5. Jump to the signal handler.

You could accomplish this simply by just editing the saved program state, and letting the scheduler do its thing, and everything will sort it's self out automagically. Since you're just "injecting" code into the main thread, you'll avoid the one or two taskswitches (one if the process to be signaled is running, the other to return from the signal handler).

Obviously things get a little trickier if the main thread is in a syscall, you can't simply return to it from usermode. As far as I know though (I could very easily be wrong with this), there wouldn't be anything wrong (other than finishing it needlessly) with letting the syscall run to completion before calling the signal handler. You could just set a breakpoint at the very end of the syscall, and do the same process as above when you hit that breakpoint.

Re: How should signals be implemented

Posted: Sun Apr 01, 2012 3:46 am
by gerryg400
Rudster816 wrote:As far as I know though (I could very easily be wrong with this), there wouldn't be anything wrong (other than finishing it needlessly) with letting the syscall run to completion before calling the signal handler. You could just set a breakpoint at the very end of the syscall, and do the same process as above when you hit that breakpoint.
The problem is that some system calls will never complete. Imagine that you have a thread trying to read a file on a CD and the user ejects the CD. Then the system prompts the user to re-insert the disc. What happens if the user decides to abort the operation with a Ctrl-C ? Clearly you cannot wait until the system call completes. It's usually not acceptable to even wait for the time-out.

Signalling a thread in user-mode is a triviality compared to killing a system call.

Re: How should signals be implemented

Posted: Sun Apr 01, 2012 4:26 am
by bluemoon
I think it's better to not having a non-completing syscall. In your example you can design the syscall to return fail immediately and the user-application/library do the retry.

However, the trouble still exists for resource locks.

Re: How should signals be implemented

Posted: Sun Apr 01, 2012 4:49 am
by thepowersgang
Actually, I see blocked threads (via a syscall like select() or read()) as being the easier case. I can just signal the wait to terminate without completion, and then when the syscall returns it checks for pending signals before returning to userspace.

The only downside with this is that kernel bugs can cause an application never to terminate (I think I still have some busy waits around which would not be able to be terminated without causing the kernel state to be undefined). Then again, this should never happen so trying to design things around it might be a bad idea.

Re: How should signals be implemented

Posted: Sun Apr 01, 2012 6:09 am
by gerryg400
bluemoon wrote:I think it's better to not having a non-completing syscall.
I agree. It's my main reason for choosing a microkernel. It gets me closer to that goal.
bluemoon wrote:In your example you can design the syscall to return fail immediately and the user-application/library do the retry.
It's a good idea but it's difficult to get enough information back to the App so that it can create a sensible error message. You basically end up with Abort, Retry and Fail.

Re: How should signals be implemented

Posted: Sun Apr 01, 2012 9:29 am
by Owen
Some signals (e.g. EINTR) can be legally handled in a second thread (so called "asynchronous signals"), but others are synchronous and need to be handled in the relevant thread (e.g. SIGSEGV).

Presumably, most of your blocking syscalls will eventually call down to a small number of kernel wait primitives. For such primitives, it would make sense to me to add a flag which can be passed indicating that the wait can be legally interrupted by a signal. Of course, this does require adding code to back out the operation; there is no way around this.

This is one area where a C++ with exceptions kernel can simplify things: If your code is written in a good RAII manner, then the back-out code should often come for free.