How to Use Select System Call In C - JohnHau/mis GitHub Wiki

The select() system call enables a system to keep track of several file descriptors. So, the select system call waits for one of the descriptors or a whole to turn out to be “ready” for a particular type of I/O activity (e.g., input possible). If the subsequent I/O process (e.g., read(2)) can be performed despite blocking, the file descriptor is deemed able. The select() method describes whether a document descriptor is available for reading, prepared for writing, or in an exception state. Suppose the stated situation is false for entirely the stated file descriptors. In that case, the select() system call blocks before the mentioned circumstances are valid for at minimum one of the stated document descriptors, equal to the stated interval of timeout. 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.

image

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.

image

The output man page for the select system call is shown below. Quit it by pressing the “q” key.

image

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.

image

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.

image

image

Conclusion: 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.