Program to demonstrate Shared Memory IPC

Share

For IPC in the operating system, we have some techniques like shared memory and message passing concepts. For inter-process communication, we create a shared memory segment using <sys/shm.h> GCC library. The function looks like this:

shmget((key_t)2345, 1024, 0666 | IPC_CREAT);

it creates a shared memory segment with key 2345, having a size of 1024 bytes. IPC_CREAT is used to create the shared segment if it does not exist. 0666 are the permissions on the shared segment. After this, we attach a shared memory to the process. The function looks like this:

shmat(shmid, NULL, 0);

The parameter shmid is a shared memory reference id followed by a shared memory address and a flag.

To demonstrate shared memory IPC we create two different processes and use a shared memory segment for communication

Process 1 (Write data into memory):
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/shm.h>
#include <string.h>

void write_shared();
int main()
{
    int i;
    void *shared_memory;
    int shmid;
    // creates shared memory segment with key 2345, having size 1024 bytes.
    // IPC_CREAT is used to create the shared segment if it does not exist.
    // 0666 are the permisions on the shared segment
    shmid = shmget((key_t)2345, 1024, 0666 | IPC_CREAT);
    printf("+ Key of shared memory is %d\n", shmid);
    // process attached to shared memory segment
    shared_memory = shmat(shmid, NULL, 0);
    // this prints the address where the segment is attached with this process
    printf("+ Process attached at %p\n", shared_memory);
    printf("** Writting buffer into shared memory \n");
    write_shared(shared_memory);
}

void write_shared(void *shared_memory)
{
    char buff[100];
    printf("++ Enter some data to write to shared memory:");
    scanf("%s",&buff);
    strcpy(shared_memory, buff); // data written to shared memory
    printf("++ You wrote : %s\n", (char *)shared_memory);
}
2. Process 2 ( Read from shared memory)
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/shm.h>
#include <string.h>


void read_shared();
int main()
{
    int i;
    void *shared_memory;
    int shmid;
    // creates shared memory segment with key 2345, having size 1024 bytes.
    // IPC_CREAT is used to create the shared segment if it does not exist.
    // 0666 are the permisions on the shared segment
    shmid = shmget((key_t)2345, 1024, 0666 | IPC_CREAT);
    printf("+ Key of shared memory is %d\n", shmid);
    // process attached to shared memory segment
    shared_memory = shmat(shmid, NULL, 0);
    // this prints the address where the segment is attached with this process
    printf("+ Process attached at %p\n", shared_memory);
    
    printf("** Reading buffer from shared memory \n");
    read_shared(shared_memory);
}

void read_shared(void *shared_memory){
    printf("- Process attached at %p\n",shared_memory);
    printf("- Data read from shared memory is : %s\n",(char *)shared_memory);
}

Now we run both processes simultaneously ( write process and then read process )

The output should look like this:

IPC

Source code repository: https://github.com/CodewithSudeep/operating-system-c-program

Other C Programs: https://codewithsudeep.com/category/c-program/

Share
Sudeep Mishra

Sudeep Mishra

Healing

%d bloggers like this: