Processes in OS

A process is the execution of a program in an operating system (OS). It is a fundamental concept that forms the backbone of modern computing, as processes allow multiple programs to run simultaneously on a computer. This article explores the nature of processes, their lifecycle, types, and inter-process communication, supplemented by schematics and code examples.




What is a Process?

A process is an instance of a program in execution. It is more than just code; it includes the program counter, stack, data segment, and other resources needed during execution. Processes are independent and isolated from one another, ensuring system stability and security.




Process Lifecycle

A process goes through various states during its lifetime. These include:

1. New: The process is being created.


2. Ready: The process is prepared to run but is waiting for CPU allocation.


3. Running: The process is currently being executed by the CPU.


4. Waiting: The process is waiting for an event, such as I/O completion.


5. Terminated: The process has finished execution.




Schematic: Process States

+———+       +——-+       +———+
|  New    |—–> | Ready |—–> | Running |
+———+       +——-+       +———+
     |                  ^               |
     |                  |               v
     v                  |         +———+
+———+             |         | Waiting |
| Terminated|<———-+         +———+
+———+




Types of Processes

1. User Processes: Created and executed by user programs. Example: a word processor or a media player.


2. System Processes: Managed by the OS to perform core functions like memory management and scheduling.




Inter-Process Communication (IPC)

Processes often need to share information. This is achieved through IPC mechanisms like:

Pipes: Data is sent between processes in a unidirectional stream.

Shared Memory: Allows multiple processes to access a common memory space.

Message Queues: Data is sent in structured messages.

Sockets: Facilitates communication over a network.



Code Example: Process Creation in C Using fork()

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

int main() {
    pid_t pid;

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

    if (pid < 0) {
        perror(“Fork failed”);
        return 1;
    } else if (pid == 0) {
        // Child process
        printf(“Child process: PID = %d\n”, getpid());
    } else {
        // Parent process
        printf(“Parent process: PID = %d\n”, getpid());
    }

    return 0;
}



Process Scheduling

To efficiently utilize the CPU, the OS employs scheduling algorithms:

First-Come, First-Served (FCFS): Processes are executed in the order they arrive.

Shortest Job Next (SJN): Prioritizes the shortest jobs.

Round Robin (RR): Allocates fixed time slices to processes in a cyclic order.

Priority Scheduling: Executes processes based on priority levels.




Applications of Processes

1. Multitasking: Allows multiple applications to run simultaneously, such as browsing the web while listening to music.


2. System Monitoring: System processes handle real-time monitoring and resource allocation.


3. Servers: Web servers utilize multiple processes to handle client requests efficiently.



Conclusion

Processes are the building blocks of operating systems, enabling multitasking, resource sharing, and efficient system operation. Understanding their lifecycle, management, and communication is essential for developing robust and efficient software solutions.

The article above is rendered by integrating outputs of 1 HUMAN AGENT & 3 AI AGENTS, an amalgamation of HGI and AI to serve technology education globally.

(Article By : Himanshu N)