~ /phase-1 /build-run
Phase I · Build & run

matrix_fork.c, line by line.

The Phase I program multiplies two N × N matrices. The parent process divides the rows among PROCS children created with fork(), waits for all of them, then prints the elapsed time.

gcc -Wall#define N 600#define PROCS 4clock() / CLOCKS_PER_SEC

How the program works#

Initialize#

initialize_matrices() fills A and B with random digits 0–9 and sets C to zero.

Split the rows#

The parent computes rows_per_proc = N / PROCS. The last child also takes any remaining rows.

Fork the workers#

The parent calls fork() PROCS times. Each child runs multiply(start, end) on its own range of rows, then calls exit(0).

Wait and time#

The parent calls wait(NULL) once per child, then reads clock() and prints the execution time.

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;
}

Compile and run#

Terminal
$ gcc -Wall matrix_fork.c -o matrix_fork
$ ./matrix_fork
Execution Time: 0.001 seconds

With the defaults (N = 600, PROCS = 4), the program prints its execution time with three decimal places.

Change the parameters#

Both parameters are compile-time constants, so every new configuration means edit, recompile, run.

1 · Open#

nano matrix_fork.c

2 · Edit#

#define N 1800
#define PROCS 4

3 · Rebuild#

gcc -Wall matrix_fork.c -o matrix_fork && ./matrix_fork

In nano, save with Ctrl + O and exit with Ctrl + X.

Verification checklist#

Check What “good” looks like
Compilation No warnings or errors from gcc -Wall
Execution No segmentation faults or crashes
Output Execution time printed with three decimal places
Processes top or htop shows the child processes while it runs

Troubleshooting#

Permission denied#

chmod +x matrix_fork

Compiler not found#

sudo apt install build-essential

Segmentation fault#

Check the matrix bounds and the logic that divides rows among processes.

Very long runs#

Lower N for a quick first test, then scale it back up for the real measurements.