Operating System Tutorial

Operating System Tutorial Types of Operating System Evolution of Operating System Functions of Operating System Operating System Properties Operating System Services Components of Operating System Needs of the Operating System

Operating Systems

Linux Operating System Unix Operating System Ubuntu Operating System Chrome Operating Systems Fedora Operating System MAC Operating System MS Windows Operating System Solaris Operating System Cooperative Operating System CorelDRAW Operating System CentOS FreeBSD Operating Systems Batch Operating System MS-DOS Operating System Commercial Mobile Operating Systems

Differences

Difference Between Multi-programming and Multitasking Difference between C-LOOK and C-SCAN Difference between Rotational Latency and Disk Assess Time Trap vs Interrupt Difference between C-SCAN and SSTF Difference between SCAN and FCFS Difference between Seek Time and Disk Access Time Difference between SSTF and LOOK Difference between Process and Program in the Operating System Difference between Protection and Security in Operating System

How To

How to implement Monitors using Semaphores How to Install a Different Operating System on a PC

Questions

What is Kernel and Types of Kernel What is DOS Operating System What is Thread and Types of Thread What is Process Scheduler and Process Queue What is Context Switching What is CPU Scheduling What is Producer-Consumer Problem What is Semaphore in Operating System Monitors in Operating System What is Deadlock What is Paging and Segmentation What is Demand Paging What is Virtual Memory What is a Long term Scheduler What is Page Replacement in Operating System What is BSR Mode What is Convoy Effect What is Job Sequencing in Operating System Why is it critical for the Scheduler to distinguish between I/O-bound and CPU-bound programs Why is there a Need for an Operating System

Misc

Process Management Process State Scheduling Algorithm FCFS (First-come-First-Serve) Scheduling SJF (Shortest Job First) Scheduling Round-Robin CPU Scheduling Priority Based Scheduling HRRN (Highest Response Ratio Next) Scheduling Process Synchronization Lock Variable Mechanism TSL Mechanism Turn Variable Mechanism Interested Variable Mechanism Deadlock Avoidance Strategies for Handling Deadlock Deadlock Prevention Deadlock Detection and Recovery Resource Allocation Graph Banker’s Algorithm in Operating System Fixed Partitioning and Dynamic Partitioning Partitioning Algorithms Disk Scheduling Algorithms FCFS and SSTF Disk Scheduling Algorithm SCAN and C-SCAN Disk Scheduling Algorithm Look and C-Look Disk Scheduling Algorithm File in Operating System File Access Methods in Operating System File Allocation Method Directory Structure in Operating System N-Step-SCAN Disk Scheduling Feedback Queue in Operating System Contiguous Memory Allocation in Operating System Real-time Operating System Starvation in Operating System Thrashing in Operating System 5 Goals of Operating System Advantages of Operating System Advantages of UNIX Operating System Bit Vector in Operating System Booting Process in Operating System Can a Computer Run Without the Operating System Dining Philosophers Problem in Operating System Free Space Management in Operating System Inter Process Communication in Operating System Swapping in Operating System Memory Management in Operating System Multiprogramming Operating System Multitasking Operating Systems Multi-user Operating Systems Non-Contiguous Memory Allocation in Operating System Page Table in Operating System Process Scheduling in Operating System Segmentation in Operating System Simple Structure in Operating System Single-User Operating System Two Phase Locking Protocol Advantages and Disadvantages of Operating System Arithmetic operations in binary number system Assemblers in the operating system Bakery Algorithm in Operating System Benefits of Ubuntu Operating System CPU Scheduling Criteria in Operating System Critical Section in Operating System Device Management in Operating System Linux Scheduler in Operating System Long Term Scheduler in Operating System Mutex in Operating System Operating System Failure Peterson's Solution in Operating System Privileged and Non-Privileged Instructions in Operating System Swapping in Operating System Types of Operating System Zombie and Orphan Process in Operating System 62-bit operating system Advantages and Disadvantages of Batch Operating System Boot Block and Bad Block in Operating System Contiguous and Non - Contiguous Memory Allocation in Operating System Control and Distribution Systems in Operations Management Control Program in Operating System Convergent Technologies in Operating System Convoy Effect in Operating System Copy Operating Systems to SSD Core Components of Operating System Core of UNIX Operating System Correct Value to return to the Operating System Corrupted Operating System Cos is Smart Card Operating System Cosmos Operating Systems Examples Generation of Operating System Hardware Solution in Operating System Process Control Block in Operating System Function of Kernel in Operating System Operating System Layers History of Debian Operating Systems Branches and Architecture of Debian Operating Systems Features and Packages of Debian Operating Systems Installation of Operating System on a New PC Organizational Structure and Development in Debian Operating Systems User Interface in Operating System Types Of Memory in OS Operating System in Nokia Multilevel Paging in OS Memory Mapping Techniques in OS Memory Layout of a Process in Operating System Hardware Protection in Operating System Functions of File Management in Operating System Core of Linux Operating System Cache Replacement Policy in Operating System Cache Line and Cache Size in Operating System What is Memory Mapping? Difference Between Network Operating System And Distributed Operating System What is the difference between a Hard link and a Soft Link? Principles of Preemptive Scheduling Process Scheduling Algorithms What is NOS? What is the Interrupt I/O Process? What is Time Sharing OS What is process termination? What is Time-Sharing Operating System What is Batch File File system manipulation What is Message-passing Technique in OS Logical Clock in Distributed System

