UNIXppt - Read-Only - Compatibility Mode
UNIXppt - Read-Only - Compatibility Mode
Key Characteristics:
• Parent and Child: After fork(), both the parent and child continue
executing from the instruction immediately following the fork().
• Memory: The parent and child share the text segment (code), but each
has its own data space, heap, and stack.
• Copy-on-Write (COW): To optimize performance, memory regions are
initially shared and made read-only. If either process tries to modify
them, the kernel creates a copy of the page for that process.
The Fork() System Call
Inherited Attributes
• The child inherits many properties of the parent, such as:
• User IDs: Real and effective user/group IDs.
• Process-related IDs: Process group, session ID, and controlling
terminal.
• Environment: Environment variables, memory mappings, and
resource limits.
• File Descriptors: The parent and child share file descriptors and
file locks.
Uses of fork
• Duplication: For concurrent execution of different sections
of code (common in network servers).
• Program Execution: The child can use exec() to run a new
program immediately after fork().
Vfork()
➢ The function vfork has the same calling sequence and same
return values as fork.
➢ The vfork function creates the new process, just like fork,
without copying the address space of the parent into the child,
as the child won't reference that address space.
➢ vfork guarantees that the child runs first, until the child calls
exec or exit. When the child calls either of these functions, the
parent resumes.
Zombie Processes
• A process that has terminated but has not yet had its status retrieved by
the parent process.
• A zombie process occurs when a child process terminates, but the parent
has not yet called wait() or waitpid() to retrieve its termination status.
• If the parent doesn’t call wait(), the child remains in a zombie state.
• Zombie Prevention: The init process (PID 1) adopts orphaned processes
(those whose parents terminate) and ensures their termination
wait and waitpid Functions
Purpose of wait and waitpid
When a child process terminates, it sends the SIGCHLD signal to its
parent. The parent can then use wait() or waitpid() to fetch the child's
termination status and handle the child’s termination appropriately.
These functions help the parent process determine whether the child
terminated normally, abnormally, or was stopped.
wait() Function
Prototype:
pid_t wait(int *statloc);
Behavior:
• The parent process can block until one of its children terminates.
• If a child has already terminated and is a zombie, wait() immediately
returns with the termination status.
• If the parent has no children, it returns immediately with an error (-1).
• The termination status is returned via the statloc argument (if
provided).
• Blocking behavior: If no child has terminated, wait() blocks the parent
until one does.
waitpid() Function
Prototype:
pid_t waitpid(pid_t pid, int *statloc, int options);
Behavior:
Non-blocking options: waitpid() has more flexibility, allowing the parent to
avoid blocking with the option.
The parent can specify which child to wait for:
• pid == -1: Waits for any child (same behavior as wait()).
• pid > 0: Waits for a specific child with the given PID.
• pid == 0: Waits for any child in the same process group as the parent.
• pid < -1: Waits for any child in a specific process group.
statloc stores the child's termination status, which can be interpreted using
macros.
Macros to Examine Termination Status
• WIFEXITED(status): Checks if the child terminated normally.
• Use WEXITSTATUS(status) to get the exit status.
• WIFSIGNALED(status): Checks if the child terminated abnormally
• Use WTERMSIG(status) to get the signal number.
• WIFSTOPPED(status): Checks if the child is currently stopped.
• Use WSTOPSIG(status) to get the signal that caused the stop.
• WIFCONTINUED(status): Checks if the child was continued after being
stopped.
waitid Function
• waitid is a extensted version of waitpid
• Provides greater flexibility than waitpid by splitting process
identification into two separate arguments.
Prototype:
int waitid(idtype_t idtype, id_t id, siginfo_t *infop, int options);
Options:
• WEXITED: Wait for processes that have exited.
• WSTOPPED: Wait for stopped processes.
• WCONTINUED: Wait for continued processes.
• WNOHANG: Non-blocking operation.
• WNOWAIT: Do not clear the child’s termination status, allowing
subsequent calls to retrieve it.
wait3 and wait4 Functions
Prototypes:
Differences: