Practicals
Semester-05
OSL
Assignment 02 B

Assignment-02-B

Title

Implement a C Program to Sort an Array in Parent Process and Display it in Reverse Order in Child Process Using fork() and execve()

Theory

In this assignment, we will implement a C program that demonstrates the use of process control system calls fork() and execve(). The parent process will sort an array and then create a child process using fork(). The child process will use execve() to load a new program, which will display the sorted array in reverse order.

  • fork(): This system call is used to create a new process by duplicating the existing process. The new process is referred to as the child process.
  • execve(): This system call is used to execute a new program within a process. It replaces the current process image with a new process image.

Code Implementation

Parent Process (main program)

parent.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) {
        // Fork failed
        printf("Fork failed!\n");
        return 1;
    } else if (pid == 0) {
        // Child process
        char *args[n+2];
        args[0] = "./child";  // Name of the child process executable
 
        for (i = 0; i < n; i++) {
            char *arg = (char *)malloc(20);
            sprintf(arg, "%d", arr[i]);
            args[i+1] = arg;
        }
        args[n+1] = NULL;
 
        execve(args[0], args, NULL);
 
        // If execve fails
        perror("execve failed");
        return 1;
    } else {
        // Parent process
        printf("Parent process (PID: %d) sorting the integers...\n", getpid());
        bubbleSort(arr, n);
 
        printf("Parent process (PID: %d) sorted integers: ", getpid());
        for (i = 0; i < n; i++) {
            printf("%d ", arr[i]);
        }
        printf("\n");
 
        // Wait for child process to complete
        wait(NULL);
    }
 
    return 0;
}

Child Process

child.c

#include <stdio.h>
#include <stdlib.h>
 
int main(int argc, char *argv[]) {
    int n = argc - 1;
    int arr[n];
 
    for (int i = 0; i < n; i++) {
        arr[i] = atoi(argv[i+1]);
    }
 
    printf("Child process (PID: %d) displaying integers in reverse order: ", getpid());
    for (int i = n-1; i >= 0; i--) {
        printf("%d ", arr[i]);
    }
    printf("\n");
 
    return 0;
}

Setup and Run

Step 1: Save the Code

Save the parent process code as parent.c and the child process code as child.c.

Step 2: Compile the Code

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

gcc -o parent parent.c
gcc -o child child.c

Step 3: Run the Parent Program

Run the parent program:

./parent

Explanation

  1. Sorting in Parent Process:

    • The parent process accepts an array of integers from the user and sorts it using the Bubble Sort algorithm.
  2. Forking a Process:

    • The fork() system call creates a child process. If fork() returns a negative value, the creation of the child process was unsuccessful. If it returns zero, it indicates that this is the child process. If it returns a positive value, it indicates that this is the parent process.
  3. Executing a New Program in Child Process:

    • The child process uses the execve() system call to replace its process image with a new program (child). This new program displays the sorted array in reverse order.

By running this program, you will observe the creation and management of child processes using fork(), and see how the execve() system call can be used to execute a new program within a process.


Contact

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