Skip to content

Build & Run

This section explains how to compile, execute, and modify the process-based parallel matrix multiplication program used in this project.


Program Overview

  • Process-Based Parallelism


    Matrix multiplication is divided across multiple child processes. Each child computes a subset of matrix rows.

  • Process Creation


    Uses fork() to create child processes and distribute workload.

  • Performance Measurement


    Execution time is measured using clock() after all processes complete.

  • Synchronization


    The parent process waits for all children using wait() before calculating total runtime.


Screenshots (Verification)

Code in nano editor

Code in nano editor

Head of the file (verification)

Head of the file


Source Code

matrix_fork.c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
#include <time.h>

#define N 600
#define PROCS 4

double A[N][N], B[N][N], C[N][N];

void initialize_matrices() {
    for (int i = 0; i < N; i++) {
        for (int j = 0; j < N; j++) {
            A[i][j] = rand() % 10;
            B[i][j] = rand() % 10;
            C[i][j] = 0;
        }
    }
}

void multiply(int start, int end) {
    for (int i = start; i < end; i++) {
        for (int j = 0; j < N; j++) {
            for (int k = 0; k < N; k++) {
                C[i][j] += A[i][k] * B[k][j];
            }
        }
    }
}

int main() {
    srand(time(NULL));
    initialize_matrices();

    clock_t start_time = clock();
    int rows_per_proc = N / PROCS;

    for (int p = 0; p < PROCS; p++) {
        pid_t pid = fork();
        if (pid == 0) {
            int start = p * rows_per_proc;
            int end = (p == PROCS - 1) ? N : start + rows_per_proc;
            multiply(start, end);
            exit(0);
        }
    }

    for (int p = 0; p < PROCS; p++) {
        wait(NULL);
    }

    clock_t end_time = clock();
    double time_taken =
        (double)(end_time - start_time) / CLOCKS_PER_SEC;

    printf("Execution Time: %.3f seconds\n", time_taken);
    return 0;
}

Compilation

  • Compile Using GCC


    gcc -Wall matrix_fork.c -o matrix_fork
    

    The -Wall flag enables compiler warnings to ensure code quality.


Execution

  • Run the Program


    ./matrix_fork
    

    Default configuration:
    N = 600, PROCS = 4


Expected Output

Execution Time: 0.001 seconds
Terminal Output Screenshot

Execution Time


Modifying Parameters

  • Open in Nano

nano matrix_fork.c
  • Adjust Configuration

#define N 800
#define PROCS 2
  • Recompile & Execute

Save (Ctrl + O), Exit (Ctrl + X), then recompile and run again.


Verification Checklist

  • Compilation

No warnings or errors during compilation.

  • Execution

No segmentation faults or runtime crashes.

  • Output

Execution time printed with three decimal precision.

  • Process Monitoring

Use top or htop to verify multiple processes are active.


Troubleshooting

  • Permission Denied

chmod +x matrix_fork
  • Segmentation Fault

Check matrix bounds and process distribution logic.

  • Long Execution Time

Reduce N for initial testing.

  • Compiler Not Found

sudo apt install build-essential

What This Fixes

  • No long text walls
  • Visual consistency with your other CS330 pages
  • Clean hierarchy
  • Clear technical emphasis
  • Screenshots contained properly
  • Code untouched
  • Looks like structured systems documentation, not lab notes