Zombie and Orphan Process in Operating System

What is Zombie Process in OS?

A zombie process is a term used in operating system (OS) to refer to a process that has completed its execution but still has an entry in the process table. In other words, it is a process that has finished its task but still exists in the process table.

When a child process completes its execution and terminates, it sends a signal to its parent process indicating its exit status. However, if the parent process fails to acknowledge this signal, the child process remains in the process table as a zombie process.

Zombie processes do not consume any system resources, but they still occupy space in the process table, which can lead to a shortage of available process IDs. If too many zombie processes accumulate, they can cause the system to become slow and unresponsive.

To avoid zombie processes, it is important for the parent process to handle the SIGCHLD signal, which is sent by the child process when it terminates. The parent process should use the wait() system call to retrieve the exit status of the child process and remove it from the process table.

What does happen to Zombie Process?

Zombie processes are already dead and cannot be killed. However, they occupy space in the process table, which can cause a shortage of available process IDs and make the system slow and unresponsive.

The only way to get rid of zombie processes is to remove their entry from the process table. This can be done by having the parent process handle the SIGCHLD signal and call the wait() system call to retrieve the exit status of the child process and remove it from the process table.

If the parent process does not handle the SIGCHLD signal and retrieve the exit status of the child process, the zombie process will remain in the process table until the system is rebooted. Therefore, it's important for the parent process to properly handle the termination of its child processes to avoid accumulating zombie processes.

Example of Zombie Process in OS

Here is an example code in C that creates a zombie process:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
int main() {
pid_tchild_pid = fork();
if (child_pid == -1) {
perror("fork");
exit(1);
    } else if (child_pid == 0) {
        // Child process
printf("Child process is running...\n");
exit(0);
    } else {
        // Parent process
printf("Parent process is running...\n");
        // The parent process does not handle the SIGCHLD signal
        // or call the wait() system call to retrieve the exit status
        // of the child process and remove it from the process table.
sleep(10); // Let the child process exit and become a zombie
printf("Parent process is exiting...\n");
    }
return 0;
}

Output:

Parent process is running...
Child process is running...
Parent process is exiting...

Explanation:

In this example, the parent process creates a child process using the fork() system call. The child process prints a message and then exits using the exit() system call. The parent process does not handle the SIGCHLD signal or call the wait() system call to retrieve the exit status of the child process and remove it from the process table. Instead, it waits for 10 seconds using the sleep() system call and then exits.

When the child process exits, it becomes a zombie process because the parent process does not retrieve its exit status. The zombie process will remain in the process table until the system is rebooted or until the parent process handles the SIGCHLD signal and calls the wait() system call to remove it from the process table.

Why is zombie process harmful to the system?

