Hello and welcome.
In this session we will discuss file operation in Linux system.
These are different topics under the file operations, they are file descriptor.
The different system calls involved in file operations such as open read write close lseek.
File descriptors are the handle that are required to perform any operations on the file.
So before performing any operation on the file, we need to open the file.
The open() system call returns the handle to the file called as file descriptor.
file descriptor is the minimum non negative number returned to the program.
In a process.
By default, the process has three file descriptors already opened.
They are file descriptor '0', which is standard input and corresponds to the keyboard file descriptor
'1' which is the standard output and corresponds to the screen.
File descriptor '2' is the standard error, and usually standard error is redirected to screen.
So.
These three file descriptors are by default available for any given process so the user application can
perform file related operation on standard input, standard output and standard error.
In Linux, everything is considered as a file.
For example, a keyboard is considered as a file.
The output system is considered as a file.
So all the things in Linux is considered as a file.
Now let's begin discussion on open system call.
In order to perform any operations on the file, like reading or writing, we need to open the file
and provide the handle to the kernel.
As discussed earlier.
Any further operations on this file will be done by this handle.
In this figure, we can see that 'fd' =0, fd=1, and fd=2 are already
pre available by default and corresponds to standard input, standard output and standard error.
Later when we open any new file, for example, the mentioned file from location home/user/data.log,
the next available file descriptor is given to the file.
So now any operation to this file is performed using the handle fd=3.
Please note that the file descriptor obtained for a file after performing open system call is not constant
or same all the time.
This depends on the number of file already opened.
Now let's discuss how to use open system call.
These are the headers required to be included to perform open system call.
The open system call has the parameter as mentioned here.
The first parameter is the path.
Which specifies the location where the file is present, followed by the flag and the optional parameter,
which is the mode.
This is a generic description of the open system call which has file-path.
The flag.
These two parameters are mandatory.
And the third parameter mode is optional.
And this optional parameter mode depends upon one of the flag parameters.
We will discuss this in the next slide.
We will now discuss the parameters used in open system call.
path provides the location of the file where it is present.
Then we have flags.
In the flags, we need to have at least one of the following flags.
Such as
O_RDONLY
O_WRONLY.
.
O_RDWR
O_RDWR.
.
This corresponds to read only.
Write only and read write.
.
So in these three flags, one of the flags is mandatory field along these flags.
Other flags can be used by using bitwise oring.
Then we use O_CREAT flag along with one of these flag.
Then the third parameter mode.
Has to be specified.
So this o_creat is used to create a new file if the file is not present.
And while creating the file, we need to specify the mode or the permission in which we are going to
create the file.
For example, the access permission of a file like read, write or execute.
This permission we had discussed in previous video session the open system call Returns a value which
is file descriptor or the handle.
In case of an error, it returns a -1 and the error is set accordingly and the error occurred.
We can check in the variable named 'errno' which is a global parameter in which the error
is set and we can check the error occurred.
Some of the most common errors are EACCES.
This is occurred when we have access permission issue EEXIST.
EISDIR.
We will see few of these examples in program.
As discussed, if open system call is success, it returns least non-zero value.
This value is called file descriptor.
All file operations on the file, like read or write, is performed on this file descriptor.
Now let us see the manual page for Open System Call.
No.
Let us open the manual for Open system call.
The manual page can be used as a cross reference.
It provides the syntax for the open system call and also the description of how the system call can
be used.
I suggest you to refer to the manual to explore the system calls.
Now we will see the open system call with the Example program.
Here we have written an example program to demonstrate open system call.
Firstly, we need to include the required header file.
Used for open system call as specified here.
You can also refer to the manual to get the header files that is used for any particular system call.
For example.
Here we can see the required header files that needs to be included for the open system call.
The 'errno.h' header file is used to get the error information if any error has been occurred.
In this program, we are opening a file called Startup.
And in read only mode and the open system call returns the file descriptor and is collected in the variable
'fd'.
If open system call has failed, it returns -1.
And if Open System call is success, it returns the file descriptor.
Of the open file.
Now let us compile and execute the program and see its output.
Let us first compile the program.
Now we have compiled the program.
Before executing the program, let us see if the file already present or not.
Here.
The file startup is present.
Now let us execute the program.
after .
Executing the program.
We see that open system call has executed successfully.
The file descriptor of the open file created is '3'.
From here, we can see that the open system call is success and it has returned the file descriptor
of the startup file and hence this line is printed with the value of fd=3.
Please note that we are opening the startup file in read only mode.
Now.
Now let us delete the file.
Startup here.
And now we have removed the file startup and now let us execute the same program once again.
Now, if we execute the program, we see that open system has failed.
That is the startup file is not present and hence the open system call has failed and it has returned
a value -1 and 'errno' has stored.
The reason why the system call has failed and the value of errno is '2'.
'perror' can be used to print the error message.
Here we can see that the error number '2' corresponds to the message.
No such file or directory.
Now let us again create the same file.
Now we have created the required file.
Now let us execute the same program.
Now.
When we execute the program again, we can see that the system call is success and the file descriptor
returned is 3.
The startup file is present and the open system call has returned successful and the corresponding
file descriptor has been assigned to the variable fd.
Now let us see the permission of startup file.
The permission in which the startup file is created is.
The user 'Mohan' has read, write permission without executable permission.
The group 'Mohan' has read write permission without execute permission, and others have only read permission
without write and.
Execute permission.
This program, when we execute, we execute as the user.
Mohan And we see that we are opening the file in read only mode.
And now this file has the read access for Mohan.
And hence when we execute the program, we can see the success.
Now let us change the mode of the file.
Now here we have changed the permission for user Mohan.
Here, we can see that we have removed the write permission for the user.
Mohan.
Now, let us execute the program.
Yet we can see that even though the file is present and when we execute the program, the open system
call has failed.
This is because the open system call is trying to open the file in read only mode, whereas the permission
for user Mohan does not have read permission and hence the open system call has failed.
Now let us again change the permission of the startup file.
Now the startup file has read permission for user Mohan.
Now, if we execute the program, we can see that the system call is success.
In this program, we will see how to create a file using open system call.
Here we use O_CREAT to create a file if the file is not present.
For example, here we have specified to create a new file called 'newfile.log' in read write mode.
And here we have specified, O_creat.
So when the execution starts, the open system call checks if the 'newfile.log' is present.
If the specified file is not present, it creates a new file with the permission specified by
the user here.
Now, let us execute this program.
Let us compile the program first.
We have compiled the program now before executing, let us see whether the file is present or not.
Let us delete the file that is present already.
No, there is no file named 'newfile.log'.
Now, let us execute the program.
Now we can see the open system call is executed successfully.
That is the 'newfile.log' was not present.
And if you see here now, the file should have been created.
The file is now created.
Now let us check the permissions with which the file has been created.
So if you check here, the permission that has been created is same as the permission that has been
specified in the program.
So whenever 'O_CREAT' flag is used, the open system call sees if the file.
Is present or not.
If the file is already present, it ignores the creation, and if the file is not present, it creates
a new file with the permission that is mentioned here.
Now let us alternatively see how we can use the other methods to read the file.
Here in place of.
The permission specified here.
'774' We can also specify the same using the standard flags that are used.
For example, let us take this as example and we can see the cross reference in the manual page.
Here.
This flag refers that the user has read, write and executable permission, which is nothing but the
first 7 that we have mentioned here.
And next, let us see this.
This says that the group has read, write and executable permission.
And in the same way.
This says that 'others' have read permission only, and that is nothing but '4' present here.
So we can also specify the permissions using these flags as a note.
if we are using more than one flags.
It should be used in conjunction with 'OR'.
Now let us compile the same program once again.
Now.
We have compiled the program now.
Let us remove the file that we have created.
No, Let's execute the program.
After executing, let's check the permission of the file that is created.
Now we can see that this file that is created is the same as that we have mentioned in the program.
In this program.
We will see how to use 'O_EXCL' here.
This can be used along with 'O_CREAT'.
Now let us.
Compile this program and execute along and explain the working of the program.
Now let us compile the program.
Now we have compiled the program and now let us see if this file exists or not.
This file is not there.
Now let us execute the program.
We have executed the program and we can see that the open system call was success, that is this 'newfile2.log'
is being created because of the 'O_CREAT' .
So here we can see.
Here we can see that the 'newfile2.log' is being created.
Now, if we try to execute the same program, once again, we can see that we are getting an error.
That is, if you specify 'O_EXCL'.
If the file is already created and if we try to and if 'O_CREAT' is specified, it throws an error saying
that the file is already created.
So now let us again remove the file.
And now if we execute.
The program is successful and it has created a new file.
But if the same program, if it is executed for the second time, it throws an error saying that the
file is already present.
So this can be used in use cases where we have to specify to the user only during the first time creation
of the file and not allow the second time creation of the file.
Join me for the next session where we discuss the Read() system call.
Thank you.