LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   When ( p.revents & POLLIN ) will be true ? (https://www.linuxquestions.org/questions/programming-9/when-p-revents-and-pollin-will-be-true-693993/)

vaclinux 12-31-2008 12:11 AM

When ( p.revents & POLLIN ) will be true ?
 
Hallo,
I have a part of code that i dont understand yet. I have googled around but no good result that can help me to understand.
Below is a snapshot of the code,
This program should send a packet to a server, then received the respond from the server.

Code:

struct pollfd p;


send(s,packet,size,0);  // send the packet
for(;;){
p.fd = s;
p.events = POLLIN;
 if (poll(&p,1,100) <= 0) continue;
 while (p.revents & POLLIN) {
  if (recv(s,response,sizeof response,0) == sizeof response) { // receive the packet
  }
 if (poll(&p,1,0) <= 0) break;[/PHP]
 }
}

My question is What "while (p.revents & POLLIN) & if (poll(&p,1,100) <= 0)" line does? When the condition will be true ?

Thanks for any help,

rameshandram 12-31-2008 03:05 AM

monitoring POLLIN bit, if it is on , will receive the response from the server .

vaclinux 12-31-2008 10:52 PM

Thanks to the reply,
Okay, that pop up me another question,
When the POLLING bit, will be set to ON?
Thanks again,

wje_lq 01-01-2009 12:05 AM

The code that you're looking at is not a complete program. It's a small bit of what is probably a much larger program. You're going to have to study that larger program to determine the answer to your question.

vaclinux 01-01-2009 08:56 PM

Yes, you are correct Mr.wje_lq,and i have explained in my first post of this thread.
I cant post all source code here, there around 3-4 different source code file.
What do you think make polling bit set 1? give me any kind condition when it can be happen(polling bit set 1), later i will try to understand by myself.

My understanding so far ...
Is it waiting to receive the whole packet from server with out any interruption, if it receive the whole data the polling set 1, means can b interrupted(break the loop?)?
Correct me, if it is wrong.

Thanks your reply btw

paulsm4 01-01-2009 09:10 PM

You're "kind of" correct.

If you use normal, blocking I/O, the OS won't let you proceed until the descriptor you're using is ready.

If you use "asynchronous I/O", then you need to manage this yourself. "poll()" is one way to see if a descriptor is ready to be used (otherwise, the I/O you initiated is still pending).

Another, different way to look at it is that poll lets you check if any one of *several* different devices (for example, the terminal or a socket) has I/O (from a previous read) available.

Here's a good introduction, you can find many more (Google for stuff like "Linux programming asynchronous I/O poll select"):

http://www.ibm.com/developerworks/ai...rm1/index.html

zaki Rahat 03-20-2016 01:42 AM

I am also having problem in this code. Could you explain this please?
 
p.fd = s;
p.events = POLLIN;
if (poll(&p,1,100) <= 0) continue;
while (p.revents & POLLIN)/*monitoring POLLIN bit, if it is on , will receive t$
{
if (recv(s,response,sizeof response,0) == sizeof response) {
if (!memcmp(packet,response,16)) {

NevemTeve 03-20-2016 06:18 AM

Please use [code] and [/code] tags. This code is an anti-example, it shows you what to avoid.

pseudo-code:
Code:

char *msgp= response;
size_t restlen= sizeof response;

...
poll
...

if (p.revents & POLLIN) {
    int err= 0, eof= 0, nomoredata= 0;

    while (!err && !eof && !nomoredata && restlen>0) {
        ssize_t retlen= recv (s, msgp, restlen, 0);
        if (retlen<0) {
            int ern= errno;
            if (ern==EWOULDBLOCK) {
                nomoredata= 1;
            } else {
                /* handle the error*/
                err= ern;
            }

        } else if (retlen==0) {
            /* handle the connection-loss */
            eof= 1;

        } else {
            msgp += retlen;
            restlen -= retlen;
        }
    }
}
if (err || eof) {
    close (s);
} else if (restlen==0) {
    /* complete message has been read */
} else {
    /* repeat 'poll' */
}


John VV 03-20-2016 12:00 PM

two different people with the same code fragment ????

am i right in guessing that this is part of a homework assignment

but 8 years apart ?

sundialsvcs 03-22-2016 08:55 AM

Strictly speaking, the & operator represents bitwise logical-AND.

p.revents is the variable being tested, and POLLIN is probably a #define which corresponds to some fixed integer.

Let's say, for the sake of example, that POLLIN corresponds to the value 0x02, binary 00000010. If we perform a "bitwise logical-AND" against this value, the result will be nonzero [i](true)[/font] if this bit is equal to 1 (in p.revents ...); otherwise it will be zero (false).

Notice that only the value of bit #2 is being tested, since only this bit (in the mask, POLLIN) is equal to "1." All other bits will be ignored, because "anything AND zero" always equals zero.

Therefore, this statement tests whether the #2 bit (for example ...), in p.revents, is set to "1."

Q.E.D.

- - - -
Notice that the && operator is not at all(!) the same thing. "Why it is different," I now leave as an exercise to the reader.

slackerz 05-22-2023 12:11 PM

Why do you feel the need to guess?

Code:

POLLIN
actually corresponds to
Code:

0x001
in modern colloquial UNIX.


All times are GMT -5. The time now is 02:48 PM.