Solved

An example use of poll()

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
while loop over for loop 7 92
string initialization in java 11 109
java ^ examples 8 62
building a service to accessed via telnet client, want colors ? 4 80
In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
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.
This is a video that shows how the OnPage alerts system integrates into ConnectWise, how a trigger is set, how a page is sent via the trigger, and how the SENT, DELIVERED, READ & REPLIED receipts get entered into the internal tab of the ConnectWise …

932 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

8 Experts available now in Live!

Get 1:1 Help Now