Inter-Process Communication Using Shared Memory
🟦 Inter-Process Communication Using Shared Memory
1️⃣ What is Shared Memory?
Shared memory is an IPC mechanism where multiple processes access the same physical memory region.
-
Fastest IPC mechanism
-
No kernel involvement after setup
-
Requires explicit synchronization (semaphores/mutex)
2️⃣ Why Shared Memory?
✔ Very high performance
✔ Suitable for large data sharing
❌ Risk of race condition without synchronization
3️⃣ System Calls Used (System V Shared Memory)
| Function | Purpose |
|---|---|
ftok() | Generate IPC key |
shmget() | Create shared memory |
shmat() | Attach shared memory |
shmdt() | Detach shared memory |
shmctl() | Control / delete memory |
4️⃣ Communication Model (Simple Example)
-
Parent process writes data
-
Child process reads data
-
Shared memory is created before fork()
5️⃣ Small Example: Parent Writes, Child Reads
🔹 Program Explanation
-
Create shared memory
-
Fork child
-
Parent writes message
-
Child reads message
-
Shared memory deleted
6️⃣ C Program: Shared Memory IPC
7️⃣ Sample Output
8️⃣ Important Notes
-
Shared memory is not synchronized by default
-
Race conditions may occur
-
Use semaphores for synchronization
-
Must remove shared memory using
shmctl()
Explanation of Functions Used in Shared Memory IPC Program
The program uses System V shared memory and a few process control system calls.
1️⃣ ftok() — Generate IPC Key
Prototype
Purpose
Generates a unique key used to identify the shared memory segment.
Arguments
| Argument | Description |
|---|---|
pathname | Existing file name |
proj_id | Project identifier (character or number) |
Example
📌 Note:
Both processes must use the same key to access shared memory.
2️⃣ shmget() — Create or Access Shared Memory
Prototype
Purpose
Creates a shared memory segment or accesses an existing one.
Arguments
| Argument | Description |
|---|---|
key | IPC key |
size | Size of shared memory in bytes |
shmflg | Permission and flags |
Example
Flags Explained
-
0666→ Read/write permissions -
IPC_CREAT→ Create segment if not present
3️⃣ shmat() — Attach Shared Memory
Prototype
Purpose
Attaches shared memory to the process’s address space.
Arguments
| Argument | Description |
|---|---|
shmid | Shared memory ID |
shmaddr | Address to attach (NULL = system chooses) |
shmflg | Access mode (0 = read/write) |
Example
📌 Return Value
-
Pointer to shared memory
-
(void *) -1on failure
4️⃣ fork() — Create Child Process
Prototype
Purpose
Creates a child process that shares the same memory segment.
Return Values
| Return | Meaning |
|---|---|
0 | Child process |
>0 | Parent process |
<0 | Error |
Example
5️⃣ strcpy() — Write to Shared Memory
Prototype
Purpose
Copies message into shared memory.
Example
📌 Important:
Shared memory behaves like normal memory.
6️⃣ sleep() — Delay Execution
Prototype
Purpose
Ensures parent writes first before child reads.
Example
7️⃣ shmdt() — Detach Shared Memory
Prototype
Purpose
Detaches shared memory from process address space.
Arguments
| Argument | Description |
|---|---|
shmaddr | Address returned by shmat() |
Example
8️⃣ shmctl() — Control / Delete Shared Memory
Prototype
Purpose
Controls shared memory segment.
Arguments
| Argument | Description |
|---|---|
shmid | Shared memory ID |
cmd | Control command |
buf | Optional structure |
Example
Commands
| Command | Meaning |
|---|---|
IPC_RMID | Remove shared memory |
IPC_STAT | Get status |
9️⃣ wait() — Wait for Child Process
Prototype
Purpose
Ensures parent waits for child before deleting memory.
Example
🔹 Summary Table (Quick Revision)
| Function | Purpose |
|---|---|
ftok() | Generate key |
shmget() | Create shared memory |
shmat() | Attach memory |
fork() | Create child |
strcpy() | Write data |
shmdt() | Detach memory |
shmctl() | Delete memory |
wait() | Synchronize parent |
Comments
Post a Comment