LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 06-30-2008, 06:15 AM   #1
turnmeout
LQ Newbie
 
Registered: Feb 2008
Posts: 12

Rep: Reputation: 0
Thumbs up select command


I have a doubt in select command,

select program:
highsock is an global variable
while(1) {
FD_ZERO (&socks);
FD_SET (highsock, socks);
readsocks = select(highsock+1, &socks, 0, 0, &timeout);
if (readsocks < 0) {
perror("select");
exit(EXIT_FAILURE);
}
if (readsocks == 0) {
/* Nothing ready to read, just show that
we're alive */
printf(".");
fflush(stdout);
} else
read_socks(readsocks);
}

void read_socks(int readsocks) {
int listnum;
if (FD_ISSET(highsock,&socks))
handle_new_connection();
else
perror("No data available :%d\n", readsocks);
}

In our case selects returns with total number of socket descriptors as 38 and also while checking with FD_ISSET it gets failed with error as
No data available :38

My question is
1. Whether select can return such more number of file descriptors ?
2. How this type of errors can be handled to avoid error?

Last edited by turnmeout; 06-30-2008 at 10:25 PM.
 
Old 06-30-2008, 05:39 PM   #2
pokemaster
Member
 
Registered: Apr 2005
Location: Massachusetts, USA
Distribution: debian,ubuntu,slackware
Posts: 110

Rep: Reputation: 17
I don't see enough data here to help you out. Where are socks and highsock declared? Where is highsock initialized? Where is the while loop running? Is socks a global variable? Else, how does read_socks fit into the scope?

A bigger code listing may be in order. I've reposted it below to clarify indentations & such in your code.
Quote:
I have a doubt in select command,
select program:
Code:
while(1) {
    FD_ZERO (&socks);
    FD_SET (highsock, socks);
    readsocks = select(highsock+1, &socks, 0, 0, &timeout);
    if (readsocks < 0) {
        perror("select");
        exit(EXIT_FAILURE);
    }
    if (readsocks == 0) {
        /* Nothing ready to read, just show that
        we're alive */
        printf(".");
        fflush(stdout);
    } else
        read_socks(readsocks);
}

void read_socks(int readsocks) {
    int listnum;
    if (FD_ISSET(sock,&socks))
        handle_new_connection();
    else
        perror("No data available :%d\n", readsocks);
}
In our case selects returns with total number of socket descriptors as 38 and also while checking with FD_ISSET it gets failed with error as
No data available :38

My question is
1. Whether select can return such more number of file descriptors ?
2. How this type of errors can be handled to avoid error?
 
Old 06-30-2008, 06:08 PM   #3
pinniped
Senior Member
 
Registered: May 2008
Location: planet earth
Distribution: Debian
Posts: 1,732

Rep: Reputation: 50
If you have 38 file descriptors, then those are 0 .. 37 - there is no '38'.
 
Old 06-30-2008, 07:00 PM   #4
pokemaster
Member
 
Registered: Apr 2005
Location: Massachusetts, USA
Distribution: debian,ubuntu,slackware
Posts: 110

Rep: Reputation: 17
I think you need to change your FD_ISSET call to be FD_ISSET(readsocks,&socks);

Am I right?
 
Old 07-03-2008, 12:17 AM   #5
turnmeout
LQ Newbie
 
Registered: Feb 2008
Posts: 12

Original Poster
Rep: Reputation: 0
Pokemaster,
I have reedited the code once again and posted in the same way.
Can you now provide us the answer for this?
 
Old 07-03-2008, 06:52 AM   #6
pokemaster
Member
 
Registered: Apr 2005
Location: Massachusetts, USA
Distribution: debian,ubuntu,slackware
Posts: 110

Rep: Reputation: 17
I'm making a few assumptions here, and doing the best I can. Here is a full code listing, one that actually works all on its own. Open two terminal windows in the same directory. Run
Code:
mkfifo tmpfile
, paste the code below into a c file, compile it, then run the program. In the second window, once the program is running, type
Code:
echo > tmpfile
and see what turns up in the first window. repeat this command as many times as you like, it will keep working the same way.

The problem with your post is that you have given us the teeniest window into your program and, to be honest, I cannot see where the bug lies (especially since I cannot compile what you've given us). Next time, post all the related code so we can help. If you cannot post it all because this is schoolwork, then go ask your teacher. If it is work related and covered under an NDA, hire a consultant to help you out; go ask an expert who will also sign an NDA. Don't ask in a public forum. If nothing is holding you back from posting all the code, then post it all.

Here's a working select program, give it a try as illustrated.
Code:
#include <sys/time.h>
#include <stdio.h>
#include <fcntl.h>
#include <stdlib.h>

int highsock;
fd_set socks;
void read_socks(int readsocks);

int main() {
        struct timeval timeout;
        int readsocks;
        highsock = open("tmpfile",O_RDONLY);
        printf ("Opened file %d\n", highsock);
        timeout.tv_sec = 1;
        timeout.tv_usec = 0;

        while(1) {
                FD_ZERO (&socks);
                FD_SET ( highsock, &socks);
                readsocks = select(highsock+1, &socks, 0, 0, &timeout);
                if (readsocks < 0) {
                        perror("select");
                        exit(EXIT_FAILURE);
                }
                if (readsocks == 0) {
                        /* Nothing ready to read, just show that
                        we're alive */
                        printf(".");
                        fflush(stdout);
                } else {
                        read_socks(readsocks);
                }
        }
}

void handle_new_connection() {
        char buf[80];
        buf[79] = '\0';
        read(highsock,buf,79);
        printf("Connection handled!\n");
}

void read_socks(int readsocks) {
        int listnum;
        char pbuf[80];
        if (FD_ISSET(highsock,&socks)) {
                handle_new_connection();
        } else {
                sprintf(pbuf,"No data available :%d\n",readsocks);
                perror(pbuf);
        }
}
 
  


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



Similar Threads
Thread Thread Starter Forum Replies Last Post
select all command in vi nagabioinfo Linux - Newbie 13 11-26-2007 09:52 AM
Copy select files with command line Storyman Linux - Newbie 5 01-23-2007 04:35 AM
MySQL select command denied to user error lsimon4180 Linux - Software 2 03-03-2005 07:34 PM
Using escape string error in MySQL select command, any one can help me? myunicom Linux - General 2 02-16-2004 10:20 PM
select command slack66 Linux - General 3 07-12-2003 03:10 AM


All times are GMT -5. The time now is 10:10 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration