System Information
UNIX operating systems provide standardized interfaces that allow applications to obtain information about the system on which they are running. This information includes the operating system name, host name, hardware architecture, system limits, and other configuration details. Using standard APIs instead of platform-specific methods improves the portability and reliability of applications.
Learning Objectives
- Understand system information APIs.
- Learn the
uname()function. - Study the
struct utsnamestructure. - Understand host and system identification.
- Prepare for system configuration APIs.
1. What is System Information?
System information consists of descriptive data about the operating system and the execution environment. Applications use this information to identify the operating system, determine supported features, display diagnostic information, and adapt their behavior when appropriate.
2. Why is System Information Needed?
- Identify the operating system.
- Display system diagnostics.
- Support portable applications.
- Determine available system resources.
- Retrieve host identification.
- Assist with debugging and logging.
3. uname()
The uname() function retrieves information describing the
current operating system.
The information is returned in a
struct utsname structure.
4. struct utsname
The uname() function stores the returned information in a
struct utsname structure.
The exact size of each character array is implementation-dependent.
5. Members of struct utsname
| Member | Description |
|---|---|
sysname |
Operating system name. |
nodename |
Network node (host) name. |
release |
Operating system release. |
version |
Operating system version. |
machine |
Hardware architecture. |
6. Example Using uname()
7. Host Name
The host name identifies a computer within a network.
It is commonly used by network applications, system administration
utilities, distributed systems, and diagnostic tools.
The host name is typically available through the
nodename member returned by
uname().
8. System Information Workflow
9. Practical Applications
| Application | Purpose |
|---|---|
| Installation Programs | Detect operating system information. |
| Diagnostic Utilities | Display system information. |
| Logging Systems | Record host identification. |
| Monitoring Software | Collect environment information. |
| Distributed Applications | Identify participating hosts. |
10. Interview Questions
- What information does
uname()return? - Which header file declares
uname()? - What structure stores information returned by
uname()? - What does the
sysnamemember represent? - What information is stored in
machine? - What does the
nodenamemember represent? - Why should applications use
uname()instead of platform-specific methods? - How does system information improve portability?
- Why is host identification useful?
- What is returned if
uname()fails?
11. gethostname()
The gethostname() function retrieves the current system's
host name.
Unlike uname(), which returns multiple pieces of system
information, gethostname() returns only the host name.
12. Example Using gethostname()
13. sysconf()
The sysconf() function returns the current value of a
system configuration variable.
These values describe runtime system limits and implementation-defined
characteristics.
14. Common sysconf() Variables
| Variable | Description |
|---|---|
_SC_ARG_MAX |
Maximum length of command-line arguments. |
_SC_CHILD_MAX |
Maximum simultaneous processes per user. |
_SC_OPEN_MAX |
Maximum number of open files. |
_SC_PAGESIZE |
Memory page size. |
_SC_CLK_TCK |
Clock ticks per second. |
15. Example Using sysconf()
16. pathconf()
The pathconf() function returns configuration information
associated with a particular file or directory path.
17. fpathconf()
The fpathconf() function performs the same operation as
pathconf(), but uses an open file descriptor instead of a
pathname.
18. pathconf() vs fpathconf()
| Function | Uses |
|---|---|
pathconf() |
File or directory pathname. |
fpathconf() |
Open file descriptor. |
19. System Configuration Workflow
20. Interview Questions
- What is the purpose of
gethostname()? - How does
gethostname()differ fromuname()? - What information does
sysconf()return? - What does
_SC_OPEN_MAXrepresent? - Why should applications use
sysconf()instead of hardcoding system limits? - What is the purpose of
pathconf()? - How does
fpathconf()differ frompathconf()? - What type of argument does
fpathconf()require? - When would
pathconf()be useful? - Which header file declares these configuration APIs?
21. sysconf() Return Values
The sysconf() function returns the current value of the
requested system configuration variable.
The return value may have one of three meanings.
| Return Value | Meaning |
|---|---|
| Positive Value | The requested configuration value. |
| -1 and errno unchanged | The limit is indeterminate (no fixed value). |
| -1 and errno set | An error occurred. |
22. pathconf() Return Values
The pathconf() and fpathconf() functions use
the same return-value convention as sysconf().
Applications should clear errno before calling these
functions so that an indeterminate limit can be distinguished from an
error.
23. Error Handling
24. Common _PC_* Variables
| Variable | Description |
|---|---|
_PC_NAME_MAX |
Maximum filename length. |
_PC_PATH_MAX |
Maximum pathname length. |
_PC_PIPE_BUF |
Maximum atomic write size for pipes/FIFOs. |
_PC_LINK_MAX |
Maximum number of hard links. |
_PC_CHOWN_RESTRICTED |
Whether ownership changes are restricted. |
25. Example Using pathconf()
26. Runtime vs Compile-Time Limits
| Compile-Time Limits | Runtime Limits |
|---|---|
| Defined in header files. | Obtained using APIs. |
| Known while compiling. | Determined while executing. |
| May not reflect the running system. | Represent the actual running environment. |
| Usually constants. | May differ across systems. |
27. Choosing the Correct API
| Need | API |
|---|---|
| Operating system information | uname() |
| Host name | gethostname() |
| Runtime system limit | sysconf() |
| Path-specific limit | pathconf() |
| Open file descriptor limit | fpathconf() |
28. Runtime Configuration Workflow
29. Best Practices
- Use runtime configuration APIs instead of hardcoded limits.
- Always check return values.
- Clear
errnobefore checking for indeterminate values. - Use
sysconf()for system-wide limits. - Use
pathconf()for filesystem-specific limits.
30. Interview Questions
- What are the possible return values of
sysconf()? - Why should
errnobe cleared before callingsysconf()? - How do you distinguish an indeterminate limit from an error?
- What does
_PC_NAME_MAXrepresent? - When should
pathconf()be used instead ofsysconf()? - What is the purpose of
_PC_PIPE_BUF? - Explain runtime limits and compile-time limits.
- Why are runtime configuration APIs more portable?
- Which API should be used to obtain path-specific configuration values?
- What happens if
sysconf()returns-1anderrnois zero?
31. getconf Utility
The getconf command displays the current value of
system configuration variables from the command line.
Many of these values correspond to the information returned by
sysconf() and pathconf().
32. Useful Linux Commands
| Command | Purpose |
|---|---|
uname -a |
Display complete operating system information. |
hostname |
Display the system host name. |
getconf ARG_MAX |
Display maximum command-line argument length. |
getconf OPEN_MAX |
Display maximum number of open files. |
getconf PAGE_SIZE |
Display memory page size. |
getconf NAME_MAX /tmp |
Display maximum filename length for a filesystem. |
33. Complete Practical Example
34. System Information API Comparison
| API | Purpose |
|---|---|
uname() |
Retrieve operating system information. |
gethostname() |
Retrieve host name. |
sysconf() |
Retrieve runtime system configuration values. |
pathconf() |
Retrieve configuration values for a pathname. |
fpathconf() |
Retrieve configuration values for an open file descriptor. |
35. Complete Information Retrieval Flow
36. Best Practices
- Always prefer standardized APIs over platform-specific interfaces.
- Check every function's return value before using the result.
- Use
sysconf()andpathconf()instead of hardcoded limits. - Handle indeterminate configuration values correctly.
- Write applications that adapt to the executing environment.
37. Common Programming Mistakes
- Ignoring function return values.
- Hardcoding system limits.
- Assuming all UNIX systems have identical limits.
- Confusing compile-time constants with runtime values.
- Not checking errno when sysconf() returns -1.
38. Advanced Interview Questions
- Explain the purpose of
uname(). - When would you use
gethostname()instead ofuname()? - Why is
sysconf()preferred over hardcoded limits? - How do
pathconf()andfpathconf()differ? - What does an indeterminate value returned by
sysconf()mean? - Why should portable software avoid compile-time assumptions?
- Which API retrieves filesystem-specific configuration values?
- How does
getconfrelate tosysconf()? - Which information is returned by
struct utsname? - Why should applications query the operating system at runtime?
39. Module Summary
This module introduced the standardized interfaces used to obtain information about the operating system and its execution environment. It covered operating system identification, host names, runtime system configuration, filesystem-specific configuration values, practical examples, Linux utilities, and programming practices for writing portable UNIX applications.
Concept Mastery Assessment — Module 32
Verify your conceptual understanding with 12 targeted technical assessment questions. Instant feedback and explanations are provided for every choice.