Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is stat() an expensive system call?

Tags:

c

linux

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?

like image 899
Rama P Avatar asked Jun 17 '13 14:06

Rama P


People also ask

What is stat () system call?

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.

Why are system calls expensive?

A system call requires that the system switches from User mode to Kernel mode. This makes system calls expensive.

What is stat in programming?

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.

Is Fopen a system call?

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.


1 Answers

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.

like image 169
David Souther Avatar answered Nov 04 '22 08:11

David Souther