Zombie processes can be harmful to a system for several reasons:

  • Resource wastage: Zombie processes occupy space in the process table and consume some system resources, such as a process ID. If too many zombie processes accumulate, they can cause a shortage of available process IDs, which can lead to the failure of new processes to start.
  • System slowdown: Zombie processes can accumulate and cause the system to become slow and unresponsive because the kernel has to maintain their entry in the process table.
  • Security risk: Zombie processes can be a security risk because they may be used by attackers to perform malicious activities. For example, attackers can create a large number of zombie processes to consume system resources and bring down the system.
  • Reduced system availability: Zombie processes can reduce the system's availability because they can occupy system resources and prevent new processes from starting. This can lead to downtime, which can be costly for organizations.

What is Orphan Process?

An orphan process is a process that has been created by another process (called the parent process), but the parent process has terminated or exited before the child process. As a result, the child process is left without a parent process.

Orphan processes are still running processes, but they are not being monitored by their parent process. This can cause issues because the parent process may have allocated resources to the child process that it may not release properly, which can lead to resource leaks.

By default, orphan processes are adopted by the init process (process ID 1) in UNIX-based operating systems. The init process becomes the new parent process of the orphan process and is responsible for monitoring and managing it. This ensures that resources are properly released when the orphan process exits.

In general, orphan processes are not harmful to the system, but they can cause issues if they are left unattended for a long time. Therefore, it's important to ensure that the parent process of a child process properly handles its termination to avoid leaving behind orphan processes.

What does happen with the Orphan Processes?

When a parent process terminates before its child processes, the child processes become orphan processes. Orphan processes are still running processes, but they are not being monitored by their parent process.

In UNIX-based operating systems, orphan processes are automatically adopted by the init process (process ID 1). The init process becomes the new parent process of the orphan process and is responsible for monitoring and managing it. The init process periodically checks the status of orphan processes and reaps them when they exit.

If an orphan process is left unattended for a long time, it can consume system resources, such as memory or CPU time, and lead to resource leaks. Therefore, it's important to ensure that the parent process of a child process properly handles its termination to avoid leaving behind orphan processes.

In general, orphan processes are not harmful to the system, but they can cause issues if they are left unattended for a long time. By adopting orphan processes, the init process ensures that resources are properly released when the orphan process exits and that the system remains stable and responsive.

Example of Orphan Process in OS

Here is an example code in C that creates an orphan process:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main() {
pid_tchild_pid = fork();
if (child_pid == -1) {
perror("fork");
exit(1);
    } else if (child_pid == 0) {
        // Child process
printf("Child process is running...\n");
sleep(10); // Let the child process become an orphan
printf("Child process is exiting...\n");
exit(0);
    } else {
        // Parent process
printf("Parent process is running...\n");
exit(0);
    }
return 0;
}

Output:

Parent process is running...

Explanation:

In this example, the parent process creates a child process using the fork() system call. The child process prints a message and then waits for 10 seconds using the sleep() system call to become an orphan process. The parent process prints a message and then immediately exits using the exit() system call, leaving the child process as an orphan process.

When the parent process exits, the child process becomes an orphan process because it has no parent process. The orphan process continues to run, but it is not being monitored by its parent process. The init process will adopt the orphan process and become its new parent process, ensuring that it is properly monitored and managed.

Why is Orphan Process harmful to the system?

Having too many orphan processes can be harmful to a system for several reasons:

  • Resource wastage: Orphan processes can consume system resources such as memory, CPU time, and other system resources. If too many orphan processes accumulate, they can cause a shortage of available system resources, which can lead to system slowdowns and instability.
  • Security risk: Orphan processes can be a security risk because they may be used by attackers to perform malicious activities. For example, attackers can create a large number of orphan processes to consume system resources and bring down the system.
  • Difficult to manage: When there are too many orphan processes in the system, it can become difficult to manage them. Since the init process is responsible for managing orphan processes, it may become overloaded if there are too many of them. This can cause delays in orphan process reaping and may cause the system to become slow and unresponsive.
  • Reduced system availability: Orphan processes can reduce the system's availability because they can occupy system resources and prevent new processes from starting. This can lead to downtime, which can be costly for organizations.