-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShared Memory.cpp
More file actions
88 lines (58 loc) · 2.23 KB
/
Shared Memory.cpp
File metadata and controls
88 lines (58 loc) · 2.23 KB
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/*
Shared Memory Model
Create a shared memory segment in the address space by one process, read bt a different processes (different programs) (then delete)
shmget(): allocates a shared memory segment and returns identifier for said segment.
#include <sys/ipc.h>
#include <sys/shm.h>
int shmget(key_t key, size_t size, int shmflg); //If shmflg is set to 0, IPC_PRIVATE, IPC_CREAT, IPC_EXCL
shmat(): attaches shared memory segment(which has shmid) to the address space of the calling process(which has shmaddr).
void *shmat(int shmid, const void *shmaddr, int shmflg);
If shmadder is null the system will arrange a page aligned address to attach to segment.
shmdt(): detaches shared memory segment(which has shmid) to the address space of the calling process(which has shmaddr).
int shmdt(const void *shmaddr);
shmctl(): performs control operations on segment with shmid, and used to mark shared memory segment for DESTRUCTION.
int shmctl(int shmid, int cmd, struct shmid_ds *buf);
ftok(): convert pathname and project identifier for IPC key (system V)
#include <sys/types.h>
key_t ftok(const char *pathname, int proj_id)
*/
//Writing Process
#include <iostream>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <stdio.h>
int main()
{
//create a unique key
key_k key = ftok("shmfile", 65);
//create identifier for shmid
int shmid = shmget(key1024,0666 | IPC_CREAT);
//attach shared memory
char *str = (char*) shmat(shmid,(void*)0,0);
cout <<"Write Date:";
gets(str);
printf("Data written in memory: %s\n", str);
//Detach shared memory from this writing process
shmdt(str);
return 0;
}
//Reading Process (segment attaches to this)
#include <iostream>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <stdio.h>
int main()
{
//Generate a new key
key_k key = ftok("shmfile", 65);
//returns identifier in shmid
int shmid = shmget(key1024,0666| IPC_CREAT);
//attach shared memory to writing process
char *str = (char*) shmat(shmid,(void*)0,0);
printf("Shared memory reads: %s\n", str);
//Detach the shared memory
shmdt(str);
//destroy the shared memory
shmctl(shmid, IPC_RMID, NULL);
return 0;
}