0% found this document useful (0 votes)
14 views6 pages

Unix Subject

The document provides an overview of Unix processes and signals, detailing the structure and behavior of processes, including their creation, identification, states, and termination. It explains the fork() system call for creating processes, the wait() function for synchronizing parent and child processes, and the concept of zombie processes. Additionally, it discusses the exec() system call for executing new programs within a process's context.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views6 pages

Unix Subject

The document provides an overview of Unix processes and signals, detailing the structure and behavior of processes, including their creation, identification, states, and termination. It explains the fork() system call for creating processes, the wait() function for synchronizing parent and child processes, and the concept of zombie processes. Additionally, it discusses the exec() system call for executing new programs within a process's context.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

UNIT- II

Unix Process and Signals

In Unix operating systems, processes are fundamental units of execution.

They are instances of running programs managed by the operating system's


kernel.

Processes can communicate with each other and with the kernel through
various mechanisms, one of which is signals.

1. Unix Processes:

 Definition: A process is an instance of a program in execution. It consists


of an address space, code, data, and other operating system resources
such as file descriptors, signals, etc.
 Creation: Processes are typically created by the fork() system call,
followed by an exec() call to replace the process memory with a new
program.
 Identification: Each process is identified by a unique Process ID (PID).
Parent processes can identify child processes through their PIDs.
 States: Processes can be in various states, including Running, Sleeping,
Stopped, Zombie, etc., depending on their activity and interaction with the
system.
 Termination: Processes can terminate voluntarily by calling exit() or
involuntarily due to errors or signals from the operating system.

process structure in unix

In Unix operating systems, a process is an instance of a running program.

It's a fundamental concept in operating systems and plays a crucial role in managing
resources, multitasking, and executing tasks concurrently.

Here's an overview of the typical structure of a process in Unix:

1. Process Control Block (PCB):


 Each process in Unix is represented by a Process Control Block (PCB), also
known as a Process Descriptor.
 PCB contains essential information about the process, including its state,
program counter, stack pointer, memory allocation, open files, and other
relevant information.
2. Program Code and Data:
 Every process has its program code and associated data, including variables,
data structures, etc.
 The program code consists of the instructions that the CPU executes to carry
out the tasks assigned to the process.
3. Address Space:
 Each process has its virtual address space, which includes the memory
locations accessible to the process.
 The address space typically includes the program's code, data, heap
(dynamically allocated memory), and stack (local variables, function call
stack).
4. Process State:
 Processes can be in various states, including:
 Running: Currently being executed by the CPU.
 Ready: Ready to execute but waiting for CPU time.
 Blocked: Waiting for an event such as I/O or a signal.
 Zombie: Terminated, but its parent process hasn't yet collected its exit
status.
 Sleeping: Voluntarily waiting for a certain amount of time.
 etc.
5. Process Identifier (PID):
 Each process is uniquely identified by its Process ID (PID), a non-negative
integer.
 The PID is used by the operating system to manage and control processes.
6. Parent-Child Relationship:
 Processes in Unix can have parent-child relationships.
 When a process creates another process (via fork() system call), the new
process is called the child, and the original process is called the parent.
7. File Descriptors:
 Unix processes have a table of file descriptors that represent open files, pipes,
sockets, etc.
 These file descriptors provide access to various I/O resources.
8. Signal Handlers:
 Processes can define signal handlers to catch and respond to signals sent by
the operating system or other processes.
9. Resource Usage:
 Processes may consume system resources such as CPU time, memory, I/O
operations, etc.
 The operating system manages and allocates these resources to processes
based on scheduling algorithms and priorities.

Understanding the structure and behavior of processes is crucial for efficient resource
management, multitasking, and inter-process communication in Unix-like operating
systems.
fork() system call:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

int main() {
pid_t pid; // variable to store the process ID

printf("Parent process starts...\n");

pid = fork(); // Create a child process

if (pid < 0) { // fork failed


fprintf(stderr, "Fork failed\n");
return 1;
} else if (pid == 0) { // Child process
printf("Child process starts...\n");
printf("Child process PID: %d\n", getpid());
printf("Child process parent PID: %d\n", getppid());
printf("Child process ends.\n");
exit(0);
} else { // Parent process
printf("Parent process continues...\n");
printf("Parent process PID: %d\n", getpid());
printf("Parent process parent PID: %d\n", getppid());
printf("Parent process waiting for child to finish...\n");
wait(NULL); // Wait for the child process to terminate
printf("Parent process ends.\n");
}

return 0;
}
 The fork() system call is used to create a new process. After fork(), both
