View Single Post
Old 26-07-2004, 15:40   #12
Ed_Bunker
Senior Member
 
L'Avatar di Ed_Bunker
 
Iscritto dal: Jan 2004
Cittā: Montignoso(MS)
Messaggi: 9458
Quote:
Originariamente inviato da ilsensine
Ad esempio questo funziona...
Codice:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/poll.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

static void __attribute__((__noreturn__)) fail(const char *s) {
  perror(s);
  exit(-1);
}

int main(void) {
  int fd;
  int ret;
  char buf[4096];
  struct pollfd pfd;
reopen:
  fd = open("pipe_name", O_RDONLY|O_NONBLOCK);
  if(fd<0) fail("open");
  pfd.fd = fd;
  pfd.events = POLLIN;
  fprintf(stderr, "pipe aperto sul fd %d...\n", fd);
retry:
  ret = poll(&pfd, 1, -1);
  fprintf(stderr, "poll=%d revents=%d\n", ret, pfd.revents);
  if(pfd.revents&POLLIN) {
    ret = read(fd, buf, 4095);
    fprintf(stderr, "read=%d\n", ret);
    if(ret<=0) goto retry;
    buf[ret] = '\0';
    fprintf(stderr, "recv %s\n", buf);
    goto retry;
  } else if(pfd.revents&POLLHUP) {
    fprintf(stderr, "Hangup!\n");
    close(fd);
    goto reopen;
  } else {
    fprintf(stderr, "!?\n");
    goto retry;
  }
  return 0;
}
Pero' in questo modo potresti fare attesa attiva senza "accorgertene". O no ?!? Tu controlli il valore di ritorno della poll ed in tutti i casi o fai una poll successiva oppure passi a fare la read (Nel caso ret valga 1). Ma il fatto che ret valga 0 non dovrebbe essere un errore, visto che tu non hai imposto un timeout alla poll (-1 potrebbe invece verificarsi in caso d'errore) ?!? Ora riprovo...
__________________
"Il Meglio che si possa ottenere č evitare il peggio." I.C.
Ed_Bunker č offline   Rispondi citando il messaggio o parte di esso
 
Hardware Upgrade Forum Database Error
Database Error Database error
The Hardware Upgrade Forum database has encountered a problem.

Please try the following:
  • Load the page again by clicking the Refresh button in your web browser.
  • Open the www.hwupgrade.it home page, then try to open another page.
  • Click the Back button to try another link.
The www.hwupgrade.it forum technical staff have been notified of the error, though you may contact them if the problem persists.
 
We apologise for any inconvenience.