The Wayback Machine - https://web.archive.org/web/20240620110948/https://www.geeksforgeeks.org/ipc-shared-memory/
Open In App

IPC through shared memory

Last Updated : 19 Oct, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Inter Process Communication through shared memory is a concept where two or more process can access the common memory and communication is done via this shared memory where changes made by one process can be viewed by another process.

The problem with pipes, fifo and message queue – is that for two process to exchange information. The information has to go through the kernel.

  • Server reads from the input file.
  • The server writes this data in a message using either a pipe, fifo or message queue.
  • The client reads the data from the IPC channel,again requiring the data to be copied from kernel’s IPC buffer to the client’s buffer.
  • Finally, the data is copied from the client’s buffer.

A total of four copies of data are required (2 read and 2 write). So, shared memory provides a way by letting two or more processes share a memory segment. With Shared Memory the data is only copied twice – from input file into shared memory and from shared memory to the output file.

Used System Calls

The system calls that are used in the program are:

            Function           

Signature

                Description
ftok()

key_t ftok()

It is used to generate a unique key.
shmget()

int shmget(key_t key,size_t size, int shmflg);

Upon successful completion, shmget() returns an identifier for the shared memory segment.
shmat()

void *shmat(int shmid ,void *shmaddr ,int shmflg); 

Before you can use a shared memory segment, you have to attach yourself to it using shmat(). Here, shmid is a shared memory ID and shmaddr specifies the specific address to use but we should set it to zero and OS will automatically choose the address.
shmdt()

int shmdt(void *shmaddr);

When you’re done with the shared memory segment, your program should detach itself from it using shmdt().
shmctl()

shmctl(int shmid,IPC_RMID,NULL);

When you detach from shared memory, it is not destroyed. So, to destroy shmctl() is used.

Shared Memory for Writer Process

CPP




#include <iostream>
#include <stdio.h>
#include <sys/ipc.h>
#include <sys/shm.h>
using namespace std;
 
int main()
{
    // ftok to generate unique key
    key_t key = ftok("shmfile", 65);
 
    // shmget returns an identifier in shmid
    int shmid = shmget(key, 1024, 0666 | IPC_CREAT);
 
    // shmat to attach to shared memory
    char* str = (char*)shmat(shmid, (void*)0, 0);
 
    cout << "Write Data : ";
    cin.getline(str, 1024);
 
    cout << "Data written in memory: " << str << endl;
 
    // detach from shared memory
    shmdt(str);
 
    return 0;
}


Shared Memory for Reader Process

CPP




#include <iostream>
#include <stdio.h>
#include <sys/ipc.h>
#include <sys/shm.h>
using namespace std;
 
int main()
{
    // ftok to generate unique key
    key_t key = ftok("shmfile", 65);
 
    // shmget returns an identifier in shmid
    int shmid = shmget(key, 1024, 0666 | IPC_CREAT);
 
    // shmat to attach to shared memory
    char* str = (char*)shmat(shmid, (void*)0, 0);
 
    cout << "Data read from memory:" << str;
 
    // detach from shared memory
    shmdt(str);
 
    // destroy the shared memory
    shmctl(shmid, IPC_RMID, NULL);
 
    return 0;
}


Output



Similar Reads

Difference between Shared Memory Model and Message Passing Model in IPC
Prerequisite - Inter-Process Communication 1. Shared Memory Model: In this IPC model, a shared memory region is established which is used by the processes for data communication. This memory region is present in the address space of the process which creates the shared memory segment. The processes that want to communicate with this process should
2 min read
Algorithm for implementing Distributed Shared Memory
Distributed shared memory(DSM) system is a resource management component of distributed operating system that implements shared memory model in distributed system which have no physically shared memory. The shared memory model provides a virtual address space which is shared by all nodes in a distributed system. The central issues in implementing D
3 min read
What is Distributed shared memory and its advantages
DSM is a mechanism that manages memory across multiple nodes and makes inter-process communications transparent to end-users. The applications will think that they are running on shared memory. DSM is a mechanism of allowing user processes to access shared data without using inter-process communications. In DSM every node has its own memory and pro
3 min read
fork() and memory shared b/w processes created using it
Prerequisite: fork() in C So when we do a fork() what are the sections the two process actually share? Is the heap memory per process? Are the global variables shared? Will malloc return the same address to both? Let us run the program below and take a look at the output of it to clear the questions above. C/C++ Code // C program to demonstrate wor
4 min read
IPC using Message Queues
Prerequisite : Inter Process Communication A message queue is a linked list of messages stored within the kernel and identified by a message queue identifier. A new queue is created or an existing queue opened by msgget(). New messages are added to the end of a queue by msgsnd(). Every message has a positive long integer type field, a non-negative
3 min read
Message based Communication in IPC (inter process communication)
Prerequisites - Cloud computing, Load balancing in Cloud Computing, Inter-process Communication In the development of models and technologies, message abstraction is a necessary aspect that enables distributed computing. Distributed system is defined as a system in which components reside at networked communication and synchronise its functions onl
5 min read
IPC technique PIPES
Pipes are a type of IPC (Inter-Process Communication) technique that allows two or more processes to communicate with each other by creating a unidirectional or bidirectional channel between them. A pipe is a virtual communication channel that allows data to be transferred between processes, either one-way or two-way. Pipes can be implemented using
4 min read
Inter Process Communication (IPC)
A process can be of two types: Independent process.Co-operating process. An independent process is not affected by the execution of other processes while a co-operating process can be affected by other executing processes. Though one can think that those processes, which are running independently, will execute very efficiently, in reality, there ar
14 min read
Difference between Volatile Memory and Non-Volatile Memory
Volatile Memory: It is the memory hardware that fetches/stores data at a high-speed. It is also referred as temporary memory. The data within the volatile memory is stored till the system is capable of, but once the system is turned off the data within the volatile memory is deleted automatically. RAM (Random Access Memory) and Cache Memory are som
3 min read
Random Access Memory (RAM) and Read Only Memory (ROM)
Random Access Memory (RAM) is a type of computer memory that is used to temporarily store data that the computer is currently using or processing. RAM is volatile memory, which means that the data stored in it is lost when the power is turned off. RAM is typically used to store the operating system, application programs, and data that the computer
5 min read