Process Resources

 

Chapter 36: Process Resources
Linux System Programming – TLPI Series | EmbeddedPathashala
28
HTML Files
15
Resource Limits
3
Core Syscalls
50+
Interview Q&As

About This Chapter

Chapter 36 of TLPI covers everything a Linux systems programmer needs to know about process resource management. Every running process consumes resources — CPU time, virtual memory, file descriptors, signal queues. Linux gives you full programmatic control over monitoring (getrusage()) and limiting (getrlimit() / setrlimit()) these resources.

This chapter is split into 28 separate HTML files — one for every section and subsection. Each file covers theory, a code example in a pre/code block, and interview questions.

Key Terms

getrusage() getrlimit() setrlimit() struct rusage struct rlimit RUSAGE_SELF RUSAGE_CHILDREN soft limit hard limit RLIM_INFINITY RLIMIT_AS RLIMIT_CPU RLIMIT_NOFILE RLIMIT_NPROC RLIMIT_STACK ulimit SIGXCPU SIGXFSZ CAP_SYS_RESOURCE /proc/PID/limits

Section 36.1 — Process Resource Usage (getrusage)

📄 36.1 — getrusage() Overview

Introduction to the getrusage() system call. Covers the function signature, the three who flags (RUSAGE_SELF, RUSAGE_CHILDREN, RUSAGE_THREAD), what the call returns, and when to use each flag.

getrusage() RUSAGE_SELF RUSAGE_CHILDREN RUSAGE_THREAD

→ Open

📄 36.1a — struct rusage Fields

Deep dive into all 17 fields of struct rusage. Explains which 9 are actually used on Linux, which 8 are BSD legacy zeros, and what each used field means — CPU time, page faults, context switches, RSS.

ru_utime ru_stime ru_maxrss ru_minflt / ru_majflt ru_nvcsw / ru_nivcsw

→ Open

📄 36.1b — RUSAGE_CHILDREN Accumulation

How child resource usage is accumulated through the process tree. The wait() dependency rule, grandchild scenario, ru_maxrss is a max not a sum for children, and the pre-2.6.9 SIGCHLD deviation bug.

RUSAGE_CHILDREN wait() dependency grandchild rule SIGCHLD deviation

→ Open

Section 36.2 — Process Resource Limits

📄 36.2 — Resource Limits Introduction

What resource limits are, why they matter, the ulimit/limit shell commands, and how /proc/PID/limits (since 2.6.24) lets you view any process’s limits without writing code.

ulimit /proc/PID/limits why limits exist

→ Open

📄 36.2a — getrlimit() and setrlimit() API

Function signatures, the struct rlimit with rlim_cur and rlim_max, the rlim_t data type, RLIM_INFINITY, and safe printing with (long long) cast and %lld format specifier.

getrlimit() setrlimit() struct rlimit RLIM_INFINITY rlim_t printing

→ Open

📄 36.2b — Soft Limits, Hard Limits, and Inheritance Rules

Who can change what: privileged vs unprivileged processes, irreversibility of hard limit lowering, CAP_SYS_RESOURCE, fork/exec inheritance, per-user-ID scoped limits, and the complete Table 36-1 of all 15 constants.

soft vs hard limit CAP_SYS_RESOURCE fork() / exec() inheritance per-user-ID measurement

→ Open

📄 36.2c — Example Programs: printRlimit() and rlimit_nproc

Full walkthrough of Listing 36-2 (printRlimit helper) and Listing 36-3 (RLIMIT_NPROC demo). Explains why only 4 children were created (26 pre-existing), EAGAIN error handling, and zombie accumulation.

printRlimit() rlimit_nproc demo EAGAIN zombie accumulation

→ Open

📄 36.2d — Unrepresentable Limit Values

The 32-bit vs 64-bit rlim_t mismatch on x86-32. RLIM_SAVED_CUR and RLIM_SAVED_MAX constants, what happens when _FILE_OFFSET_BITS=64 expands rlim_t to 64 bits, and glibc’s silent RLIM_INFINITY conversion behaviour.

RLIM_SAVED_CUR RLIM_SAVED_MAX 32-bit rlim_t _FILE_OFFSET_BITS=64

→ Open

Section 36.3 — Details of Specific Resource Limits

📄 36.3 — Section 36.3 Introduction and Table 36-1

The Section 36.3 introduction: how limits get set in practice (ulimit, /proc, setrlimit), complete Table 36-1 with all 15 constants, categorisation by type (memory/CPU/file/IPC), and the three patterns of breach behaviour (signal+error, error only, signal only).

