ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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 ?
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.
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.
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"):
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)) {
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.