2021SC@SDUSC
Now we've known what fd is, how does fd function remains unseen.Start with function open(), we shall discuss about how actually those operations to files finally happens.
Open()
We might write some code to open a file in linux that is:
#include <fcntl.h>
using namespace std;
int main()
{
off_t fd;
fd=open("rd.in",O_RDONLY);
cout<<fd<<endl;
return 0;
}
fd will be given an number which is unsigned integer that we've talked about.Usually the number is larger than 3 because 0,1,2 have been used as stdin, stdout and stderr.
Do_sys_open
How does the kernal get such fd?Go back to functions to see.When users' process call open() to open a file, kernal VFS will call sys_open()->do_sys_open() to function.Here's