Is the stat()
system call really expensive? I read somewhere that it is a costly system call to use. Is it, really? If so are there any other alternatives?
The stat() system call returns data on the size and parameters associated with a file. The call is issued by the ls -l command and other similar functions. The data required to satisfy the stat() system call is contained in the inode.
A system call requires that the system switches from User mode to Kernel mode. This makes system calls expensive.
The stat() function gets status information about a specified file and places it in the area of memory pointed to by the buf argument. If the named file is a symbolic link, stat() resolves the symbolic link. It also returns information about the resulting file.
fopen is a function call, but it may sometimes be referred to as a system call because it is ultimately handled by the "system" (the OS). fopen is built into the C runtime library.
In a typical setting, stat(2)
, fstat(2)
, and lstat(2)
are the only sane techniques for getting file information. If you're seeing performance problems, it would be worthwhile to profile your application and see what happens.
To profile, compile with gcc -pg
and run the executable with gprof(1)
.
You could, potentially, switch to using a larger library like Qt, but that will not likely handle any performance problems, and they will likely use stat(2)
anyway.
So, whether it's expensive or not, there are no reasonable alternatives.
That said, as in Jim Mcnamara's comment, it's not expensive for precisely these reasons. As there's no other alternative, the glibc and linux programmers have made it as performant as possible.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With