Multiple Read File Descriptors

This is a discussion on Multiple Read File Descriptors within the C Programming forums, part of the General Programming Boards category; Hi, I'm working on a project that requires listening and processing messages from multiple external sources. My application is written ...

  1. #1
    Registered User
    Join Date
    Nov 2003
    Posts
    12

    Arrow Multiple Read File Descriptors

    Hi,

    I'm working on a project that requires listening and processing messages from multiple external sources.

    My application is written in C and running on a Linux Read Hat 9 machine.

    I have successfully connected to one external source, but have a questions on how to connect and listen to more than one source without blocking each other.

    Generally, how I connect to a source is like this:

    Code:
    fd_set rd1;
    int hdle1 = setup(.....); //setup is a function in an API I'm using
    
    FD_ZERO(&rd1);
    FD_SET(hdle1, &rd1);
    
    select(hdle+1, &rd1, NULL, NULL, NULL); //waiting
    My question is, if I have have 2 hdles and use FD_SET() on rd1, will I be able to detect incoming data from both sources?

    if so, will it create blocking on one of the sources if no data has been detected from the other source? (waiting on one source while the other one has data)

    i.e.
    Code:
    fd_set rd1;
    int hdle1 = setup(.....); //set up first source
    int hdle2 = setup(.....); //set up second source
    
    FD_ZERO(&rd1);
    FD_SET(hdle1, &rd1);  
    FD_SET(hdle2, &rd1);
    
    select(hdle+2, &rd1, NULL, NULL, NULL); //waiting
    Thanks in advance for your help! Any input is appreciated!

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,630
    You'll get alot more responses in the "C Programming" forum...

    Perhaps a PM will be kind enough to move your thread for you.

    gg

  3. #3
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    472
    This is the c programming forum isnt it?

  4. #4
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,630
    See! The responses are picking up already!

    Dohhh!

    gg

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,422
    Close
    Code:
    fd_set rd1;
    int hdle1 = setup(.....); //set up first source
    int hdle2 = setup(.....); //set up second source
    int max = hdle1 > hdle2 ? hdle1 : hdle2; 
    int result;
    
    FD_ZERO(&rd1);
    FD_SET(hdle1, &rd1);  
    FD_SET(hdle2, &rd1);
    result = select(max+1, &rd1, NULL, NULL, NULL); //waiting 
    
    if ( FD_ISSET(hdle1, &rd1) ) {
      // go read from hdle1
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    Registered User
    Join Date
    Nov 2003
    Posts
    12

    Arrow

    Hi, Thanks for your help.

    I tried to follow your advice, but I came across something strange.

    Code:
    fd_set rd1;
    int hdle1 = setup(.....); //set up first source
    int hdle2 = setup(.....); //set up second source
    int max = hdle1 > hdle2 ? hdle1 : hdle2; 
    int result;
    
    for(;;) {
    FD_ZERO(&rd1);
    FD_SET(hdle1, &rd1);  
    FD_SET(hdle2, &rd1);
    result = select(max+1, &rd1, NULL, NULL, NULL); //waiting 
    
    if ( FD_ISSET(hdle1, &rd1) ) {
      // go read from hdle1
    
    }
    }
    Instead of waiting at the select statement for any incoming messages, it just goes in an infinite loop. What should happen is that each loop occurs whenever there is something on the select, but right now, the select is looping even though there is no incoming messages.

    I printed out the result variable:
    loop 1, result = 1
    loop 2, result = 2
    loop 3, result = 1
    loop 4, result = 1
    loop 5, result = 1 .......

    Does anyone know what is happening?

  7. #7
    Obsessed with C chrismiceli's Avatar
    Join Date
    Jan 2003
    Posts
    501
    If select is returning 1, then there is something that needs to be read, hdle2 apparently has something that needs to be read and you are not getting it if that is all of your code. Your loop is infinite because you never break;. You need to FD_ISSET(hdle2, &rd1).
    Last edited by chrismiceli; 04-21-2004 at 10:26 AM.
    Help populate a c/c++ help irc channel
    server: irc://irc.efnet.net
    channel: #c

  8. #8
    Registered User
    Join Date
    Nov 2003
    Posts
    12
    Thanks, I got rid of the infinite loop problem....

    but it seems like my program is only detecting a read on one of the file descriptors.

    Code:
    FD_ZERO(&rd1);
    FD_SET(hdle1, &rd1);  
    FD_SET(hdle2, &rd1);
    
    for (;;) {
         result = select(max+1, &rd1, NULL, NULL, NULL); //waiting 
    
         if (FD_ISSET(hdle1, &rd1) ) {
          .......
        }
    
         if (FD_ISSET(hdle2, &rd1)) {
          .......
        }
    }
    it is only detecting a read message in hdle2, even though there should be a message from hdle1. It seems like that the program only detects the last handle that is set, e.g. if I switch the FD_SET's around, it will only detect a read message in hdle1.

    Is there a parameter that I should set? (I thought that the default number of FDs was 64)

  9. #9
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,422
    FD_ZERO(&rd1);
    FD_SET(hdle1, &rd1);
    FD_SET(hdle2, &rd1);
    Put these inside the loop, before you call select()

    rd1 is modified by the select call to indicate what is available for reading - that's how the ISSET works.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  10. #10
    Registered User
    Join Date
    Nov 2003
    Posts
    12
    That was the problem!!! Thanks for all your help!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File transfer- the file sometimes not full transferred
    By shu_fei86 in forum C# Programming
    Replies: 13
    Last Post: 03-13-2009, 12:44 PM
  2. read multiple lines from a file
    By YankeePride13 in forum C Programming
    Replies: 2
    Last Post: 11-10-2005, 09:30 PM
  3. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 09:46 AM
  4. archive format
    By Nor in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 08-05-2003, 07:01 PM
  5. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21