Re: timeout problem while using poll()



Rick Jones <rick.jones2@xxxxxx> writes:
Sinan <sardok@xxxxxxxxx> wrote:
i'm using poll() system call on 1 udp socket for both reading and
writing, i'm setting timeout value but poll() never returns 0 which
i need. instead of returnin 0, it returns 1 continuously and ready
for writing (POLLOUT flag is enabled) , so i'm guessing, since it is
never got block, timeout value resets all the time. what should i
do?

this is how i used in code,

int timeout = 2000;
while(1) {
if ( (pret = poll(&pfd, 1, timeout)) < 0) {
err_sys(1, "main()->poll");
}
if(!pret) {
printf("timeout happens sometimes\n");
break;
}
if(pfd.revents & POLLIN) {
nread = udm_socket_read(buff);
UCP_reply_handler(buff, nread,
&first_ubi_dev);

}
if(pfd.revents & POLLOUT) {
if(sendcmd) {
if(sendcmd()) {
sendcmd = NULL;
}

Um, why the two "sendcmd()" calls there?

There isn't. He's using a stupid C trick of treating a function pointer as
a boolean.

It more correctly should be

if (sendcmd != NULL) {
if (sendcmd() != 0) {
sendcmd = NULL;
}
}

Assuming sendcmd is a pointer to a function returning int.

scott
.