LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 02-09-2006, 01:35 PM   #1
estratos
Member
 
Registered: Jan 2006
Distribution: Ubuntu
Posts: 85

Rep: Reputation: 15
recvfrom in its own thread?


I'm developing a multithreaded application that should listen to a specific port over UDP. For listening, I'm considering these two possibilities:

1. I place recvfrom in its own thread. This thread remains blocked on recvfrom until a packet arrives. The rest of the threads continue working.
2. I don't place recvfrom in its own thread. I use select in order to detect incoming packets.

Which choice do you prefer? Which option would consume less CPU while waiting for incoming packets?

Thanks.
 
Old 02-09-2006, 04:29 PM   #2
Mara
Moderator
 
Registered: Feb 2002
Location: Grenoble
Distribution: Debian
Posts: 9,696

Rep: Reputation: 232Reputation: 232Reputation: 232
Blocking recvfrom is more or less equal to select. When there's no data, the process sleeps. It's woken up when data arives.

The thing with multi-threaded version is that you need to make sure they're not waiting for the data (from recvfrom thread) actively (waiting in for or while) -- that takes processor time.

In general, separate recvfrom thread is not a very effective solution, because you need to pass the data to the other threads. On the other hand, if there's much processing done in each of the other threads, it's worth considering. All depends on the program you're writing.
 
Old 02-09-2006, 06:15 PM   #3
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
Quote:
Originally Posted by estratos
I'm developing a multithreaded application that should listen to a specific port over UDP. For listening, I'm considering these two possibilities:

1. I place recvfrom in its own thread. This thread remains blocked on recvfrom until a packet arrives. The rest of the threads continue working.
2. I don't place recvfrom in its own thread. I use select in order to detect incoming packets.

Which choice do you prefer? Which option would consume less CPU while waiting for incoming packets?

Thanks.
Quote:
Blocking recvfrom is more or less equal to select. When there's no data, the process sleeps. It's woken up when data arives.
This is not true.

As Mara suggested there is a danger with blocking sockets and thats processor time, but theres a major difference between using select and blocking on recvfrom. Having a thread block waiting for a package will probably max out the cpu and using O_NONBLOCK and constantly polling is not a good idea; while using select on a socket is much more cpu friendly and also allows you to give a time out which can be beneficial.

Last edited by dmail; 02-09-2006 at 06:26 PM.
 
Old 02-10-2006, 02:24 AM   #4
estratos
Member
 
Registered: Jan 2006
Distribution: Ubuntu
Posts: 85

Original Poster
Rep: Reputation: 15
recvfrom blocked thread vs select

The main thread should never wait the "recvfrom thread" for a response. This last thread modifies some global variables once a packet is received, while the main thread is working on other tasks. Anyway, we are talking about CPU time and you say that the thread configuration sucks a little more CPU than a loop over select.

If I use select, I could reconsider my program topology. My program comunicates over two rs232 ports and UDP. Using select I could inspect these three file descriptors at the same time. On the other hand, with threads my program remains cleaner and more modular. Every thread does its own job.

Any other opinion or do I take dmail's and Mara's as definitive?

Thanks a lot guys.
 
Old 02-12-2006, 04:05 PM   #5
Mara
Moderator
 
Registered: Feb 2002
Location: Grenoble
Distribution: Debian
Posts: 9,696

Rep: Reputation: 232Reputation: 232Reputation: 232
The number of threads is critial here. With 100, performance drops and it's clear to see (BTW when writing about threads I meant thread+select inside or thread+blocking operations). In your case, choose what's easier to implement. Threaded solution would not have a big performance drop, but think how to wake them when the packets arrived (a loop waiting for a variable to change is not a good idea).
 
Old 02-16-2006, 03:46 AM   #6
estratos
Member
 
Registered: Jan 2006
Distribution: Ubuntu
Posts: 85

Original Poster
Rep: Reputation: 15
Mara, I don't like either the idea of looping while waitng a variable to change. But, is not that the same as looping around a select()?
 
Old 02-16-2006, 04:23 PM   #7
Mara
Moderator
 
Registered: Feb 2002
Location: Grenoble
Distribution: Debian
Posts: 9,696

Rep: Reputation: 232Reputation: 232Reputation: 232
No. The thing is that select (with rather high timeout set) is handled by the system. The process is asleep and it doesn't run until something interesting happens.
 
Old 02-17-2006, 01:28 AM   #8
estratos
Member
 
Registered: Jan 2006
Distribution: Ubuntu
Posts: 85

Original Poster
Rep: Reputation: 15
OK. I understand that with a high timeout select is a better solution.

I've read somewhere that most linux systems can't manage timeouts smaller than 100 ms. Is this true?
 
Old 02-17-2006, 05:13 PM   #9
Mara
Moderator
 
Registered: Feb 2002
Location: Grenoble
Distribution: Debian
Posts: 9,696

Rep: Reputation: 232Reputation: 232Reputation: 232
It manages. The question is *when* the call will return. It may be later then the timeout says. It all depends on the kernel, through. In most (?) cases time interrupt comes 100 times per second. Also, your process may not be the one woken up. Timeout passed to select is more or less the time that will pass until the call returns (if there's no other event), I wouldn't count on it if you'd like to have precise measurement.
 
Old 02-18-2006, 03:26 AM   #10
estratos
Member
 
Registered: Jan 2006
Distribution: Ubuntu
Posts: 85

Original Poster
Rep: Reputation: 15
Thank you very much.
 
  


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
Why does a multithreaded program gets hanged at recvfrom()? kranti Programming 1 10-17-2005 10:44 AM
Why does a multithreaded program gets hanged at recvfrom()? kranti Linux - General 1 10-17-2005 04:09 AM
problem with recvfrom() in linux kernel .4.21-15.EL?? nkshirsagar Red Hat 0 06-27-2005 09:59 AM
Problem in recvfrom() live_dont_exist Programming 5 05-08-2005 04:25 PM
recvfrom() takes 20 ms to return? pingswept Programming 3 05-25-2004 03:18 AM

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

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