-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsharedmem1.c
55 lines (45 loc) · 1.65 KB
/
sharedmem1.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
50
51
52
53
54
55
//producer consumer using concurrent processes
// gcc <program.c> -lrt
// remember to put -lrt at the end
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <sys/mman.h>
#include <sys/fcntl.h>
#include <assert.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/wait.h>
// shared memory
struct permission {
int user, group, other;
} *perm;
int main() {
int shmfd = shm_open ("MyFile_SHM", O_CREAT | O_EXCL | O_RDWR, S_IRUSR | S_IWUSR);
assert (shmfd != -1);
/* Resize the region to store 1 struct instance */
assert (ftruncate (shmfd, sizeof (struct permission)) != -1);
/* Map the object into memory so file operations aren't needed */
struct permission *perm = mmap (NULL, sizeof (struct permission),
PROT_READ | PROT_WRITE, MAP_SHARED, shmfd, 0); // addr==NULL allows the kernel to choose the addr for the mapping
assert (perm != MAP_FAILED);
/* Create a child process and write to the mapped/shared region */
pid_t child_pid = fork();
if (child_pid == 0) { // child process
perm->user = 6;
perm->group = 4;
perm->other = 0;
/* Unmap and close the child's shared memory access */
munmap (perm, sizeof (struct permission));
close (shmfd);
return 0;
}
/* Make the parent wait until the child has exited */
wait (NULL);
/* Read from the mapped/shared memory region */
printf ("Permission bit-mask: 0%d%d%d\n", perm->user, perm->group, perm->other);
/* Unmap, close, and delete the shared memory object */
munmap (perm, sizeof (struct permission));
close (shmfd);
shm_unlink ("MyFile_SHM");
}