WikiGalaxy

Personalize

Introduction to Process Management

Overview:

  • Process management is a fundamental concept in operating systems, responsible for managing the execution of processes in a system.
  • It involves creating, scheduling, and terminating processes, ensuring efficient CPU utilization.
  • Processes are instances of running programs, each having a unique process ID (PID).

Process Creation

Process Creation:

  • Processes are created through system calls like fork() in UNIX-based systems.
  • Parent processes can create child processes, forming a hierarchy.
  • Process creation involves allocating resources such as memory and I/O.

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

int main() {
    pid_t pid = fork();
    if (pid == 0) {
        printf("Child Process\n");
    } else {
        printf("Parent Process\n");
    }
    return 0;
}
    

Explanation:

  • The fork() system call creates a new process by duplicating the existing process.
  • The child process receives a PID of 0, and the parent gets the child's PID.
  • This example demonstrates basic process creation and the distinction between parent and child processes.

Console Output:

Parent Process
Child Process

Process Scheduling

Process Scheduling:

  • Process scheduling is crucial for multitasking, determining which process runs at a given time.
  • Schedulers are classified into long-term, short-term, and medium-term schedulers.
  • Scheduling algorithms include FCFS, SJF, Round Robin, and Priority Scheduling.

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

int main() {
    for (int i = 0; i < 5; i++) {
        if (fork() == 0) {
            printf("Process %d\n", i);
            break;
        }
    }
    return 0;
}
    

Explanation:

  • This example illustrates a simple process scheduling scenario using a loop to create multiple processes.
  • Each iteration creates a new child process, simulating a scheduling environment.
  • Understanding scheduling helps optimize CPU time and improve process efficiency.

Console Output:

Process 0
Process 1
Process 2
Process 3
Process 4

Process Termination

Process Termination:

  • Process termination occurs when a process completes its execution or is terminated by the system.
  • System calls like exit() are used for normal termination, while kill() can terminate processes forcefully.
  • Proper termination ensures resource deallocation and system stability.

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

int main() {
    printf("Process Starting\n");
    exit(0);
    printf("This will not be printed\n");
    return 0;
}
    

Explanation:

  • The exit() function terminates the process, deallocating resources and returning control to the OS.
  • After exit(), any code written is not executed, emphasizing the point of termination.
  • Understanding process termination is crucial for managing system resources effectively.

Console Output:

Process Starting

Inter-process Communication (IPC)

Inter-process Communication (IPC):

  • IPC allows processes to communicate and synchronize their actions.
  • Methods include pipes, message queues, shared memory, and semaphores.
  • IPC is essential for process collaboration and data exchange.

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

int main() {
    int fd[2];
    pipe(fd);
    if (fork() == 0) {
        close(fd[0]);
        write(fd[1], "Hello", 5);
        close(fd[1]);
    } else {
        char buffer[6];
        close(fd[1]);
        read(fd[0], buffer, 5);
        buffer[5] = '\0';
        printf("Received: %s\n", buffer);
        close(fd[0]);
    }
    return 0;
}
    

Explanation:

  • This example uses a pipe for IPC, allowing communication between parent and child processes.
  • The child process writes to the pipe, and the parent reads from it, demonstrating data exchange.
  • IPC mechanisms are vital for developing complex applications requiring process interaction.

Console Output:

Received: Hello

Process States

Process States:

  • Processes transition through various states: New, Ready, Running, Waiting, and Terminated.
  • The state of a process is determined by its current activity and resource availability.
  • Understanding process states helps manage process lifecycle and scheduling effectively.

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

int main() {
    printf("Process Created\n");
    sleep(2);
    printf("Process Running\n");
    sleep(2);
    printf("Process Terminated\n");
    return 0;
}
    

Explanation:

  • This example simulates the transition of process states using sleep() to mimic waiting.
  • Understanding the process lifecycle is crucial for effective process management and resource allocation.
  • The example highlights the key states a process goes through during its execution.

Console Output:

Process Created
Process Running
Process Terminated

logo of wikigalaxy

Newsletter

Subscribe to our newsletter for weekly updates and promotions.

Privacy Policy

 • 

Terms of Service

Copyright © WikiGalaxy 2025