Solved

An example use of poll()

Posted on 1997-04-21
1
432 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
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
repeatEnd java challenge 42 83
java ^ examples 8 57
haveThree challenge 22 100
fix34  challenge 9 97
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…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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.
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

758 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now