C Programming

Read System Call in C

A system call is a method for software to communicate with the operating system. When software performs a system call, it sends the request to the kernel of the operating system. To read by a file descriptor, you can use the read () system function. Each process has its personal file descriptors table in the operating system. The sole difference between read () and write () is that read () reads data from the file referred to by the file descriptor. The reading time for the file is updated after a successful read ().

Syntax

# ssize_t read(int fd, const void *buf, size_t count);

The file descriptor is the first argument. The buffer is the second argument. Finally, the third option specifies how many bytes you consider reading. Bytes is just the volume of data to copy, and the buffer is the address of the memory space where data is to be written out. The return result represents the quantity of data written in bytes; if it varies from bytes, something seems to be wrong. If the value is negative, the system call will abort.

Pre-requisites

The GCC compiler is necessary to compile the code that contains a read() system call. To execute and assemble C language code, we’ll need to install a compiler package on our machine. So, we’ll need to install the GCC compiler too. You can install it with the presented below command in a Linux terminal, as shown below. It will install in just a few moments, and you will be able to simply write and run C codes in files.

By hitting the enter key, the GCC compiler will be installed. It will take a few moments before asking you to say yes or no. If you choose yes, it will begin screening the output of the GCC compiler. Utilize the below-appended instruction to install GCC on your system.

$ sudo apt install gcc

First Example of Read() System Call in C

Let’s start with read () to get a better understanding of it. First, you must use the shell’s GNU editor to generate a C-type file. Try running the simple query below in the terminal for this.

$ nano read1.c

It will take a bit of time for the above-mentioned command to open, but once it does, you will be able to use it immediately. It launches your Ubuntu 20.04’s GNU editor right away. The GNU editor will appear as shown in the image attached below. Now, write the displayed code in the editor.

The read () system call receives the user’s input from the keyboard that is the file descriptor as 0 and puts it in the buffer “buff”, which is just a character array. It can only read up to 10 bytes at a time. Only the first 10 letters will be read, regardless of how much the user inputs. Lastly, using the write () system call, the data is displayed on the window.

On the display, it prints the very same 10 bytes as from the buffer from the file descriptor 1. You should now compile the code shown above. We need to use the GCC compiler for the code compilation. To compile the “read1.c” file, run the GCC command below. Return to the console and write the command:

$ gcc read1.c

Now, we must run this code using the./a.out command in the terminal as seen below.

$ ./a.out

As per the program, the output is displayed in the above-attached screenshot.

Second Example of Read () System Call in C

You can’t predict how much the user would enter as input. As a result, the third argument to the write () system cannot be used to provide the right bytes. The result may differ from what you expected. Keep in mind what read () returns if it succeeds. To demonstrate this concept, we are executing this example.  Now again use the shell’s GNU editor to generate a C-type file. Try running the simple query below in the terminal for this.

$ nano read.c

It will take a bit of time for the above-mentioned command to open, but once it does, you will be able to use it immediately. It launches your Ubuntu 20.04’s GNU editor right away. The GNU editor will appear as shown in the image attached below. Now, write the displayed code in the editor.

In this code, we utilize the nread variable to hold the number of bytes that are read by the read () system call in C, and use the variable in write () to display the same amount of bytes on the window. You should now compile the code shown above.

We need to use the GCC compiler for the code compilation. To compile the “read.c” file, run the GCC command below. Return to the console and type the command:

$ gcc read.c

Once the code is compiled, run this code using the ./a.out command in the terminal, as seen below.

$ ./a.out

As per the program, the output is displayed in the above-attached screenshot.

Conclusion

To comprehend the concept of the Read () system call in Ubuntu, we have explored a few simple and linear examples in C. We’ve also covered how to set up the GCC compiler for code compilation. I hope you can now simply use the Read () system call to build your code on Ubuntu 20.04.

About the author

Kalsoom Akhtar

Hello, I am a freelance writer and usually write for Linux and other technology related content