At the start, log in from your Linux operating system and open the command-line console terminal. There are two ways to open it. The first one uses a shortcut key “Ctrl+Alt+T” while you are at the desktop of the Linux system, and the other is using the activity area. Click on the activity menu bar highlighted at the top left corner of the desktop screen. The search bar will pop up. Tap on it, and write “terminal” in it. Hit the key “Enter” to proceed. The terminal shell will be opened then. After the opening of the console terminal, it’s time first to create a file having a “txt” type extension for simple use. We may use the “touch” instruction and the name of a file, e.g., test.txt, to create it. Press the “Enter” button, and a file will be created.
In the home directory of your Linux system, you can find the recently formed file “test.txt”. Double-tap on it to open it and type out the below-presented data in it. Save it by pressing “Ctrl+S” or simply clicking on the button of “Save”. Close it after updation using a cross sign at the right side of the file.
Coming back to the terminal, you can also check the data of this file using a very simple “cat” command as beneath. The output is showing the contents of the file “test.txt”.
Now, to start working on the C language, your Linux system must have some C language compiler in it. For this, you have to install a compiler first. We suggest installing the “GCC” compiler using the “apt” sudo command as below.
After the configuration of a compiler, you can now work on the C language properly. Firstly, we have to create a new file with a “C” type extension at its end using the nano editor. Therefore, try the below query in the console to do so.
Explanation of Code
After the nano editor has been opened, write out the below code of C language in it to use the readv system call in our Linux system. First of all, we have used the below libraries in the C code to be work properly and without any error. Then we have declared the main function as shown in the snap. At the very start of the main function, we have to define two variables, “i”, and “fd”. After that, three-character type arrays or less have been specified named “f1”, “f2”, and “f3”. After this, we have declared a structure type list or array named “iovec”. Every iovec structure defines a fragment, which would be an individual disjoint buffer. Then we have created another variable of type size named “nr”. After all the declarations, we have specified a simple “open” system call to open the file “test.txt” from its director and read all its contents, and revert to the file descriptor “fd”. The flag O_RDONLY has been used for reading purposes. On the next line, we have declared an “if” statement to check whether the file descriptor is equaled to “-a” or not. If it is “-1”, it will through an error message “open” in the shell and return 1. Outside of the “if” statement, we have used the file descriptors to assign values to the structure indexes. The “iov.base” is a pointer showing the start of a buffer, and the “iov.len” is showing the total size of the buffer in bytes. A vector seems to be a collection of sections. Each section of the vector specifies the location and size of a buffer cache upon which or as of what data can be inscribed or read. Beforehand moving on to the subsequent buffer, the readv() method fully covers every one of the “iov_len” bytes buffers. Before actually moving on to another buffer cache, the writev() method still inscribes out the entire “iov_len” bytes. Beginning with iov, formerly iov, and so forth, via iov[count-1], both operations still act on the sections in order. After it, we have declared a “readv” system call to read the file descriptor and “iov” byte buffers up to the size of 3.
Then we have checked the returned bytes. If the return value is “-1”, it will show an error message “readv”. We have used the “for” loop to print the file characters via “iov”. If the function has been closed, It will display “close”. Save this code using “Ctrl+S” and quit the file using the “Ctrl+X” shortcut method.
Now compile the code using the gcc command as stated below.
After that, run the execution command to check the results. The output below is showing the contents in chunks and showing the error messages as well. It is also showing the index number for the buffer array as 0, 1, and 2.
We have finished all the essential parts to intricate the “readv” system call in the Ubuntu 20.04 Linux system. All the commands will work the same for other Linux distributions.