LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
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
 
LinkBack Search this Thread
Old 02-20-2005, 09:26 PM   #1
jwstric2
Member
 
Registered: Jan 2004
Posts: 105

Rep: Reputation: 15
select multiple with pthreads


I've got a thread that is doing a select() on a set of sockets and waiting for incoming requests/other communication and one thread that is handling all data transfers needed (also in a select). Is there a way for a thread to force the data listening thread to drop out of his select so I can add the incoming file descriptors?

Thanks in advance
 
Old 02-21-2005, 03:26 AM   #2
Mara
Moderator
 
Registered: Feb 2002
Location: Grenoble
Distribution: Debian
Posts: 9,527

Rep: Reputation: 147Reputation: 147
You can set maximum time select() is listening. When the time passes, it stops and you can do all pending actions and then run select again. In fact most programs work this way. The only thing is to set the time in select to fit your program.

Code:
int select(int n, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout);
The interesting parameters is the last one - timeout.
 
Old 02-21-2005, 01:28 PM   #3
jwstric2
Member
 
Registered: Jan 2004
Posts: 105

Original Poster
Rep: Reputation: 15
What is the proper way to combine two fd_set into one. Suppose I have fd_set for data connections and an fd_set for transfers? How can I combined these into a single fd_set for calling select on.
 
Old 02-21-2005, 03:47 PM   #4
Mara
Moderator
 
Registered: Feb 2002
Location: Grenoble
Distribution: Debian
Posts: 9,527

Rep: Reputation: 147Reputation: 147
There's no single function, but the process is not that hard.
Code:
int max; /* initalize with last created socket+1 */
fd_set my_set; /* combination */
fd_set connections;
fd_set tranfers;
int i;

FD_ZERO(&my_set);
for(i=0; i< max; ++i){
    if(FD_ISSET(i,&connections)) FD_SET(i,&my_set);
    if(FD_ISSET(i,&transfers)) FD_SET(i,&my_set);
}
 
Old 02-24-2005, 01:27 PM   #5
jwstric2
Member
 
Registered: Jan 2004
Posts: 105

Original Poster
Rep: Reputation: 15
Thanks, this works well. I've actually designed it with a communication thread and data thread with seperate fd_sets. Problem now seems to be with pthreads. If I run the servicing thread alone I get the following information.

WITH THREAD:

SOCK FOR TCP IS 4
SET SOCK IN FD SET 4
SOCFD IS SET 1
Starting run thread
In run thread
HERE BEFORE SELECT FD_ISSET 4 IS 0



WITHOUT THREAD, STANDALONE COMMUNICATION METHOD:

SOCK FOR TCP IS 4
SET SOCK IN FD SET 4
SOCFD IS SET 1
HERE BEFORE SELECT FD_ISSET 4 IS 1

Its almost as if the fd_set is not being shared, which is just weird.

Im about to do some debugging of this but if anyone has an idea that would be helpful I'd appreciate it. The communcation library is a static build and pthreads are being used with gcc compiler just for a little more info.

Last edited by jwstric2; 02-24-2005 at 01:35 PM.
 
Old 02-24-2005, 03:10 PM   #6
Mara
Moderator
 
Registered: Feb 2002
Location: Grenoble
Distribution: Debian
Posts: 9,527

Rep: Reputation: 147Reputation: 147
Hard to say without seeing the code. COuld you please paste the fragment from mergind fd_sets to the select?
 
  


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
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Pthreads a12ctic Linux - Software 1 06-09-2005 10:05 PM
Select() did not select my socket thvo Programming 1 05-08-2005 12:20 AM
Select problem with multiple listeners Fruitbat Programming 3 04-29-2005 12:24 PM
pthreads socket9001 Programming 2 12-29-2003 12:23 AM
pthreads h/w Programming 5 10-09-2003 11:11 AM


All times are GMT -5. The time now is 07:52 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration