Solved

An example use of poll()

Posted on 1997-04-21
1
455 Views
Last Modified: 2013-12-26
I would like to see how to use poll()?
e.g. Open a list of files from the command line, use
poll to read and write to screen.
0
Comment
Question by:n1443160
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 2

Accepted Solution

by:
Philippe earned 100 total points
ID: 1292626

n1443160,

First a short note on poll and normal files:

Poll will only tell you if reading a file would block or not. Thus on a normal file it will always say that the file is readable.

The program below will open all the files on the command line plus stdin. It will then use poll to find out which ones are readable and read one character from each at a time. It will poll once per second. Note that if any of the files is a normal file
you will get read errors as soon as you reach the end of the file (see above).

Two simple test with the program:

1. run it without command line arguments and type in some text
   It will print out your text each time you type return and
    std-in becomes readable.

2. Assuming you are on a unix system. Create a named pipe (a fifo) using the command mkfifo myfifo. Then in one window do
a cat > myfifo and in another window run the program with myfifo as parameter. You can now type in each window and see the text
appear as soon as you hit return.

 Hope this answers your question, I'll be glad to answer further questions.

   Philippe
   
Here is the sample program:

#include <poll.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

#define MAX_FILES 32

main (int argc, char ** argv)
{
  int i;
  struct pollfd fds[MAX_FILES];
 
  char c;

  /* use only fifos and not normal files ! */

  for (i=1; i< argc; i++)
    if ((fds[i].fd = open(argv[i],O_RDONLY)) == -1)
      {perror(argv[i]); exit(0);}
    else
      fds[i].events = POLLIN;

  /* add stdin to make it more interesting */

  fds[0].fd = 0; /* 0 is stdin */
  fds[0].events = POLLIN;

  while (1) {
    /* wait a second for data to read from files */
   
    if (poll(fds, (unsigned long) argc, 1000) < 0)
      perror ("poll");

    for (i=0; i< argc; i++)
      if (fds[i].revents) {
      if (read(fds[i].fd,&c,1) != 1)
        perror("reading");
      printf("read %c from %s \n", c, i==0?"stdin":argv[i]);
      fds[i].revents = 0;
      }
  }
}

0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

691 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question