Find the answer to your Linux question:
Results 1 to 6 of 6
Hello all! Okay, I'm completely stumped on this one! I have a program that uses select to monitor a couple of file descriptors (nothing odd there right?) One file descriptor ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Apr 2005
    Posts
    7

    Weird problem with select


    Hello all!

    Okay, I'm completely stumped on this one! I have a program that uses select to monitor a couple of file descriptors (nothing odd there right?) One file descriptor is used for reading, the other for writing. The following code illustrates what I am doing and for me at least, reproduces the problem I am experiencing:

    Code:
    #include <stdlib.h>
    #include <stdio.h>
    
    #include <unistd.h>
    #include <sys/time.h>
    #include <sys/types.h>
    
    
    int main&#40;int argc, char* argv&#91;&#93;&#41;
    	&#123;
    	fd_set readSet;
    	fd_set writeSet;
    	int result;
    	char c;
    	
    	FD_ZERO&#40;&readSet&#41;;
    	FD_ZERO&#40;&writeSet&#41;;
    	FD_SET&#40;STDIN_FILENO, &readSet&#41;;
    	FD_SET&#40;STDOUT_FILENO, &writeSet&#41;;
    	
    	while&#40;1&#41;
    		&#123;
    		result = select&#40;FD_SETSIZE, &readSet, &writeSet, NULL, NULL&#41;;
    		if &#40;result>0&#41;
    			&#123;
    			if &#40;FD_ISSET&#40;STDIN_FILENO, &readSet&#41;&#41;
    				&#123;
    
    				read&#40;STDIN_FILENO, &c, 1&#41;;
    				printf&#40;"%c\n", c&#41;;
    				fflush&#40;stdout&#41;;
    				&#125;
    			if &#40;FD_ISSET&#40;STDOUT_FILENO, &writeSet&#41;&#41;
    				&#123;
    				printf&#40;"test"&#41;;
    				fflush&#40;stdout&#41;;
    				FD_CLR&#40;STDOUT_FILENO, &writeSet&#41;;
    				&#125;
    			
    			&#125;
    		&#125;
    
    	return EXIT_SUCCESS;
    	&#125;
    Now I would expect the output of this program to be:
    Code:
    test
    If I type 'hello' followed by return I would expect to see:
    Code:
    h
    e
    l
    l
    o
    ...but instead, nothing happens! Why is this? I have not removed STDIN_FILENO from readSet. Thanks.

    Matt.

  2. #2
    Linux Enthusiast
    Join Date
    Jan 2005
    Posts
    575
    I have tried your programme both on a Linux box and a Solaris box
    and it prints test but the
    if (FD_ISSET(STDIN_FILENO, &readSet))

    test seems to always be false.I assume that's because the first time
    select gets called there's nothing to read so STDIN_FILENO
    gets removed from readSet.I tried putting a
    FD_SET(STDIN_FILENO, &readSet);
    just before the end of the while loop and it works fine on both systems.
    Well actually there is something I don't understand: if I type
    abc followed by ENTER I get the three letters each on one line followed
    by two newlines while I was expecting one.Apart from that
    everything else seems to be working according to specifications as far as I can see.

  3. #3
    Linux Enthusiast
    Join Date
    Jan 2005
    Posts
    575
    Oh , never mind I get it now.

  4. $spacer_open
    $spacer_close
  5. #4
    Just Joined!
    Join Date
    Apr 2005
    Posts
    7
    Thanks. Got the problem sorted now. One more weird problem though. If I use a timeval structure to specify a timeout for select, select will return immediately if I use a time interval less than one second (e.g. tv_sec=0, tv_usec=500). What's that all about!!?!? I've seen select used for generating sub-second delays, so surely it should work?

    Matt.

  6. #5
    Linux Enthusiast
    Join Date
    Jan 2005
    Posts
    575
    With the example you give you ask select to wait for 500
    millionths of a second.That will always seem "immediately"
    to a human.Not to mention that on many machines it won't
    be accurate to the microsecond.

  7. #6
    Just Joined!
    Join Date
    Apr 2005
    Posts
    7
    Doh! Of course, I was thinking milliseconds, not microseconds. Clearly my brain was not in gear!

    Thanks.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •