Example for Select System Call:
Let’s have a simple example to check out the working of select system call. Open the command-line shell using the “Ctrl+Alt+T.” After the terminal opening, make sure you have a C language compiler installed on your system because the code will not work without it. If you don’t have one, try out the below command to do so.
If you want to see the Linux select system call working, you must have the manpages-dev package installed on your system as per the below command. Write your user password to continue.
You can see the syntax and working of select system call using the below “man” command.
The output man page for the select system call is shown below. Quit it by pressing the “q” key.
Standard directories, command shell terminal & pseudo-terminal applications, STREAMS centered documents, FIFOs, including pipes, are all supported by the select() module. The activity of select() on document descriptors referring to these other file formats is unknown. So below is the explanation of the Select system call.
The description of the syntax shown in the above image is described below.
Above is the library to be included in the C code while using the select system call. If it’s not included, the code doesn’t work.
The nfds statement defines the file descriptor set that will be evaluated. The select() method checks file descriptors between 0 and nfds-1.
While readfds isn’t null, it refers to an array of form fd_set that defines which document descriptors should be tested for readiness to read on input and which document descriptors should be all set to read on output.
When the writefds parameter isn’t empty, it refers to an entity of form fd_set that defines which document descriptors should be tested for readiness to inscribe on input. The document descriptors should be prepared to compose on output.
While the errorfds parameter isn’t empty, it refers to an object of kind fd_set, which determines which document descriptors should be tested for awaiting error state of affairs on input; also, the document descriptors should be verified on output.
The readfds, writefds, and errorfds parameters are updated after accomplishment to signify that document descriptors are prepared for having read, fully prepared for writing, or even have an exception state awaiting, accordingly. If the subsequent bit is fixed on input and the relevant situation is valid for every file descriptor a little under nfds, the subsequent bit would be placed on effective accomplishment.
While the timeout parameter isn’t really empty, it leads to a structure timeval entity that determines the supreme time to hold your fire for the choice to finish. Select() doesn’t quite obstruct if such timeout parameter leads to a structure timeval object with 0 elements. Select() stops unless an occurrence allows one of the filters to be retrieved with a true (non-zero) result, whether the timeout statement is an empty reference. Select() succeeds effectively and produces 0 when the waiting period ends even before occurrence happens, which might require one of several masks to be adjusted to a non-zero value.
This macro eliminates the collection (erases all of the document descriptors). It can be used as the very first move in a document descriptor collection’s initialization.
The document descriptor fd is added to the collection by this macro. Using a document descriptor to a collection that has already existed would be non-operational and therefore does not result in an exception error.
The document descriptor fd is removed from the collection by this macro. A file descriptor eliminated from the collection which does not exist is non-operational and therefore does not result in an exception.
The select() system call alters the information or data of the collections as per the rules mentioned above. The FD ISSET() macro may be cast off by invoking select() to see whether a document descriptor remains available in a collection.
Let’s create a new C type file named “new.c” as below.
The GNU editor has been opened. Write out the beneath code in it. The code contains headers, main function with some file descriptors as elaborated above. It also contains a structure for the time interval value. Then we have used the select system call to check for the error in the “if” statement. Press “Ctrl+S” to save this code and “Ctrl+X” from quitting the editor.
Now compile the code using “gcc” instruction.
Run the code and don’t add any input. The output will say “select timeout.”
Again, run the code and give some input in the terminal, e.g., “Aqsa.” It will output that the “data is available.”
We have implemented the select system call on the Ubuntu 20.04 Linux system. The code should work on all the distributions of Linux equally and properly. Hope you find this article easy to implement and understand.