Linux kernel & device driver programming

Cross-Referenced Linux and Device Driver Code

[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ]
Version: [ 2.6.11.8 ] [ 2.6.25 ] [ 2.6.25.8 ] [ 2.6.31.13 ] Architecture: [ i386 ]
  1 /*
  2  * Test out reading with poll()
  3  * This should run with any Unix
  4  *
  5  * Copyright (C) 2003 Alessandro Rubini and Jonathan Corbet
  6  * Copyright (C) 2003 O'Reilly & Associates
  7  *
  8  * The source code in this file can be freely used, adapted,
  9  * and redistributed in source or binary form, so long as an
 10  * acknowledgment appears in derived source files.  The citation
 11  * should list that the code comes from the book "Linux Device
 12  * Drivers" by Alessandro Rubini and Jonathan Corbet, published
 13  * by O'Reilly & Associates.   No warranty is attached;
 14  * we cannot take responsibility for errors or fitness for use.
 15  *
 16  * $Id: polltest.c,v 1.1 2003/02/07 18:01:38 corbet Exp $
 17  */
 18 
 19 #include <stdio.h>
 20 #include <unistd.h>
 21 #include <stdlib.h>
 22 #include <errno.h>
 23 #include <sys/poll.h>
 24 #include <fcntl.h>
 25 
 26 char buffer[4096];
 27 
 28 int main(int argc, char **argv)
 29 {
 30     struct pollfd pfd;
 31     int n;
 32 
 33     fcntl(0, F_SETFL, fcntl(0,F_GETFL) | O_NONBLOCK); /* stdin */
 34     pfd.fd = 0;  /* stdin */
 35     pfd.events = POLLIN;
 36 
 37     while (1) {
 38         n=read(0, buffer, 4096);
 39         if (n >= 0)
 40             write(1, buffer, n);
 41         n = poll(&pfd, 1, -1);
 42         if (n < 0)
 43             break;
 44     }
 45     perror( n<0 ? "stdin" : "stdout");
 46     exit(1);
 47 }
 48 
  This page was automatically generated by the LXR engine.