Table 36-1 limit categories breach patterns all 15 constants

→ Open

📄 36.3 — RLIMIT_AS

Virtual address space size limit. brk/sbrk/mmap/mremap/shmat fail with ENOMEM on breach. How malloc() hits this limit. Interaction with stack growth.

RLIMIT_AS virtual memory ENOMEM mmap / brk

→ Open

📄 36.3 — RLIMIT_CORE

Core dump file size limit. Setting to 0 disables core dumps entirely. Security use case (prevent memory contents being dumped to disk). RLIMIT_FSIZE interaction (whichever is smaller wins).

RLIMIT_CORE core dump disable core dumps RLIMIT_FSIZE interaction

→ Open

📄 36.3 — RLIMIT_CPU

CPU time limit in seconds. SIGXCPU at soft limit (once per second thereafter), SIGKILL at hard limit. Portable signal handler pattern. How UNIX implementations differ in post-SIGXCPU behaviour.

RLIMIT_CPU SIGXCPU SIGKILL signal handler

→ Open

📄 36.3 — RLIMIT_DATA

Data segment size limit — the sum of .data + .bss + heap. sbrk()/brk() fail with ENOMEM when the heap tries to grow past this limit. malloc() returns NULL as a result.

RLIMIT_DATA heap limit sbrk / brk ENOMEM

→ Open

📄 36.3 — RLIMIT_FSIZE

Maximum file size limit. SIGXFSZ signal AND EFBIG error are both generated simultaneously when write() or truncate() would exceed the soft limit. Default action is core dump.

RLIMIT_FSIZE SIGXFSZ EFBIG write / truncate

→ Open

📄 36.3 — RLIMIT_MEMLOCK

Locked (non-swappable) memory limit. Affects mlock(), mlockall(), and locking options of mmap() and shmctl(). MCL_FUTURE flag can cause later brk/mmap/mremap to fail if the limit is set too low.

RLIMIT_MEMLOCK mlock / mlockall MCL_FUTURE BSD-derived

→ Open

📄 36.3 — RLIMIT_MSGQUEUE

POSIX message queue byte limit per real user ID. The byte formula: maxmsg × sizeof(msg_msg*) + maxmsg × msgsize. The sizeof(msg_msg*) addend prevents zero-length message flooding. Linux-specific since 2.6.8.

RLIMIT_MSGQUEUE mq_open() byte formula zero-length msg overhead

→ Open

📄 36.3 — RLIMIT_NICE

Nice value ceiling for unprivileged processes. The formula: max nice = 20 − rlim_cur. Affects setpriority() and nice(). Linux-specific since 2.6.12. Why the inverted formula is used.

RLIMIT_NICE nice ceiling 20 − rlim_cur setpriority()

→ Open

📄 36.3 — RLIMIT_NOFILE

Maximum file descriptor number + 1. EMFILE on open/socket/pipe/dup. NR_OPEN hard ceiling (pre-2.6.25) vs /proc/sys/fs/nr_open (2.6.25+). System-wide file-max limit. sysconf(_SC_OPEN_MAX) behaviour.

RLIMIT_NOFILE EMFILE NR_OPEN nr_open / file-max

→ Open

📄 36.3 — RLIMIT_NPROC

Maximum processes/threads per real user ID. fork/vfork/clone fail with EAGAIN. threads-max system file. sysconf(_SC_CHILD_MAX) bug fixed in 2.6.23. Scanning /proc/PID/status Uid to estimate current count.

RLIMIT_NPROC EAGAIN on fork threads-max per-user-ID count

→ Open

📄 36.3 — RLIMIT_RSS

Resident set size limit — present but not enforced on modern Linux. Historical context: it had a minor effect on MADV_WILLNEED in old 2.4 kernels (up to 2.4.29). Why Linux never fully implemented it.

RLIMIT_RSS no-op MADV_WILLNEED history BSD-derived

→ Open

📄 36.3 — RLIMIT_RTPRIO

Realtime scheduling priority ceiling. Affects sched_setscheduler() and sched_setparam(). Linux-specific since 2.6.12. Important for embedded real-time Linux systems (PREEMPT_RT patch).

RLIMIT_RTPRIO sched_setscheduler RT priority ceiling EPERM

→ Open

📄 36.3 — RLIMIT_RTTIME

Realtime CPU time limit in microseconds — prevents a runaway RT process from locking the system. SIGXCPU at soft limit, SIGKILL at hard limit, same as RLIMIT_CPU. Linux-specific since 2.6.25.

