Sponsored Content
Top Forums Shell Programming and Scripting ksh interrupt read instruction with signal Post 302578066 by Corona688 on Wednesday 30th of November 2011 04:32:56 PM
Old 11-30-2011
It may be an operating system incompatibility. POSIX allows a signal to interrupt a read() command, but I don't think it requires it.

Tooling up to see if I can reproduce this behavior in raw C.

---------- Post updated at 03:32 PM ---------- Previous update was at 03:14 PM ----------

Thought I'd looked at that before, and had. This program:

Code:
#include <stdio.h>
#include <signal.h>
#include <unistd.h>

int handler(int sig)
{
        fprintf(stderr, "!!!\n");
}

int main(void)
{
        char buf[512];
        ssize_t s;
        signal(SIGALRM, handler);
        alarm(1);
        s=read(STDIN_FILENO, buf, 512);
        if(s <= 0) perror("Error");
        return(0);
}

Code:
$ ./a.out
!!!
^C
$

So receiving SIGALRM doesn't necessarily cause read() to return EINTR.

Looking around the man pages, I find this in man 7 signal:

Code:
   Interruption of System Calls and Library Functions by Signal Handlers
       If  a signal handler is invoked while a system call or library function
       call is blocked, then either:

       * the call is automatically restarted after the signal handler returns;
         or

       * the call fails with the error EINTR.

       Which  of  these  two  behaviors  occurs  depends  on the interface and
       whether or not the signal handler was established using the  SA_RESTART
       flag  (see sigaction(2)).  The details vary across Unix systems; below,
       the details for Linux.

       If a blocked call to one of the following interfaces is interrupted  by
       a  signal  handler, then the call will be automatically restarted after
       the signal handler returns if the SA_RESTART flag was  used;  otherwise
       the call will fail with the error EINTR:

           * read(2),  readv(2),  write(2),  writev(2),  and ioctl(2) calls on
             "slow" devices.  A "slow" device is one where the  I/O  call  may
             block  for  an indefinite time, for example, a terminal, pipe, or
             socket.  (A disk is not a slow device according to  this  defini-
             tion.)   If  an I/O call on a slow device has already transferred
             some data by the time it is interrupted by a signal handler, then
             the  call  will  return a success status (normally, the number of
             bytes transferred).

...

Let's try that out:

Code:
#include <stdio.h>
#include <signal.h>
#include <unistd.h>

int handler(int sig)
{
        fprintf(stderr, "!!!\n");
}

int main(void)
{
        struct sigaction act, old;
        char buf[512];
        ssize_t s;

        act.sa_handler=handler;
        sigemptyset(&act.sa_mask);
        act.sa_flags=SA_RESETHAND;
        sigaction(SIGALRM, &act, &old);

        alarm(1);
        s=read(STDIN_FILENO, buf, 512);
        if(s <= 0) perror("Error");
        return(0);
}

Code:
$ ./a.out
!!!
Error: Interrupted system call

$

So Linux signals can cause EINTR, but it's an optional feature.

If KSH is really, really supposed to get EINTR when signalled, that it doesn't is a bug in KSH since Linux can do that if you use sigaction instead of signal(). (You can also turn it off in sigaction with SA_RESTART). I'm not convinced it's supposed to be a portable feature in any case though.
This User Gave Thanks to Corona688 For This Post:
 

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Interrupt signal Control C takes too long to terminate a process

I have a process to terminate, and when keying Control C/ kill -int , it takes 15 minutes to half an hour to terminate the process. I've tried using kill -2, or keying control c twice, however the process seem to be killed abruptly, without writing into the log file. So the only way in order to... (8 Replies)
Discussion started by: paqui
8 Replies

2. Shell Programming and Scripting

ksh read timeout

any idea on how to timeout the read statement for ksh? for bash u can use read -t option -t timeout Cause read to time out and return failure if a complete line of input is not read within timeout seconds. This option has ... (2 Replies)
Discussion started by: ashterix
2 Replies

3. Shell Programming and Scripting

is there any way to read a line twice in KSH

Hi All, Is there any way to read the previous line in file reading ? or is there any way to read a line twice in KSH ? thanks in advance !! Srini (6 Replies)
Discussion started by: Srini75
6 Replies

4. Shell Programming and Scripting

read from console in ksh

I am stuck with a problem while reading data from a file.. while do read line #do some operations and if some condition is satisfied, ask the user to enter his choice. # using the choice continue operations. done < fileBeingRead.txt The... (4 Replies)
Discussion started by: ajaykumarns
4 Replies

5. Programming

how to discard instruction from previous signal

hello everyone, I'm having a problem doing signal handling so I post this thread to see if I could get help. I want asynchronous signal handling, that means when I'm processing a signal (signal 1), if the same signal comes (signal 2) that signal (signal 2) shall be processed; and moreover,... (7 Replies)
Discussion started by: nvhoang
7 Replies

6. UNIX for Dummies Questions & Answers

simple way to read an array in ksh

hi, I'm a newbie to shell scripting. I wanted to initialise an array using basic for loop and read it. Then i want to print it as a .CSV file.. Any help would me much appreciated.. (1 Reply)
Discussion started by: pravsripad
1 Replies

7. UNIX for Advanced & Expert Users

Interrupt storm detected on "irq 20" throttling interrupt source

I receive the following warning messages on a very new machine which has FreeBSD 8.1 x64 installed on it: Interrupt storm detected on "irq 20" throttling interrupt source It is unclear what this means and what its origins are (motherboard? CPU? RAM?). I can start the desktop and the message is... (4 Replies)
Discussion started by: figaro
4 Replies

8. UNIX for Advanced & Expert Users

Is there any shell command to show which interrupt handler handle which interrupt number?

Hi, all: Is there any shell command to show which interrupt handler handle which interrupt number in the system? li,kunlun (5 Replies)
Discussion started by: liklstar
5 Replies

9. Shell Programming and Scripting

ksh while read issue

Hello, I have used a chunk of ksh script similar to this in many places without any issue: while : do print; read OPTION?"Enter a number (q to quit): " expr ${OPTION} + 1 >/dev/null 2>&1 CHECKVAL=$? if }" != ${OPTION} ]; then ... (2 Replies)
Discussion started by: port43
2 Replies

10. Shell Programming and Scripting

Signal trapped during read resumes sleeping

Greetings. This is my first post in this forum; I hope y'all find it useful. One caveat: "Concise" is my middle name. NOT! :D I am almost done with a shell script that runs as a daemon. It monitors a message log that is frequently written to by a database server but it it works my client will... (2 Replies)
Discussion started by: jakesalomon
2 Replies
All times are GMT -4. The time now is 02:45 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy