0% found this document useful (0 votes)
4 views11 pages

Ipc Using Shared Memory in Os PPT With Source Code

This document discusses Inter-Process Communication (IPC) using shared memory, highlighting its efficiency and speed for data exchange between processes. It covers the basic concepts, advantages, implementation steps, and synchronization mechanisms necessary to maintain data integrity. Example source code is provided to illustrate how to create, attach, write to, and read from shared memory segments.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views11 pages

Ipc Using Shared Memory in Os PPT With Source Code

This document discusses Inter-Process Communication (IPC) using shared memory, highlighting its efficiency and speed for data exchange between processes. It covers the basic concepts, advantages, implementation steps, and synchronization mechanisms necessary to maintain data integrity. Example source code is provided to illustrate how to create, attach, write to, and read from shared memory segments.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 11

Ipc Using Shared Memory In Os Ppt

With Source Code

SlideMake.com
Introduction to Inter-Process Communication
(IPC)
• IPC allows processes to communicate and synchronize their
actions within an operating system.

• Shared memory is one of the fastest IPC mechanisms


because processes share a common memory space.

• This presentation explains how shared memory works and


provides example source code for implementation.
What is Shared Memory in OS?

• Shared memory enables multiple processes to access a


common region of memory for data exchange.

• It reduces the overhead of data transfer compared to other


IPC methods like message passing.

• Proper synchronization mechanisms are essential to


prevent data inconsistency in shared memory.
Advantages of Shared Memory IPC

• Fast data exchange due to direct access to memory without


kernel intervention.

• Suitable for large data transfers between processes.

• Facilitates real-time data sharing, especially in high-


performance computing applications.
Basic Concepts of Shared Memory

• A shared memory segment is created by one process and


attached to the address space of other processes.

• Processes must coordinate access to shared memory to


avoid race conditions.

• Synchronization can be achieved through semaphores,


mutexes, or other mechanisms.
Implementation Overview

• The key steps include creating, attaching, and detaching


shared memory segments.

• Processes communicate by reading and writing to the


shared memory.

• Proper cleanup involves removing shared memory


segments after usage.
#include <string.h>

int main() {
key_t key creates
• This code = ftok("shmfile", 65);
a shared memory segment with a unique
intkeyshmid = of
and size shmget(key,
1024 bytes. 1024, 0666|
IPC_CREAT);
•printf("Shared
It uses `ftok()` to memory
generate a key based oncreated
segment a filenamewith
and a
ID:project
%d\n",identifier.
shmid);
return 0;
• The segment is created with read/write permissions for
} owner, group, and others.
```
Example Source Code - Attaching and Writing
to Shared Memory
```c
char
• str;
• str = (char

• ) shmat(shmid, (void

• )0, 0);
• strcpy(str, "Hello Shared Memory");
• printf("Data written to shared memory: %s\n", str);
• shmdt(str);
• ```
Example Source Code - Reading from Shared
Memory
```c
char
• str;
• str = (char

• ) shmat(shmid, (void

• )0, 0);
• printf("Data read from shared memory: %s\n", str);
• shmdt(str);
• shmctl(shmid, IPC_RMID, NULL);
• ```
Synchronization in Shared Memory IPC

• Synchronization mechanisms like semaphores prevent race


conditions during concurrent access.

• Semaphores can be used to control access, ensuring only


one process modifies shared data at a time.

• Proper synchronization is crucial for maintaining data


consistency and integrity.
Summary & Key Takeaways

• Shared memory provides a fast and efficient means of IPC


in operating systems.

• Implementation involves creating, attaching, accessing, and


removing shared memory segments.

• Proper synchronization is essential to prevent data


corruption during concurrent access.

You might also like