Operating System - HP-UX
1825793 Members
2292 Online
109687 Solutions
New Discussion

POLLNORM persists on empty-pipe

 
Kiran N. Mehta
Advisor

POLLNORM persists on empty-pipe

Hello:

Can someone help me with why poll(2) returns with POLLNORM set even after the pipe is emptied (on HP-UX 11.00):

===== Test Case Begins =======

#include
#include
#include
#include
#include
#include
int main(void)
{
pid_t pid, pfd[2];
char lineCount[BUFSIZ], mybuf[PIPE_BUF];
int n, nbytes, pass;
n = nbytes = pass = 0;


pipe(pfd);

if ( (pid = fork()) < 0 ) {
perror("main: ");
}
else if (pid > (pid_t)0) /* parent */
{
sleep(5);
close(pfd[1]);

while ( poll_for_readability( pfd[0] ) )
{
pass++;
sprintf(lineCount, "%d:", pass);
n = strlen(lineCount);
write(STDOUT_FILENO, lineCount, n);

nbytes = read(pfd[0], mybuf, PIPE_BUF);
if (nbytes)
write(STDOUT_FILENO, mybuf, nbytes);
else
write(STDOUT_FILENO, "No data for this pass\n", 22);

mybuf[0] = '\0';
nbytes = 0;

waitpid(pid, NULL, WNOHANG);
}

}
else /* child */
{
close(pfd[0]);
write(pfd[1], "Hello World\n", 12);
close(pfd[1]);
}

return 0;
}

int poll_for_readability(int sfd)
{
struct pollfd fds;
int fdCount = 0;

fds.fd = sfd;
fds.events = POLLNORM;
fds.revents = 0;

if ( (fdCount = poll(&fds, 1, 0)) < 0 )
{
perror("poll failed");
exit(1);
}
else if (fds.revents&(POLLERR|POLLHUP|POLLNVAL))
return 0;
else if (fds.revents&POLLNORM)
return 1;
else
return 0;
}

===== Test Case Ends =======

Thanks,
Kiran