-
Notifications
You must be signed in to change notification settings - Fork 0
/
17_Shared_Memory_Data_Transfer_P2.c
49 lines (40 loc) · 1.44 KB
/
17_Shared_Memory_Data_Transfer_P2.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
// Q. Transfer message from program1 to read & display by program2 using Shared Memory
// program1 --> 17_Shared_Memory_Data_Transfer_P1.c (Sender)
// program2 --> 17_Shared_Memory_Data_Transfer_P2.c (Receiver)
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/shm.h>
#include <string.h>
int main()
{
void *sharedMemory;
int shmid;
char data[50];
// Create shared memory segment with key 1234, having size 1024 bytes.
// 0777 are the permissions on the shared segment
shmid = shmget((key_t)1234, 1024, 0777);
printf("P2: Key of shared memory is %d.\n", shmid);
// Process attached to shared memory segment
sharedMemory = shmat(shmid, NULL, 0);
// Print the address where the segment is attached with this process
printf("P2: Process attached at %p.\n", sharedMemory);
// Output
printf("P2: Data read from shared memory - %s\n", (char *)sharedMemory);
}
/*
Output:
s4shibam@SHIBAM:~/OS$ gcc 17_Shared_Memory_Data_Transfer_P1.c
s4shibam@SHIBAM:~/OS$ ./a.out
P1: Key of shared memory is 0.
P1: Process attached at 0x7f4078c88000.
P1: Enter the data to pass :
I am Shibam.
P1: Entered data - I am Shibam.
s4shibam@SHIBAM:~/OS$ gcc 17_Shared_Memory_Data_Transfer_P2.c
s4shibam@SHIBAM:~/OS$ ./a.out
P2: Key of shared memory is 0.
P2: Process attached at 0x7f33ba8f5000.
P2: Data read from shared memory - I am Shibam.
s4shibam@SHIBAM:~/OS$
*/