Task creation, program replacement và parent-child lifecycle

Process identity/lifecycle tách khỏi program image: fork/clone tạo task, exec thay image, exit tạo trạng thái chờ parent reap.

Process Lifecycle

Kernel task model

Linux kernel đại diện execution bằng task; thread group chia sẻ TGID và mỗi task có TID. User space gọi fork, vfork, clone/clone3 với sharing flags; pthread implementations dùng clone semantics. PID namespace làm ID nhìn thấy khác theo namespace.

fork và copy-on-write

fork tạo child với virtual mappings gần giống parent, page tables đánh COW thay vì copy toàn bộ pages. Hai bên ghi sẽ fault/copy page. Multithreaded process fork có caveat: child chỉ giữ calling thread; trước exec chỉ async-signal-safe operations an toàn theo POSIX.

execve

execve thay address space/program, stack/argv/env và reset nhiều attributes; PID thường giữ. File descriptors không có FD_CLOEXEC vẫn tồn tại, gây secret/socket leak sang child. Credentials/setuid, signal dispositions, mappings và threads có rules riêng.

Exit, wait và zombies

Exit đóng/release resources nhưng giữ minimal status/accounting thành zombie cho parent wait. Parent không reap tạo zombie accumulation; orphan được reparent tới subreaper/init. Zombie không giữ heap nhưng tiêu PID/task table entry.

Process groups/sessions

Process groups và sessions hỗ trợ job control/signal fan-out; controlling terminal gửi signals như SIGINT/SIGHUP. Service managers/container runtimes quản cgroup/process tree, không nên kill chỉ một PID nếu children độc lập.

Failure modes

Fork failure do PID/memory/cgroup limits; exec failure để child chạy nhầm code path; leaked descriptors; zombie/orphan; fork bomb; unsafe fork trong multithreaded runtime. Use posix_spawn/process APIs và explicit close-on-exec/lifecycle ownership.

clone(2) · fork(2) · execve(2) · wait(2)
← Mục lụcMemory →