Home > Enterprise >  Select system call
Select system call

Time:09-17

I have been doing socket programming and the following is the select system call. If this program doesn't get an input within 5secs it will terminate else it will execute the command in terminal. I don't understand which part of program is making the given message execute as a command in terminal. For example, if we type ls and give enter it executes the ls command in terminal but I don't understand what part of code is responsible to execute the ls command. Please help me out. Here is the code.

#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>
int main(void)
{
    fd_set rfds;
    struct timeval tv;
    int retval;

    /* Watch stdin (fd 0) to see when it has input. */

    FD_ZERO(&rfds);
    FD_SET(0, &rfds);
    /* Wait up to five seconds. */

    tv.tv_sec = 5; //in seconds
    tv.tv_usec = 0; //in microseconds

    retval = select(1, &rfds, NULL, NULL, &tv);

    /* Don't rely on the value of tv now! */

    if (retval == -1) //select failed
        perror("select()");
    else if (retval) //user input
        printf("Data is available now.\n");
    /* FD_ISSET(0, &rfds) will be true. */
    else
        printf("No data within five seconds.\n");
    exit(EXIT_SUCCESS);
}//program exit

CodePudding user response:

I don't understand what part of code is responsible to execute the ls command.

No part of the code is executing the command.

The command is immediately exiting when input is available. It will not read the input. Instead, the command shell where you started the program will read the input after the program exited and process the input - i.e. shell will execute ls.

  • Related