- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
IPC through shared memory
Shared memory is a memory shared between two or more processes. However, why do we need to share memory or some other means of communication?
To reiterate, each process has its own address space, if any process wants to communicate with some information from its own address space to other processes, then it is only possible with IPC (inter process communication) techniques. As we are already aware, communication can be between related or unrelated processes.
Usually, inter-related process communication is performed using Pipes or Named Pipes. Unrelated processes (say one process running in one terminal and another process in another terminal) communication can be performed using Named Pipes or through popular IPC techniques of Shared Memory and Message Queues.
We have seen the IPC techniques of Pipes and Named pipes and now it is time to know the remaining IPC techniques viz., Shared Memory, Message Queues, Semaphores, Signals, and Memory Mapping.
We know that to communicate between two or more processes, we use shared memory but before using the shared memory what needs to be done with the system calls, let us see this −
Create the shared memory segment or use an already created shared memory segment (shmget())
Attach the process to the already created shared memory segment (shmat())
Detach the process from the already attached shared memory segment (shmdt())
Control operations on the shared memory segment (shmctl())
Here we will create two processes. One can write and another can read. Let us see how the reader and writer processes are working using shared memory.
Example Code
#include <iostream> #include <sys/ipc.h> #include <sys/shm.h> #include <stdio.h> using namespace std; main() { key_t my_key = ftok("shmfile",65); // ftok function is used to generate unique key int shmid = shmget(my_key,1024,0666|IPC_CREAT); // shmget returns an ide in shmid char *str = (char*) shmat(shmid,(void*)0,0); // shmat to join to shared memory cout<<"Write Data : "; fgets(str, 50, stdin); printf("Data written in memory: %s\n",str); //detach from shared memory shmdt(str); }
Example Code
#include <iostream> #include <sys/ipc.h> #include <sys/shm.h> #include <stdio.h> using namespace std; main() { key_t my_key = ftok("shmfile",65); // ftok function is used to generate unique key int shmid = shmget(my_key,1024,0666|IPC_CREAT); // shmget returns an ide in shmid char *str = (char*) shmat(shmid,(void*)0,0); // shmat to join to shared memory printf("Data read from memory: %s\n",str); shmdt(str); shmctl(shmid,IPC_RMID,NULL); // destroy the shared memory }
Output
- Related Articles
- Shared Memory Model of Process Communication
- Algorithm for implementing Distributed Shared Memory
- What are Shared Memory MIMD Architectures?
- What is shared memory in the OS?
- Message Passing vs Shared Memory Process communication Models
- What is shared-memory model in computer architecture?
- What are the different shared-memory multiprocessor models?
- What is shared memory architecture in parallel databases?
- Differentiate between shared memory and message passing model in OS.
- What is the shared memory concept by using producer consumer problem?
- Difference between Shared Memory Multiprocessors and Message-Passing Multiprocessors in Computer Architecture.
- IPC using Message Queues
- What is inter process communication (IPC)?
- Shared properties in JavaScript
- How is IPC implemented in the Android, MAC, Windows Operating systems?
