-
Notifications
You must be signed in to change notification settings - Fork 0
/
sharedMemory.c
69 lines (49 loc) · 1.74 KB
/
sharedMemory.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include <sys/wait.h>
#include <sys/mman.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>
#include <errno.h>
#include <unistd.h>
#include "macros.h"
#define MEM_SIZE 4096
// fork(), shm_open(), mmap()
int main (int argc, char *ARGV[]) {
// Param check
if(argc != 2) {
fprintf(stderr, "Usage: %s <shm_name>\n", ARGV[0]);
exit(EXIT_FAILURE);
}
int retvalue, mem_fd;
// Create shared memory segment
SYSC(mem_fd, shm_open(ARGV[1], O_CREAT | O_RDWR, 0666), "shm_open error");
SYSC(retvalue, ftruncate(mem_fd, MEM_SIZE), "ftruncate error");
// Create Child Process
pid_t pid;
SYSC(pid, fork(), "fork error");
if (pid == 0) {
// Child Process
// Shared memory mapping
void *ptr;
SYSCN(ptr, mmap(NULL, MEM_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, mem_fd, 0), "mmap error");
// Write on shared memory segment
const char *msg = "Hello, shared memory!\n";
SYSC(retvalue, sprintf(ptr, "%s", msg), "sprintf error");
// Shared memory unmapping
SYSC(retvalue, munmap(ptr, MEM_SIZE), "munmap error");
exit(EXIT_SUCCESS);
} else {
// Father Process
// Shared memory mapping
void *ptr;
SYSCN(ptr, mmap(NULL, MEM_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, mem_fd, 0), "mmap error");
// Wait Child process
SYSC(retvalue, waitpid(pid, NULL, 0), "waitpid error");
// Write read data on stdout
SYSC(retvalue, write(STDOUT_FILENO, ptr, strlen(ptr)), "write error");
// Shared memory unmapping
SYSC(retvalue, munmap(ptr, MEM_SIZE), "munmap error");
exit(EXIT_SUCCESS);
}
}