RLIMIT_RTTIME microseconds SIGXCPU → SIGKILL RT safety

→ Open

📄 36.3 — RLIMIT_SIGPENDING

Maximum queued signals per real user ID. sigqueue() fails with EAGAIN on breach. kill() still works even at the limit (one instance of each non-queued signal). SigQ in /proc/PID/status. Linux-specific since 2.6.8.

RLIMIT_SIGPENDING sigqueue() EAGAIN kill() exception SigQ in /proc

→ Open

📄 36.3 — RLIMIT_STACK

Maximum stack size. SIGSEGV on overflow — requires alternate signal stack to catch. Since 2.6.23, this limit also controls space for command-line arguments and environment variables passed to execve().

RLIMIT_STACK SIGSEGV alternate signal stack argv/env space (2.6.23)

→ Open

Sections 36.4 and 36.5 — Summary and Exercises

📄 36.4 — Chapter Summary

Consolidated summary of the entire chapter. getrusage() and its three who flags, the soft/hard limit model, privilege rules, fork/exec inheritance, and a quick review of all 15 resource limit constants.

summary all concepts limit model recap

→ Open

📄 36.5 — Exercises and Solutions

All three chapter exercises with complete solution code. Exercise 36-1: RUSAGE_CHILDREN wait() dependency. Exercise 36-2: rusage command wrapper (like /usr/bin/time). Exercise 36-3: behaviour when consumption already exceeds the new soft limit.

Exercise 36-1 Exercise 36-2 Exercise 36-3 solution code

→ Open

Complete File List (28 Files)

# File Topic
1 ch36-index.html Master chapter index (this page)
2 ch36-intro.html Chapter introduction and overview
3 ch36-36.1-getrusage.html 36.1 getrusage() overview and who flags
4 ch36-36.1a-rusage-struct.html 36.1a struct rusage — all 17 fields
5 ch36-36.1b-rusage-children.html 36.1b RUSAGE_CHILDREN accumulation rules
6 ch36-36.2-resource-limits-intro.html 36.2 Resource limits introduction
7 ch36-36.2a-getrlimit-setrlimit.html 36.2a getrlimit() and setrlimit() API
8 ch36-36.2b-soft-hard-limits.html 36.2b Soft/hard limits, privileges, inheritance
9 ch36-36.2c-example-program.html 36.2c printRlimit() and rlimit_nproc programs
10 ch36-36.2d-unrepresentable-limits.html 36.2d RLIM_SAVED_CUR/MAX, 32-bit rlim_t issue
11 ch36-36.3-intro.html 36.3 Section intro + Table 36-1 all 15 limits
12 ch36-36.3-rlimit-as.html RLIMIT_AS — virtual address space
13 ch36-36.3-rlimit-core.html RLIMIT_CORE — core dump size
14 ch36-36.3-rlimit-cpu.html RLIMIT_CPU — CPU time, SIGXCPU, SIGKILL
15 ch36-36.3-rlimit-data.html RLIMIT_DATA — data segment / heap
16 ch36-36.3-rlimit-fsize.html RLIMIT_FSIZE — file size, SIGXFSZ, EFBIG
17 ch36-36.3-rlimit-memlock.html RLIMIT_MEMLOCK — locked memory
18 ch36-36.3-rlimit-msgqueue.html RLIMIT_MSGQUEUE — POSIX MQ bytes
19 ch36-36.3-rlimit-nice.html RLIMIT_NICE — nice value ceiling
20 ch36-36.3-rlimit-nofile.html RLIMIT_NOFILE — max file descriptors
21 ch36-36.3-rlimit-nproc.html RLIMIT_NPROC — process count per UID
22 ch36-36.3-rlimit-rss.html RLIMIT_RSS — RSS limit (no-op on Linux)
23 ch36-36.3-rlimit-rtprio.html RLIMIT_RTPRIO — realtime priority ceiling
24 ch36-36.3-rlimit-rttime.html RLIMIT_RTTIME — realtime CPU time
25 ch36-36.3-rlimit-sigpending.html RLIMIT_SIGPENDING — signal queue limit
26 ch36-36.3-rlimit-stack.html RLIMIT_STACK — stack size, SIGSEGV
27 ch36-36.4-summary.html 36.4 Chapter summary
28 ch36-36.5-exercises.html 36.5 Exercises with solutions

Ready to Learn?

Start from the beginning or jump to any section above.

Start Chapter → Jump to Summary

Leave a Reply

Your email address will not be published. Required fields are marked *