ArticleslgStudy

computer science

Stat (system call)

Stat (system call) is a computer science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Stat (system call) rather than just read about it. In short: stat() is a Unix system call that queries the file system for metadata about a file (including special files such as directories). The metadata contains many fields including type, size, ownership, permissions and timestamps.

Stat (system call) — main illustration
Stat (system call) — illustration

Key takeaways

  • Stat (system call) belongs to computer science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Stat (system call) to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Stat (system call) from memory before moving on to harder problems.

Reference excerpt

stat() is a Unix system call that queries the file system for metadata about a file (including special files such as directories). The metadata contains many fields including type, size, ownership, permissions and timestamps. For example, the ls command uses this system call to retrieve timestamps:

mtime: when last modified (ls -l) atime: when last accessed (ls -lu) ctime: when last status changed (ls -lc) stat() appeared in Version 1 Unix. It is among the few original Unix system calls to change, with Version 4's addition of group permissions and larger file size. Since at least 2004, the same-named shell command stat has been available for Linux to expose features of the system call via a command-line interface.

Functions The C POSIX library header sys/stat.h, found on POSIX and other Unix-like operating systems, declares stat() and related functions.

Each function accepts a pointer to a struct stat buffer which the function loads with information about the specified file. As typical for system calls, each function returns 0 on success, or on failure, sets errno to indicate the failure condition and returns −1. The stat() and lstat() functions accept a path argument that specifies a file. If the path identifies a symbolic link, stat() returns attributes of the link target, whereas lstat() returns attributes of the link itself. The fstat() function accepts a file descriptor argument instead of a path, and returns attributes of the file that it identifies. The library has been extended to support large files. Functions stat64(), lstat64() and fstat64() load information into a struct stat64 buffer, which supports 64-bit sizes, allowing them to work with files 2 GiB and larger (up to 8 EiB). When the _FILE_OFFSET_BITS macro is defined as 64, the 64-bit functions are available as the original names.

Data structure The metadata structure is defined in the sys/stat.h header. The following shows the base fields, but an implementation is free to include additional fields:

POSIX.1 does not require st_rdev, st_blocks and st_blksize members; these fields are defined as part of XSI option in the Single Unix Specification. In older versions of POSIX.1 standard, the time-related fields were defined as st_atime, st_mtime and st_ctime, and were of type time_t. Since the 2008 version of the standard, these fields were renamed to st_atim, st_mtim and st_ctim, respectively, of type struct timespec, since this structure provides a higher resolution time unit. For the sake of compatibility, implementations can define the old names in terms of the tv_sec member of struct timespec. For example, st_atime can be defined as st_atim.tv_sec. Fields include:

Example

The following C program reports metadata about each file passed via the command-line – using stat() to query the system for the information.

References

External links atime and relatime IEEE Std 1003.1, 2004, documentation for fstat(2). Retrieved 2012-06-07. stat() in Perl stat() in PHP stat(2) Linux man page. Retrieved 2012-06-07. Stevens, W. Richard; Rago, Stephen A. (May 24, 2013). Advanced Programming in the UNIX Environment (Third ed.). Addison-Wesley Professional. ISBN 978-0321637734. Retrieved February 27, 2015.

Illustrations

Stat (system call): stat command line
stat command line

Worked examples

Example 1 — a first encounter with Stat (system call)

Start with the simplest possible case. Write down what Stat (system call) claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In computer science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Stat (system call) before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Stat (system call) ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Stat (system call)

In research
Stat (system call) appears in computer science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Stat (system call) in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Stat (system call) is common in secondary-school and first-year university syllabi. It links to neighbouring topics C POSIX library, POSIX, System calls, so understanding it makes those chapters shorter.
In everyday life
Look for Stat (system call) outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “Stat (system call)” →

How to study Stat (system call) in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Stat (system call) means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Stat (system call) out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Stat (system call) in simple terms?

stat() is a Unix system call that queries the file system for metadata about a file (including special files such as directories). The metadata contains many fields including type, size, ownership, permissions and timestamps.

Why does Stat (system call) matter?

Because it connects several computer science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Stat (system call)?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Stat (system call).

Tags

  • C POSIX library
  • POSIX
  • System calls
  • Unix file system-related software

Keep exploring