Read Sustem Call Definition in Linux Is in Which C File

A system telephone call is a method for software to communicate with the operating system. When software performs a system telephone call, it sends the request to the kernel of the operating system. To read by a file descriptor, you tin can use the read () system part. Each process has its personal file descriptors table in the operating system. The sole difference between read () and write () is that read () reads information 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 re-create, and the buffer is the address of the retentivity infinite where data is to be written out. The return upshot represents the quantity of information written in bytes; if it varies from bytes, something seems to be incorrect. If the value is negative, the system call will arrest.

Pre-requisites

The GCC compiler is necessary to compile the code that contains a read() organization telephone call. To execute and assemble C linguistic communication code, we'll need to install a compiler parcel on our machine. And then, nosotros'll need to install the GCC compiler too. Y'all can install it with the presented below command in a Linux terminal, as shown below. It will install in just a few moments, and y'all will exist able to only write and run C codes in files.

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

Kickoff Example of Read() Arrangement Call in C

Let'south beginning with read () to become a better understanding of it. Showtime, you must employ the trounce's GNU editor to generate a C-blazon file. Attempt running the elementary query below in the terminal for this.

It will accept a bit of time for the above-mentioned command to open, but once information technology does, you volition be able to utilise it immediately. Information technology 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 () organisation call receives the user'southward input from the keyboard that is the file descriptor as 0 and puts information technology in the buffer "buff", which is but a character array. It tin just read upward to x bytes at a time. Only the beginning 10 letters volition be read, regardless of how much the user inputs. Lastly, using the write () organisation call, the data is displayed on the window.

On the brandish, it prints the very same 10 bytes as from the buffer from the file descriptor one. Yous should now compile the code shown higher up. Nosotros need to employ the GCC compiler for the code compilation. To compile the "read1.c" file, run the GCC control below. Return to the panel and write the command:

Now, nosotros must run this lawmaking using the./a.out command in the terminal every bit seen beneath.

Every bit per the program, the output is displayed in the in a higher place-attached screenshot.

2nd Example of Read () System Telephone 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 yous expected. Keep in mind what read () returns if it succeeds. To demonstrate this concept, we are executing this example.  Now over again use the beat out'south GNU editor to generate a C-type file. Try running the simple query beneath in the terminal for this.

It will take a scrap of time for the above-mentioned command to open up, but once it does, y'all volition be able to use information technology immediately. Information technology launches your Ubuntu twenty.04'due south GNU editor right away. The GNU editor will announced every bit shown in the epitome attached below. At present, write the displayed code in the editor.

In this lawmaking, we utilize the nread variable to concord the number of bytes that are read by the read () system call in C, and utilise the variable in write () to brandish the same corporeality of bytes on the window. Yous should now compile the code shown above.

We need to use the GCC compiler for the lawmaking compilation. To compile the "read.c" file, run the GCC control below. Return to the console and blazon the control:

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

Every bit per the program, the output is displayed in the above-fastened screenshot.

Decision

To embrace the concept of the Read () organisation call in Ubuntu, we have explored a few unproblematic and linear examples in C. We've as well covered how to fix the GCC compiler for code compilation. I hope you can now just use the Read () arrangement call to build your code on Ubuntu 20.04.

Nearly the writer

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

webbwassing.blogspot.com

Source: https://linuxhint.com/read-system-call-in-c/

0 Response to "Read Sustem Call Definition in Linux Is in Which C File"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel