Practicals
Semester-05
OSL
Assignment 02 A

Assignment-02-A

Title

Process Control System Calls: Demonstration of FORK, EXECVE, and WAIT System Calls along with Zombie and Orphan States.

Theory

Process control system calls are used to create and manage processes in Unix-based operating systems. This assignment covers three fundamental system calls: fork(), execve(), and wait(), along with concepts of zombie and orphan states.

  • fork(): This system call is used to create a new process, called the child process, which runs concurrently with the parent process. The new process is a copy of the current process except for the returned value.

  • execve(): This system call replaces the current process image with a new process image. It is commonly used to run a new program.

  • wait(): This system call makes the parent process wait until all its child processes have terminated. This prevents the creation of zombie processes, which are processes that have completed execution but still have an entry in the process table.

  • Zombie State: A process that has completed execution but still has an entry in the process table. It occurs when the parent has not yet read the child's exit status.

  • Orphan State: A process whose parent process has terminated. Orphan processes are adopted by the init process.

Code Implementation

Example 1: Basic Usage of fork() and wait()

basic_fork.c

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
 
int main() {
    pid_t pid = fork();
 
    if (pid == 0) {
        // Child process
        sleep(5);
        printf("This is Child Process having id : %d\n", getpid());
        printf("This is Child Process having Parent id : %d\n", getppid());
    } else {
        // Parent process
        printf("This is Parent Process having id : %d\n", getpid());
        printf("This is Parent Process having child id : %d\n", pid);
        wait(NULL);  // Wait for the child process to complete
    }
    return 0;
}

Example 2: Sorting Integers Using fork() and wait()

sort_fork.c

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
 
void bubbleSort(int arr[], int n) {
    int i, j, temp;
    for (i = 0; i < n-1; i++) {
        for (j = 0; j < n-i-1; j++) {
            if (arr[j] > arr[j+1]) {
                temp = arr[j];
                arr[j] = arr[j+1];
                arr[j+1] = temp;
            }
        }
    }
}
 
int main() {
    int n, i;
    printf("Enter the number of integers to be sorted: ");
    scanf("%d", &n);
    int arr[n];
    printf("Enter the integers to be sorted: ");
    for (i = 0; i < n; i++) {
        scanf("%d", &arr[i]);
    }
    pid_t pid = fork();
    if (pid == 0) {
        // Child process
        printf("Child process sorting the integers using bubble sort algorithm...\n");
        bubbleSort(arr, n);
        printf("Sorted integers by child process: ");
        for (i = 0; i < n; i++) {
            printf("%d ", arr[i]);
        }
        printf("\n");
    } else if (pid > 0) {
        // Parent process
        printf("Parent process waiting for child process to complete...\n");
        wait(NULL);  // Wait for the child process to complete
        printf("Sorted integers by parent process: ");
        for (i = 0; i < n; i++) {
            printf("%d ", arr[i]);
        }
        printf("\n");
    } else {
        // fork failed
        printf("Fork failed!\n");
        return 1;
    }
    return 0;
}

Setup and Run

Step 1: Save the Code

Save the first code example as basic_fork.c and the second code example as sort_fork.c.

Step 2: Compile the Code

Open a terminal and navigate to the directory where the files are saved. Compile the programs using the GCC compiler:

gcc -o basic_fork basic_fork.c
gcc -o sort_fork sort_fork.c

Step 3: Run the Programs

Run the compiled programs:

./basic_fork
./sort_fork

Explanation

  • In the first example (basic_fork.c), fork() is used to create a child process. The parent process waits for the child process to complete using wait(), ensuring the child process does not become a zombie.

  • In the second example (sort_fork.c), fork() is used to create a child process that sorts an array of integers using bubble sort. The parent process waits for the child process to complete the sorting before printing the sorted integers.

By running these examples, you can observe how processes are created and managed in a Unix-like operating system, along with the prevention of zombie processes using the wait() system call.


Contact

For any questions or clarifications, please contact Parth Sali at parthsali04@gmail.com.