Interrupted read() calls update stat structure (Unix-specific) 
Author Message
 Interrupted read() calls update stat structure (Unix-specific)

I am having a problem with one of our programs interaction with
"idle-out" daemons, such as finger or "w".  The program polls the
keyboard as part of an event gathering loop.  The Unix SysV code does a
sequence of:

        signal(SIGALRM, handler);
        alarm(1);
        read(0, ...);
        alarm(0);
        signal(SIGALRM,SIG_IGN);

to effect the poll.  This works well, except that it cause the idle-out
program to think a read has occured.  It seems that an interrupted
read() causes the stat structure a_time and m_time elements to be
updated, even though no input occured.  The following small program
demonstrates this:

/* begin demonstration program */
/*
 * This program reads from fd 0 until an EOF is seen.  If no input occurs
 * within 2 seconds, the read() is interrupted and the loop cycles.
 * The printout shows the stat structure changes with each interrupted read().
 */
#include <stdio.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/stat.h>

static int aflag = 0;

main()
{
        char c[1];
        int atrap();
        struct stat sb;

        for (;;aflag=0) {
                signal(SIGALRM, atrap);
                alarm(2);
                if (read(0, c, 1) == 0)
                        break;
                alarm(0);
                signal(SIGALRM, SIG_IGN);
                if (fstat(0, &sb) != -1)
                        printf("%s read: atime=%ld, mtime=%ld\n",
                                (aflag ? "interrupted" : "completed"),
                                sb.st_atime, sb.st_mtime);
                }
        exit(0);

Quote:
}

atrap()
{
        aflag++;
Quote:
}

/* end demonstration program */

My question is -- given a (pre-streams) Unix SysV environment, is there
a method of polling the keyboard which will *not* produce a change to
the stat structure?  I have tried toggling the fcntl() O_NDELAY bit, but
this seems to put a greater strain on the cpu.  And, running with
O_NDELAY set for the duration of the program can really bite you, if an
unforseen death occurs.

Any solutions or suggestions will be greatly appreciated.

--
Dave Hammond

uunet!masa.com!marob!daveh



Sat, 11 Jul 1992 07:28:39 GMT  
 
 [ 1 post ] 

 Relevant Pages 

1. Unix specific tool list - TRIDLIST - Updated

2. Newbie question -- printing to screen from stat structure

3. COM 3 + 4 (Interrupt Code Needed) (DOS/x86 specific)

4. Wrong time from stat.st_atime using stat()

5. int stat(char *, struct stat *)

6. What is the 64 bit stat function call name for C in GNU

7. stat library call

8. Portable code versus unix specific build procedures

9. interrupt service routine in UNIX

10. Unix specific questions in comp.lan

11. Running C program in specific UNIX environment

12. point to a specific part of a structure

 

 
Powered by phpBB® Forum Software