-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshmem_client.cpp
More file actions
75 lines (73 loc) · 1.33 KB
/
shmem_client.cpp
File metadata and controls
75 lines (73 loc) · 1.33 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
#include <unistd.h>
#include <sys/shm.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/un.h>
#include <errno.h>
#define SHMEM_PATH "/my_shmem"
#define SHMEM_SIZE (1 << 16) // 64 KiB
int main()
{
int res, shmem_fd;
void *ptr;
shmem_fd = shm_open(SHMEM_PATH, O_RDWR | O_CREAT, 0666);
if (shmem_fd < 0)
{
perror("shm_open\n");
exit(1);
}
res = ftruncate(shmem_fd, SHMEM_SIZE);
if (res)
{
perror("ftruncate\n");
exit(1);
}
ptr = mmap(NULL, SHMEM_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, shmem_fd, 0);
if (!ptr)
{
perror("mmap\n");
exit(1);
}
int sock;
struct sockaddr_un addr;
sock = socket(AF_UNIX, SOCK_STREAM, 0);
if (socket < 0)
{
perror("socket\n");
exit(1);
}
addr.sun_family = AF_UNIX;
strcpy(addr.sun_path, SHMEM_PATH);
if (connect(sock, (struct sockaddr *)&addr, sizeof(addr)) < 0)
{
perror("connect\n");
exit(1);
}
char buf[256] = {0};
scanf("%s", buf);
send(sock, buf, sizeof(buf), 0);
close(sock);
res = close(shmem_fd);
if (res)
{
perror("close\n");
exit(1);
}
res = shm_unlink(SHMEM_PATH);
if (res)
{
perror("shm_unlink\n");
exit(1);
}
return 0;
}