Ipc Using Shared Memory in Os PPT With Source Code
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.
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