the parent and child processes will execute the same code from the
point of the fork() call.
 fork() returns different values in the parent and child processes:
 In the parent process, fork() returns the process ID of the child.
 In the child process, fork() returns 0.
 The if (pid < 0) condition checks if fork() failed.
 The if (pid == 0) block is executed by the child process, and the else
block is executed by the parent process.
 getpid() returns the process ID of the current process.
 getppid() returns the parent process ID.
 exit(0) is called to terminate the child process.
 wait(NULL) in the parent process waits for the child process to terminate
before the parent process itself terminates.

Wait():

In Unix operating systems, you can wait for a process to terminate using the
wait() or waitpid() system calls.

These functions allow a parent process to suspend its execution until one of its
child processes exits or terminates. Here's how you can wait for a process in
Unix:

1. Using the wait() System Call:


 The wait() system call suspends the execution of the calling process
until any one of its child processes exits or terminates.
 It returns the process ID of the terminated child process, or -1 if an
error occurs.
2. Using the waitpid() System Call:
 The waitpid() system call allows more control over which child process to
wait for by specifying its process ID.
 It returns the process ID of the terminated child process, or 0 if the
WNOHANG option is used and no child process has exited yet, or -1 if an
error occurs.

zombie process:

In Unix operating systems, a zombie process (or defunct process) is a process


that has completed execution but still has an entry in the process table.

This entry is needed for the parent process to read its exit status. A zombie
process no longer consumes system resources other than its entry in the
process table.

Here's how a zombie process is created and how it can be handled:

1. Creating a Zombie Process:


When a child process terminates (calls exit() or returns from main()),
it sends a termination status to its parent process.
 However, the parent process may not immediately collect this status
using wait() or waitpid(). Until the parent collects the termination
status of the child, the child process remains a zombie.
 The zombie process still retains its process ID (PID) and its entry in
the process table.
2. Handling Zombie Processes:
 To prevent the accumulation of zombie processes, the parent
process should collect the termination status of its child processes
using the wait() or waitpid() system calls.
 When the parent process calls wait() or waitpid() to collect the
status of a terminated child process, the zombie process is removed
from the process table, and its resources are released.
 If the parent process fails to collect the termination status of its
child processes, they may remain zombies until the parent process
terminates.
 In certain cases, if the parent process cannot or does not handle the
termination of its child processes, you may need to consider
alternative strategies, such as using signals ( SIGCHLD) to handle
child termination asynchronously or restructuring the application to
avoid leaving zombie processes.

Here's an example in C illustrating the creation of a zombie process:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main() {
pid_t child_pid = fork();
if (child_pid == 0) {
// Child process
printf("Child process executing\n");
exit(0); // Child process exits normally
} else if (child_pid > 0) {
// Parent process
printf("Parent process sleeping\n");
sleep(10); // Parent process sleeps, delaying the collection of child
termination status
printf("Parent process woke up\n");
// Parent process does not call wait() or waitpid() to collect child
termination status
} else {
// Fork failed
perror("fork");
return 1;
}
return 0;
}

Exec():

The exec() system call in Unix operating systems is used to execute a new
program within the context of the current process.

It replaces the current process's memory image with a new one loaded from the
specified executable file.

The exec() system call is part of the process creation and management
mechanisms in Unix systems.

There are several variants of the exec() system call, each with slightly different
behaviors:

1. int execl(const char *path, const char *arg0, ... /* (char *) NULL */ );
 This variant of exec() takes the path of the executable file and
a variable number of arguments representing the
command-line arguments to be passed to the new program.
 The argument list must be terminated by a NULL pointer.

#include <stdio.h>
#include <unistd.h>
int main() {
printf("Executing ls command using execl\n");
execl("/bin/ls", "ls", "-l", NULL);
printf("This line will not be executed\n");
return 0;
}

You might also like