0% found this document useful (0 votes)
7 views11 pages

UNIXppt - Read-Only - Compatibility Mode

The document provides an overview of process control in UNIX, detailing key concepts such as process identifiers, the fork and vfork system calls, and methods for process termination. It explains how processes can be managed using commands like wait and waitpid to retrieve termination statuses and prevent zombie processes. Additionally, it covers advanced functions like wait3 and wait4 that return resource usage statistics for terminated processes.

Uploaded by

mtulsi1103
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views11 pages

UNIXppt - Read-Only - Compatibility Mode

The document provides an overview of process control in UNIX, detailing key concepts such as process identifiers, the fork and vfork system calls, and methods for process termination. It explains how processes can be managed using commands like wait and waitpid to retrieve termination statuses and prevent zombie processes. Additionally, it covers advanced functions like wait3 and wait4 that return resource usage statistics for terminated processes.

Uploaded by

mtulsi1103
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Process Control in UNIX

process control is a fundamental concept that allows you to


manage and manipulate running processes. It involves various
techniques and commands to monitor, start, stop, and modify the
behavior of processes.
Topics
• Process Identifiers
• Fork
• vfork
• exit
• wait
• waitpid
• wait3
• wait4
Process Identifiers
Process IDs (PIDs)
Each process in UNIX has a unique PID, a non-negative integer. This
ID distinguishes one process from another. PIDs are reused after
processes terminate.
Special PIDs
PID 0 -> is reserved for the scheduler process (swapper), part of the kernel.
PID 1 -> is reserved for the init process, the first user-space process.
PID 2 -> in systems with virtual memory, is assigned to the pagedaemon,
managing memory paging.

Additional Process Identifiers


• getpid() : Returns the PID of the calling process.
• getppid() : Returns the PID of the parent process.
• getuid() : Returns the real user ID of the calling process.
• geteuid() : Returns the effective user ID of the calling process.
• getgid() : Returns the real group ID of the calling process.
• getegid() : Returns the effective group ID of the calling process.
The Fork() System Call
Purpose of fork
• fork() is a system call that allows an existing process (the parent) to
create a new process (the child).
• The new child process is a duplicate of the parent process, inheriting
its code, data, and memory layout.
Return Values:
• In the parent: Returns the PID of the child process.
• In the child: Returns 0 to the child process.
• Error: If fork() fails, it returns -1.

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.

Use Cases of vfork()


• Optimized for exec(): The primary use of vfork() is when the
child process will immediately execute a new program using
exec() after creation.
• Performance Considerations: vfork() is more efficient than
fork() in these scenarios, especially when the child does not
need to modify the parent's memory.
exit()
A process can terminate normally in five ways:

1. Executing a return from the main function.


2. Calling the exit function.
3. Calling the _exit or _Exit function.
4. Executing a return from the start routine of the last thread in the
process.
5. Calling the pthread_exit function from the last thread in the process.
Once the process terminates, the kernel closes all the open descriptors
for the process, releases the memory that it was using.

Abnormal Process Termination


• Calling abort(): aborting a process.
• Receiving Signals: A process may terminate due to receiving signals
like segmentation faults or illegal memory accesses.
• Thread Cancellation: A thread might terminate due to cancellation
requests (either deferred or immediate).
Parent Process and Termination Status
The parent process can check the child’s termination status using the wait()
or waitpid() functions.
The parent can use macros to interpret the child’s termination status:
• WIFEXITED(status): Returns true if the child terminated normally. Use
WEXITSTATUS(status) to fetch the child’s exit status.
• WIFSIGNALED(status): Indicates abnormal termination due to a signal. Use
WTERMSIG(status) to fetch the signal that caused termination.
• WIFSTOPPED(status): Returns true if the child process is currently stopped.
Use WSTOPSIG(status) to get the signal that stopped the child.
• WIFCONTINUED(status): Returns true if the child resumed after being
stopped.

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);

idtype: Specifies the type of process to wait for:


• P_PID: Wait for a specific process ID.
• P_PGID: Wait for processes in a specific process group.
• P_ALL: Wait for any child process.
infop: Points to a siginfo_t structure containing detailed state-change
information.

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

these functions extend wait and waitpid by returning resource usage


statistics for the terminated child.
Resource usage includes:
• CPU time (user and system).
• Page faults.
• Signals received.

Prototypes:

pid_t wait3(int *statloc, int options, struct rusage *rusage);


pid_t wait4(pid_t pid, int *statloc, int options, struct rusage *rusage);

Differences:

wait3: Similar to wait but includes resource usage.


wait4: Similar to waitpid but includes resource usage and support for process
selection.

You might also like