Shared Memory in Unix Using C Programming

🎯 Introduction

Shared memory is a powerful interprocess communication mechanism in Unix-based operating systems that allows multiple processes to access the same region of memory. This shared memory region can be used to share data among processes efficiently and is particularly useful when processes need to collaborate and exchange information without the overhead of inter-process communication (IPC).

In this blog post, we will explore the concept of shared memory in Unix using C programming. We'll cover the basics of shared memory, create a C program that demonstrates its usage, and provide a detailed explanation of the code and its output. We will also highlight some key points and potential use cases for shared memory.

🎯 Concept

Shared memory provides a common memory area that is mapped into the address space of multiple processes. This means that each participating process can read from and write to the shared memory segment as if it were its own memory. The critical aspect of shared memory is proper synchronization, as multiple processes accessing the same memory region simultaneously can lead to data corruption and other issues.

To ensure proper synchronization, Unix provides the concept of semaphores. Semaphores are simple integer variables used as a signaling mechanism to control access to shared resources. Processes can increment, decrement, and test the value of semaphores, allowing them to request and release access to shared memory segments safely.

🎯 Shared memory Program

#include <stdio.h>

#include <sys/ipc.h>

#include <sys/shm.h>

#include <sys/sem.h>

#include <unistd.h>


int main() {

    int id, semid, count = 0, i = 1, j;

    int *ptr;

    

    // Create a shared memory segment

    id = shmget(8078, sizeof(int), IPC_CREAT | 0666);

    ptr = (int *)shmat(id, NULL, 0);

    

    // Create a semaphore

    union semun {

        int val;

        struct semid_ds *buf;

        ushort *array;

    } u;

    

    struct sembuf sem;

    semid = semget(1011, 1, IPC_CREAT | 0666);

    ushort a[1] = {1};

    u.array = a;

    semctl(semid, 0, SETALL, u);

    

    while (1) {

        sem.sem_num = 0;

        sem.sem_op = -1;

        sem.sem_flg = 0;

        semop(semid, &sem, 1);

        

        *ptr = *ptr + 1;

        printf("Process ID: %d Count is: %d \n", getpid(), *ptr);

        

        for (j = 1; j <= 1000000; j++) {

            sem.sem_num = 0;

            sem.sem_op = +1;

            sem.sem_flg = 0;

            semop(semid, &sem, 1);

        }

    }


    // Detach shared memory

    shmdt(ptr);

    return 0;

}

🎯 Shared memory Program Explanation

🎯 Sample Output

The output of running the program will be a continuous stream of process IDs and the count value, as each process continuously increments the shared count. The exact values may vary each time the program is executed.


Process ID: 4568 Count is: 1012310

Process ID: 4568 Count is: 1012311

Process ID: 4568 Count is: 1012312

Process ID: 4568 Count is: 1012313

Process ID: 4568 Count is: 1012315

Process ID: 4568 Count is: 1012317

Process ID: 4568 Count is: 1012319

Process ID: 4568 Count is: 1012321

Process ID: 4607 Count is: 1012322

Process ID: 4607 Count is: 1012324

Process ID: 4607 Count is: 1012326

Process ID: 4607 Count is: 1012328

Process ID: 4607 Count is: 1012330

Process ID: 4607 Count is: 1012332

Process ID: 4607 Count is: 1012334

...

🎯 Summary

This blog post explored the concept of shared memory in Unix using C programming. Shared memory allows multiple processes to access the same memory region, enabling efficient inter-process communication. To ensure proper synchronization, we used semaphores to control access to the shared memory segment. The C program demonstrated how to create a shared memory segment, initialize a semaphore, and implement synchronization using semaphores.

🎯 Key Points