LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-31-2008, 12:11 AM   #1
vaclinux
LQ Newbie
 
Registered: Dec 2008
Posts: 5

Rep: Reputation: 0
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,
 
Old 12-31-2008, 03:05 AM   #2
rameshandram
LQ Newbie
 
Registered: Jul 2008
Posts: 1

Rep: Reputation: 0
monitoring POLLIN bit, if it is on , will receive the response from the server .
 
Old 12-31-2008, 10:52 PM   #3
vaclinux
LQ Newbie
 
Registered: Dec 2008
Posts: 5

Original Poster
Rep: Reputation: 0
Thanks to the reply,
Okay, that pop up me another question,
When the POLLING bit, will be set to ON?
Thanks again,
 
Old 01-01-2009, 12:05 AM   #4
wje_lq
Member
 
Registered: Sep 2007
Location: Mariposa
Distribution: FreeBSD,Debian wheezy
Posts: 811

Rep: Reputation: 179Reputation: 179
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.
 
Old 01-01-2009, 08:56 PM   #5
vaclinux
LQ Newbie
 
Registered: Dec 2008
Posts: 5

Original Poster
Rep: Reputation: 0
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
 
Old 01-01-2009, 09:10 PM   #6
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
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
 
Old 03-20-2016, 01:42 AM   #7
zaki Rahat
LQ Newbie
 
Registered: Mar 2016
Posts: 3

Rep: Reputation: Disabled
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)) {
 
Old 03-20-2016, 06:18 AM   #8
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,923
Blog Entries: 1

Rep: Reputation: 1885Reputation: 1885Reputation: 1885Reputation: 1885Reputation: 1885Reputation: 1885Reputation: 1885Reputation: 1885Reputation: 1885Reputation: 1885Reputation: 1885
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' */
}

Last edited by NevemTeve; 03-20-2016 at 02:27 PM.
 
Old 03-20-2016, 12:00 PM   #9
John VV
LQ Muse
 
Registered: Aug 2005
Location: A2 area Mi.
Posts: 17,635

Rep: Reputation: 2652Reputation: 2652Reputation: 2652Reputation: 2652Reputation: 2652Reputation: 2652Reputation: 2652Reputation: 2652Reputation: 2652Reputation: 2652Reputation: 2652
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 ?

Last edited by John VV; 03-20-2016 at 12:01 PM.
 
1 members found this post helpful.
Old 03-22-2016, 08:55 AM   #10
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,815
Blog Entries: 4

Rep: Reputation: 3981Reputation: 3981Reputation: 3981Reputation: 3981Reputation: 3981Reputation: 3981Reputation: 3981Reputation: 3981Reputation: 3981Reputation: 3981Reputation: 3981
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.
 
Old 05-22-2023, 12:11 PM   #11
slackerz
Member
 
Registered: Aug 2020
Posts: 90

Rep: Reputation: 31
Why do you feel the need to guess?

Code:
POLLIN
actually corresponds to
Code:
0x001
in modern colloquial UNIX.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
'ps' command - true meaning of 'pcpu' & 'time' values in a multi-CPU environment MarkTurbo Linux - Newbie 5 02-02-2008 04:48 AM
how to stop "while true; do nohup application; sleep 10; done &" michaeljoser Linux - Software 4 10-09-2007 07:39 AM
Ph&#7909;c h&#7891;i d&#7919; li&#7879;u b&#7883; m&#7845;t???, c&#7913; pollsite General 1 06-27-2005 12:39 PM
xine-lib trouble & more misc questions...-- a samaritan expert's dream come true bandofmercy Linux - Newbie 6 03-01-2003 07:24 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:33 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration