Here is my code so far:
master.c:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <semaphore.h>
#include <errno.h>

typedef struct {
    int index;
    // Add any other variables you need in the shared memory structure
} shared_data;

int main(int argc, char* argv[]) {
    const char shmName = argv[1]; / shared memory name from command line */
    const char semName = argv[2]; / semaphore name from command line */
    const int SIZE = sizeof(shared_data);

    printf(“Master begins execution\n”);

    int shm_fd;
    shared_data *shm_ptr;

    // Open the shared memory segment
    shm_fd = shm_open(shmName, O_CREAT | O_RDWR, 0666);
    if (shm_fd == -1) {
        printf(“Master: Shared memory failed: %s\n”, strerror(errno));
        exit(1);
    }

    // Configure size of shared memory segment
    ftruncate(shm_fd, SIZE);

    // Map shared memory segment in the address space of the process
    shm_ptr = (shared_data *)mmap(0, SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, shm_fd, 0);
    if (shm_ptr == MAP_FAILED) {
        printf(“Master: Map failed: %s\n”, strerror(errno));
        exit(1);
    }

    // Initialize index in the shared memory structure to zero
    shm_ptr->index = 0;
    printf(“Master initialized index in the shared structure to zero\n”);

    // Create a named semaphore for mutual exclusion
    sem_t mutex_sem = sem_open(semName, O_CREAT, 0660, 1);
    if (mutex_sem == SEM_FAILED) {
        printf(“Master: sem_open failed: %s\n”, strerror(errno));
        exit(1);
    }
    printf(“Master created a semaphore named %s\n”, semName);

    // Create n child processes to execute slave
    int n = atoi(argv[3]);
    printf(“Master created %d child processes to execute slave\n”, n);

    // Wait for all child processes to terminate
    printf(“Master waits for all child processes to terminate\n”);
    int i;
    for (i = 0; i < n; i++) {
        wait(NULL);
    }

    printf(“Master received termination signals from all %d child processes\n”, n);
    printf(“Updated content of shared memory segment after access by child processes:\n”);
    printf(“— content of shared memory —\n”);
    printf(“[ Current content of shared memory ]\n”);

    // Remove the semaphore
    if (sem_close(mutex_sem) == -1) {
        printf(“Master: sem_close failed: %s\n”, strerror(errno));
        exit(1);
    }
    printf(“Master removed the semaphore\n”);

    // Close access to shared memory, remove shared memory segment, and exit
    if (sem_unlink(semName) == -1) {
        printf(“Master: sem_unlink failed: %s\n”, strerror(errno));
        exit(1);
    }

    if (munmap(shm_ptr, SIZE) == -1) {
        printf(“Master: Unmap failed: %s\n”, strerror(errno));
        exit(1);
    }

    if (close(shm_fd) == -1) {
        printf(“Master: Close failed: %s\n”, strerror(errno));
        exit(1);
    }

    if (shm_unlink(shmName) == -1) {
        printf(“Master: Error removing %s: %s\n”, shmName, strerror(errno));
        exit(1);
    }

    printf(“Master closed access to shared memory, removed shared memory segment, and is exiting\n”);

    return 0;
}


slave.c:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <semaphore.h>
#include <errno.h>

typedef struct {
    int index;
    // Add any other variables you need in the shared memory structure
} shared_data;

int main(int argc, char argv[]) {
    printf(“Slave begins execution\n”);

    int child_number = atoi(argv[1]);
    const char *shmName = argv[2];
    const char *semName = argv[3];}