Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6699651
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T06:43:14+00:00 2026-05-26T06:43:14+00:00

I’ve got a program that I’m working on and I use select to choose

  • 0

I’ve got a program that I’m working on and I use select to choose from the pipes to read.

The problem is that I’m using if(FD_ISSET(pfd[i][0], &read_set)) and it fails the verification because FD_ISSET is returning 0, but if I try to read using read(pfd[i][0],&c,sizeof(c)) it will work fine and the contents from pipe pfd[0] are different from pfd[1] like I want.

I want to know what the hell is going on because FD_ISSET returns 0 but I can actually read content from it

EDIT

The global variable CORES will control how much processes I create
The next code will create the pipes and use FD_SET to set the bits

for(i=0; i<CORES; i++)
{
    if(pipe(pfd[i])==-1)
        perror("Ocorreu um erro a criar um pipe");

    FD_SET(pfd[i][0], &read_set);
}

read_set is defined right after main() starts. I use for() right after the above code to create x processes using fork() (x = CORES)

then this part runs in father process:

    while (x<CORES){
    int selec = select(pfd[CORES-1][0]+1, &read_set, NULL, NULL, NULL);

    if(selec>0)
    {
        if(FD_ISSET(pfd[x][0], &read_set))
        {
            close(pfd[x][1]);

            if(read(pfd[x][0],&c,sizeof(c))==-1)
                perror("Ocorreu um erro a ler do pipe");

            printf("c= %f \n",c);
            c_final+=c;
            x++;
        }
        else
            printf("\nFile descriptor pfd[%d][0] is not set\n",x);

    }
    else if(selec == -1)
        perror("Ocorreu um erro no select");
}

the problem with this is that FD_ISSET(pfd[x][0], &read_set) will not pass because it is not set for pfd[0] but it can be for pfd[1] or vice-versa (because I don’t know if it will fail for 0 or 1)

EDIT2:

select() returns an int for the number of file descriptors in read_set. I have CORES set to 2 so it creates 2 processes and 2 pipes and sets 2 bits in read_set. my select() is returning int 1. should it be returning int 2 or 0 counts as well (so it would be 0 and 1 = 2)?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T06:43:14+00:00Added an answer on May 26, 2026 at 6:43 am

    First argument of select is supposed to be 1+max_fd. While it is likely that pfd[CORES-1][0] is the largest, it is not guaranteed. You could calculate the largest file descriptor when you are populating the read_set, and use that for your select call.

    Also, you need to re-populate the read_set before you call select every single time. So the for loop outside the while loop needs to come in. Actually, it is better to move the select outside, and enclose the block starting with the for loop setting up the read_set and ending after the while loop in another loop – so that at every select return, you process all signaled descriptors without going into select again.


    EDIT: To clarify what I meant about the loops: you should check all descriptors every time select returns. But you cannot call select only once, as there are no guarantees that all answers will be ready at once. So you do something like:

    for (i=0; i != CORES; ++i)
    {
      // Call pipe() and populate pfd[i]
    }
    
    while (!finished) // Need a proper condition to terminate this loop
    {
        // Setup read_set
        FD_ZERO(&read_set);
        for (i=0; i != CORES; ++i)
        {
            FD_SET(pfd[i][0], &read_set);
            // find max_fd here
        }
        // select here
        if (select(max_fd+1, blahblah) > 0)
        {
            for (i=0; i != CORES; ++i) // This used to be while(x < CORES). But a for loop would do
            {
                // Check the fd, and process
            }
        }
        else
        {
             // error in select
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
I've got a string that has curly quotes in it. I'd like to replace
I am currently running into a problem where an element is coming back from
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
I have a French site that I want to parse, but am running into
I want use html5's new tag to play a wav file (currently only supported

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.