LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
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 04-24-2009, 12:38 PM   #1
mjdubois
LQ Newbie
 
Registered: Apr 2009
Posts: 1

Rep: Reputation: 0
Smile Select and blocking I/O


I am using recvfrom with UDP and recv with TCP.

Using blocking, what is the difference between calling
select() 100 times with a 1ms timeout
vs calling select() one time with a 100ms timeout?

What exactly does the operating system do during the select?
How expensive is it to repeatedly call select?
Will other processes be able to run while we are blocking?
Thanks for any and all help.
 
Old 04-25-2009, 08:52 AM   #2
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
Quote:
Originally Posted by mjdubois View Post
Using blocking, what is the difference between calling
select() 100 times with a 1ms timeout
vs calling select() one time with a 100ms timeout?
Calling select() more often with shorter times will give your program more often the opportunity to do other things at the cost of some overhead of calling select() and checking for activity on file descriptors more often.

Quote:
Originally Posted by mjdubois View Post
What exactly does the operating system do during the select?
The same things it would do if your program is not running. Since you program a sleeping in select() the OS can forget about your program for the time being... It just has registered the filedescriptors you want the OS to watch for you. When a IP packet for your program arrives, the network interface will issue an hardware interrupt (IRQ). The kernel module (driver) for the network interface gets the raw network data from the packet, passes it to the TCP/IP modules in the the kernel and figures out to which program/process it should go. And it will find out it is your program. The kernel notices your process is sleeping in select(), then checks if the filedescriptor for the incoming packet is in the set of descriptors you gave to select() to watch. If so, the kernel will mark it and return from select(), thus awakening your program.

Quote:
Originally Posted by mjdubois View Post
Will other processes be able to run while we are blocking?
Sure! UNIX/Linux is a multitasking OS. Your program is asleep in select() so no better time than this to run other processes.

Last edited by Hko; 04-25-2009 at 08:54 AM.
 
Old 04-25-2009, 02:05 PM   #3
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
"select()" with *any* timeout == "polling"...
... and Polling as Bad.

Unavoidable sometimes, but seldom ideal.

The best reason to use "select()" is if you're waiting for I/O on any of several sockets and/or file descriptors. If input arrives: you act on it (regardless of which file initiated the input). Otherwise, you block.

If you want to wait for input *and* you want to do work at the same time, use threads.

IMHO .. PSM
 
Old 04-26-2009, 06:10 AM   #4
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
Quote:
Originally Posted by paulsm4 View Post
"select()" with *any* timeout == "polling"...
... and Polling as Bad.

Unavoidable sometimes, but seldom ideal.
Good point. Quoting man select_tut:
Code:
  1.  You  should  always  try to use select() without a
      timeout.  Your program should have nothing to do if
      there is no data available.  Code that depends on
      timeouts is not usually portable and is difficult
      to debug.
Quote:
Originally Posted by paulsm4 View Post
If you want to wait for input *and* you want to do work at the same time, use threads.

IMHO .. PSM
I don't agree with that. I'd say: never use threads unless...

Coding a server program that needs to handle multiple connections (sockets) simultaneously, select() is the only feasible way to do it sequentially (i.e. within one process, one thread) which is arguably a very elegant way.

Another way is to fork off a seperate process for each connection.

Threads is third way to handle multiple connection at the same time. Threads may probably the way to squeeze out the last bits of performance from the program. But to get that last bit of performance out of the "threads-method" you really need 'to do it right'. And coding a threaded program can be very difficult to do right. It is the most error-prone way. You really need to know what you're doing in order not to create hard-to-find-bugs, security-holes and race conditions.

When you got all that right, would you still have that last bit of performance gain from it? Maybe, but is it worth the trouble?

There are people much more knowledgable than me suggesting the computing world may even be better of without threads alltogether:
Threads considered harmful
The problem with threads
 
Old 04-26-2009, 10:11 AM   #5
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
I recently developed an thread that uses select() with a timeout. If activity is detected, then I handle the incoming message(s) from the socket. If activity is detected and/or the timeout period expires, I also do something else.

The timeout period chosen was based on how often I should do the "something else". Receiving from the socket was an ancillary task. By using the select(), I was able to "kill two birds with one stone", and more importantly control how often the "something else" is done.

I'm certain that there are other ways to accomplish what I have done, but what I did is not wrong, or bad.

P.S. I rarely expect messages from the socket, so in essence the select() is being used as a timer.
 
  


Reply



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
select in vi adnanm Linux - Newbie 5 06-02-2007 05:36 AM
How does a blocking select return (kernel)? iw05t Linux - Newbie 1 11-09-2006 03:52 PM
select() Kroenecker Programming 1 03-12-2006 01:01 AM
Select() did not select my socket thvo Programming 1 05-08-2005 12:20 AM
what os to select kevinasr Linux - Newbie 2 05-18-2003 06:52 AM

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

All times are GMT -5. The time now is 11:15 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