[Subject Prev][Subject Next][Thread Prev][Thread Next][Subject Index][Thread Index]

Re: [LI] Signal behaves differently on LINUX and HPUX



On Thu, Oct 14, 1999 at 11:24:20AM +0530, Ghins Mathai wrote:
> Hi,
>   SIGALRM  interrupts  the system calls in HPUX even when
>   signal handler is present.
>   When the signal is sent, the signal handler is called but the blocking
> 
>    system calls like "accept" are interrupted.
> 
>    Can somebody tell why does this happen on HPUX and
>     how to solve it?

This is one of the ugly parts of UNIX. The signal handler executes in
the context of the process that got the signal, instead of in a context
of it's own.

System calls are restartable on x86 Linux using the following trick.

1. User process makes a system call using the instruction: "int 0x80"
2. The process switches to it's kernel stack and executes the system call.
3. If it is a blocking system call, it sleeps.
4. A signal is delivered.
5. The signal handler gets executed on either the user stack or an alternate
   stack specified using sigaltstack. Once it is done, the signal handler
   executes a sigreturn system call. This is hidden from the programmer. 
   The actual mechanism involves having executable code on the user stack
   and engineering the return address of the signal handler to jump to
   that code.
6. Now comes the ugly part. The EIP of the process that got the signal is
   decremented so that it reexecutes the "int 0x80", instead of the next
   instruction.
7. A return from syscall (iret) happens.
8. The processor executes int 0x80 and since it has all it's registers
   (The ones containing the syscall number and args) preserved, it 
   re-executes the system call successfully.

Note that it is not as easy on all processors. You have to sacrifice some
performance and use arcane compiler switches to preserve the registers so
that step 8 can work. So some OSes implement system call restarting by
returning an error code and having the C library re-execute the interrupted
system call. And my guess is that HP-UX may be one of them.

The whole problem is that there can be only one system call active at 
any time per process. So you can't have your "accept" and "sigreturn"
system call active simultaneously. 

A better OS design would be to spawn another thread in the same process
and have it execute the signal handler and continue the main thread
uninterrupted. This will completely avoid the system call restarting issue.

	-Arun

--------------------------------------------------------------------
The Linux India Mailing List Archives are now available.  Please search
the archive at http://lists.linux-india.org/ before posting your question
to avoid repetition and save bandwidth.