Hello and welcome.
In this video session, we will discuss on the read() system call.
A read() system called is used to read the contents of the file.
In order to use the read() system call, we have to include the required header files as shown.
The read() system call takes the parameter file descriptor.
Which has been obtained from the open() system call and we have to pass the buffer.
And specify the number of bytes required to read.
The read() system call returns a number of bytes actually read.
This parameter is an unsigned integer.
The buffer argument supplies the address of the memory buffer into which the input data read needs to
be placed.
The buffer must be at least.
'count' bytes long.
This means that let's say we have to read ten bytes of data from file.
In that case, the buffer should have at least ten bytes.
This buffer cannot be less than ten bytes.
The buffer is present in the user space.
And the pointer to the buffer is passed onto the read() system call, the read() System Call reads the data
from the file and copies from the kernel mode to the user mode.
As a note read does not allocate any memory.
The user has to allocate the memory and pass to the read system call.
The buffer that is being specified here is the memory that is allocated by the user in user space.
The successful call to read() returns the number of bytes actually read.
Zero byte if it has reached the end of the file or read() system call returns '-1' .
If there is an error, we will have a look into the C program.
Also, note that read() system call is used to read from a regular file, a pipe socket and FIFO.
We will discuss on topics like pipe,FIFO in upcoming sessions.
Now let us see example of read() system call on a regular file.
In this example.
Of.
read() System Call.
We are opening the file 'input.txt' in read only mode.
The system called open() is used to open the file and in case of any error, we exit from the program.
If the open() system call is success, we further read the contents of the file.
Before reading the content of the file, we have to allocate buffer.
So here we have taken buffer of 20 bytes.
And we are calling read() system call and passing the file descriptor that is obtained from the open() system
call.
and
Pass the buffer pointer and specify the number of bytes that has to be read from the file.
Now 'fd' points to the file 'input.txt'.
In this example, we are making three reads.
This is to showcase the successful read behavior.
Now in the first read.
We are reading ten bytes of data.
And in second read, we are trying to read 11 bytes of data.
And in third read, we are again trying to read ten bytes of data.
Now let us see the output of this program.
So let us compile the program and execute the program.
We have compiled the program and now let us see the contents of the input file.
The 'input.txt'
has these contents.
Now let us execute the program and analyze its output.
When we execute the program, we can see that in the program We have called the read() system call three
times.
In the first read, the user has requested for ten bytes of data.
Now let us see its output.
10 bytes of data.
Is also been written from the read() system call and the first ten bytes of data.
Hi.
Welcome.
is been printed.
Now when the second system call the read() is being called.
The user requests for 11 bytes of data, but in this case, only nine bytes of data are being written.
This is because.
After the first ten bytes of data is being returned, there is no 11 bytes of data present and it returns
whatever the data is present within the 'input.txt' file and hence it has the remaining nine bytes of data
here and returns the nine bytes of data.
Now in the third read system call.
There is no more data that has been left and hence zero bytes of data is being read.
And here we can see that.
The end of the file has been reached and there is no more data to be read.
Also here we can as a note, we can see that when we open the file, we are printing the file descriptor
here.
The file descriptor that is obtained here is 3
So in the earlier videos, we had explained that in the process we have file descriptor 0, 1 and 2 as default
for standard input, standard output and standard error.
So now when we make an open() system call, we get the next available file descriptor.
In the next class we will discuss on write() system call.
Thank you.