Threads, clocks and credentials
Quick answer
Threads share many process resources but have independent execution state. Use synchronization for shared data, a monotonic clock for elapsed time, and inspect credentials and limits to understand permissions and constraints.
A wall clock can change while an operation is in progress, making it unsuitable for ordinary elapsed-duration measurement. Use monotonic time for durations and wall time for human-facing event timestamps. Shared state still needs synchronization even when individual threads have their own execution context.
Prepare the practice environment
Run python3 lab-fixtures/syscalls/setup.py from the extracted root as an ordinary user. Enter the printed fresh private path and check pwd. Use fresh fixtures, Bash with set -e -o pipefail, LC_ALL=C and umask 077. Run each snippet in a separate Bash process, not your interactive shell. Examples use Python 3 standard-library wrappers on Linux, private files, bounded child processes and local socketpairs. No root or external network is needed. Optional strace inspection requires an authorized environment. Review the lab guide.
Prerequisites
Review: Memory mapping and shared memory.
How it works
POSIX threads share the address space and open descriptors while each has a stack, signal mask and thread identity. Linux schedules individual tasks; pthread_create and pthread_join create and collect threads, while mutexes protect critical sections. Python threading wraps native threads, but interpreter behavior such as the GIL is not a substitute for synchronization. CLOCK_REALTIME describes wall time and can jump; CLOCK_MONOTONIC does not step backward on wall-clock adjustment and is appropriate for elapsed intervals (Linux excludes suspended time). Real/effective/saved UID and GID, supplementary groups, capabilities and namespaces influence authorization. Linux kernel credentials are per-thread; POSIX libc wrappers coordinate credential changes across threads. Resource limits have soft and hard values; RLIMIT_NOFILE bounds descriptor numbers, not CPU load. Read only the current process identity and limits. See pthreads(7), clock_gettime(3), credentials(7), getrlimit(2).
Try the examples
python3 - <<'PYCODE'import threading, oslock = threading.Lock()count = 0ids = set()ready = threading.Barrier(2, timeout=2)def worker(): global count with lock: ids.add(threading.get_native_id()) assert os.getpid() == parent ready.wait() for _ in range(100): with lock: count += 1parent = os.getpid()threads = [threading.Thread(target=worker) for _ in range(2)]for t in threads: t.start()for t in threads: t.join()assert count == 200 and len(ids) == 2print('shared counter=200; workers joined')PYCODEThe lock protects the shared counter and join waits for completion. Thread IDs and PID values are not fixed.
Expected stdout:
shared counter=200; workers joinedpython3 - <<'PYCODE'import timestart = time.clock_gettime_ns(time.CLOCK_MONOTONIC)# A bounded local operation.assert sum(range(100)) == 4950end = time.clock_gettime_ns(time.CLOCK_MONOTONIC)assert end >= startprint('nonnegative monotonic interval')PYCODEThe numeric duration varies; the check asserts only a nonnegative interval, not a benchmark.
Expected stdout:
nonnegative monotonic intervalpython3 - <<'PYCODE'import os, resourceuid, euid = os.getuid(), os.geteuid()soft, hard = resource.getrlimit(resource.RLIMIT_NOFILE)assert uid >= 0 and euid >= 0assert hard == resource.RLIM_INFINITY or (soft != resource.RLIM_INFINITY and soft <= hard)print('UIDs valid; descriptor limits consistent')PYCODEThe actual IDs and limits vary. No credentials or resource limits are changed.
Expected stdout:
UIDs valid; descriptor limits consistentCommon mistakes
Do not use wall-clock differences as reliable elapsed time; do not assume shared data is safe without locks or that UID alone explains all permissions.
Practice
Run the thread example with three workers and verify 300. Explain why join and lock solve different problems, and why the monotonic interval is not a wall-clock timestamp.
Related references
Study: ps. These are reference names, not deployed links.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Threads, clocks and credentials — Knowledge Check
Apply Linux systems programming interfaces to concrete local examples.
What do threads in a process generally share?
What protects a shared counter?
Which clock is suited to elapsed intervals?
What does RLIMIT_NOFILE constrain?