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
| #include <stdio.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#define SHMSZ 27 //Size of shared memory
int main()
{
char c;
int shmid;
int shmflg = IPC_CREAT | 0666; // flags like create, access permission
key_t key = 5678; // Name of Shared Memory Segment
char *shm, *s;
if ((shmid = shmget(key, SHMSZ, shmflg)) < 0) { //Create the segment
perror("shmget");
return 1;
}
if ((shm = shmat(shmid, NULL, 0)) == (void *) -1) { //Attach segment to data space
perror("shmat");
return 1;
}
s = shm; // Fill memory to read by other process
for (c = 'a'; c <= 'z'; c++)
*s++ = c;
*s = NULL;
while (*shm != '*') // Wait for other process acknowledgement
sleep(1);
return 0;
}
|