Hello and welcome!
In this video session we will learn the Il6 system call and we will see how to use the Il6 with few
examples.
A file can be considered as a continuous set of bytes.
There is an internal indicator present which points to the offset byte of the file.
This offset is used to read or write the next set of bytes or data from the file.
This indicator is updated when we do any file operations like read or write.
LCI is a system call that is used to change the location of the read or write pointer of the file descriptor.
The location can be set either in absolute or relative terms.
Now let's see the syntax of LCI.
In order to use LCI, we have to include Unistd.h and the syntax is as follows.
It takes the file descriptor and the offset and it has a parameter w.
Hence we will discuss these parameters.
So when the LCI LCD fails, it returns a minus one on error.
So we can consider this as a file with some data in it.
So we have a offset indicator which keeps on changing its position based on the read and write system
call.
Six it refers to the initial offset of the file, and second refers to the end of the file.
Now, now let's go back to the syntax of the LCD.
So in this LCD we have to pass the file descriptor.
If D represents the file descriptor of the file and next will be the offset.
So offset refers to number of bytes that we need to uh travel.
For example uh there is one more parameter called as w.
Hence the w can take one of the three parameters like seek, set, seeker, and seek.
So when we give seek set then the number of the bytes that the internal indicator points to is zero
plus offset.
That is nothing, but it points to the offset present of offset indicated in the LCD.
So for example, let's say that we have given a offset of ten and we have given six eight as a parameter
in in W.
So now after we call a LCD the internal indicator points to the offset byte.
That is nothing but the 10th byte.
Next, let's see how C core works when we specify the parameter c underscore cur.
So the number of the bytes offset occurs is from the current position of the offset plus the offset
given in the system call.
For example, let's say that our current offset is somewhere here in the middle of the data.
And if we say the if we specify seek curve and give offset equal to five, then the indicator shifts
to five location from the current offset.
And similarly, if you specify the parameter, seek and the offset is set to size of the file plus offset
bytes specified in the system call.
When lseek fails, it returns a minus one.
In this example, we will see how to use L6 system call and the different parameter that needs to be
passed.
Here we include the required header files.
Then later open the file called input.txt in read write mode.
In case of an error from the open system call, we exit the program.
Now we are going to perform the Lseek.
Now the Lseek is being performed initially on seek set to zero bytes and later fix it to two bytes and
later we are performing a lseek on 6 to 6 offset bytes, and we are reading the contents from the new
offset position, and finally travelling to the end of the offset of the file and appending some new
data.
Let's compile this code.
Run the program and analyze the code line by line.
Now let's compile the code using the gcc compilers.
We have got the output file lseek and let's see the input file.
The contents of input input file is something like.
This is example of ordering.
This is a input of the input.txt.
Let's print the input on the screen for reference.
So now we are going to run the object file that we have obtained from compiling the code.
After running the program.
Let's analyze the output.
The initial offset position is zero.
So this is from this position.
From this line.
So initial offset is zero because we have given a six set here.
So six set travels from the beginning of the file to zero bytes and and returns back the new offset
value.
Now the new offset points to the zeroth byte of the file.
Next the offset position is two.
We have got the output that is nothing, but after performing a check of two bytes from six eight,
that means whenever we give six set it is it refers to from the beginning of the file, travel the bytes
mentioned in the parameter.
So now from the beginning of the file we have traveled two bytes.
So the new position seek position would be two.
That is the output what we have got.
Next let's analyze this output.
Seek position is eight.
Right.
Now we have called the Wilczek with parameter as c underscore C, and given a offset white offset six.
This means from the current offset travel to six bytes.
So the current offset in this case was two.
So two plus six is eight.
So the new offset now points to byte eight.
So so now after the offset is pointing to new location we are going to do a read system call.
And we are going to read the ten bytes of data and copy it into the buffer.
So after reading the data from the input.txt we are going to print the buffer here.
So uh so read bytes are as follows.
Let's see.
The read bytes are as follows So example of is the data that is being read from the file.
So if you see here we are getting this string from the input dot txt.
This means the read starts from the eight byte and gets the next ten bytes of data from the file.
So now next we are going to perform a Lseek and travel to the end of the file.
So the seek end specifies the offset to end of the file.
And we are going to print the seek new seek position.
That is the.
This is nothing, but it points to the end of the file and the position is 65.
So now after going to the end of the file, we are going to append the new data.
So here the new data is copied to the buffer and written on to the written to the file using the file
descriptor.
And finally we close the file.
So now let's check the contents of the file.
So here this was the original content.
And we have appended the new data.
This appending of new data is performed after travelling to the secant.
So the secant has travelled to the end of the file.
And after this when we write a new set of data, it has appended to the end of the file.
So this secant can also act as a append flag.
Okay, so one more point to mention here is whenever we open any file, we get the file descriptor.
Then we perform the operations like read write like whatever we have to perform.
Then finally we need to close the file.
So the closing the file will return the uh file descriptor that we have obtained previously to the system.
And this file descriptor can be used to open any other new file.