Skip to content

Commit c1cd265

Browse files
committed
geeks-scenario reader writer tested and readme updated to the usefull links.
1 parent b922fc5 commit c1cd265

File tree

5 files changed

+345
-0
lines changed

5 files changed

+345
-0
lines changed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# Shared-Memory-IPC
22

3+
* https://stackoverflow.com/questions/14509662/performance-difference-between-ipc-shared-memory-and-threads-memory
4+
35
## Example One
46
1. https://stackoverflow.com/questions/5656530how-to-use-shared-memory-with-linux-in-c (inside Solutions @shakram02)
57

geeks_shm/ff.txt

+1
Large diffs are not rendered by default.

geeks_shm/reader.c

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// #include
2+
#define _GNU_SOURCE
3+
#include <stdio.h>
4+
#include <sys/ipc.h>
5+
#include <sys/shm.h>
6+
7+
8+
int main()
9+
{
10+
// ftok to generate unique key
11+
key_t key = ftok("shmfile",65);
12+
13+
// shmget returns an identifier in shmid
14+
int shmid = shmget(key,1024,0666|IPC_CREAT);
15+
16+
// shmat to attach to shared memory
17+
char *str = (char*) shmat(shmid,(void*)0,0);
18+
19+
printf("Data read from memory: %s\n",str);
20+
21+
//detach from shared memory
22+
shmdt(str);
23+
24+
// destroy the shared memory
25+
shmctl(shmid,IPC_RMID,NULL);
26+
27+
return 0;
28+
}

geeks_shm/splicer-benchmarking.h

+285
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,285 @@
1+
// 8888888b. 8888888888 8888888888 .d8888b. 8888888 888b 888 .d8888b. 888 888 888 8888888b. 8888888888
2+
// 888 "Y88b 888 888 d88P "88b 888 8888b 888 d88P Y88b 888 888 888 888 "Y88b 888
3+
// 888 888 888 888 Y88b. d88P 888 88888b 888 888 888 888 888 888 888 888 888
4+
// 888 888 8888888 8888888 "Y8888P" 888 888Y88b 888 888 888 888 888 888 888 8888888
5+
// 888 888 888 888 .d88P88K.d88P 888 888 Y88b888 888 888 888 888 888 888 888
6+
// 888 888 888 888 888" Y888P" 888 888 Y88888 888 888 888 888 888 888 888 888
7+
// 888 .d88P 888 888 Y88b .d8888b 888 888 Y8888 Y88b d88P 888 Y88b. .d88P 888 .d88P 888
8+
// 8888888P" 8888888888 888 "Y8888P" Y88b 8888888 888 Y888 "Y8888P" 88888888 "Y88888P" 8888888P" 8888888888
9+
10+
#ifndef SPLICE_H
11+
#define SPLICE_H
12+
#endif
13+
14+
15+
#include <errno.h>
16+
#include <sys/uio.h>
17+
#include <sys/stat.h>
18+
#include <linux/unistd.h>
19+
#include <unistd.h>
20+
#include <stdio.h>
21+
#include <stdlib.h>
22+
#include <time.h> // time()
23+
24+
25+
26+
#define SPLICE_SIZE 1024
27+
#define K_MULTIPLY 40000
28+
29+
30+
#if defined(__i386__)
31+
#define __NR_sys_splice 313
32+
#define __NR_sys_tee 315
33+
#define __NR_sys_vmsplice 316
34+
#elif defined(__x86_64__)
35+
#define __NR_sys_splice 275
36+
#define __NR_sys_tee 276
37+
#define __NR_sys_vmsplice 278
38+
#define __NR_sys_process_vm_readv 310
39+
#define __NR_sys_process_vm_writev 311
40+
#elif defined(__powerpc__) || defined(__powerpc64__)
41+
#define __NR_sys_splice 283
42+
#define __NR_sys_tee 284
43+
#define __NR_sys_vmsplice 285
44+
#elif defined(__ia64__)
45+
#define __NR_sys_splice 1297
46+
#define __NR_sys_tee 1301
47+
#define __NR_sys_vmsplice 1302
48+
#define __NR_sys_process_vm_readv 1332
49+
#define __NR_sys_process_vm_readv 1333
50+
#elif defined(__arm__)
51+
#define __NR_sys_splice 340
52+
#define __NR_sys_tee 342
53+
#define __NR_sys_vmsplice 343
54+
#else
55+
#error unsupported arch
56+
#endif
57+
58+
#ifndef SPLICE_F_MOVE
59+
60+
#define SPLICE_F_MOVE (0x01) /* move pages instead of copying */
61+
#define SPLICE_F_NONBLOCK (0x02) /* don't block on the pipe splicing (but */
62+
/* we may still block on the fd we splice */
63+
/* from/to, of course */
64+
#define SPLICE_F_MORE (0x04) /* expect more data */
65+
#define SPLICE_F_GIFT (0x08) /* pages passed in are a gift */
66+
67+
#endif /* SPLICE_F_MOVE defined */
68+
69+
/*
70+
* SPLICE_F_UNMAP was introduced later, so check for that seperately
71+
*/
72+
#ifndef SPLICE_F_UNMAP
73+
#define SPLICE_F_UNMAP (0x10) /* undo vmsplice map */
74+
#endif
75+
76+
static int clock_counter;
77+
clock_t start, end;
78+
79+
80+
// .d8888b. .d88888b. 888b d888 888b d888 .d88888b. 888b 888 .d8888b.
81+
// d88P Y88b d88P" "Y88b 8888b d8888 8888b d8888 d88P" "Y88b 8888b 888 d88P Y88b
82+
// 888 888 888 888 88888b.d88888 88888b.d88888 888 888 88888b 888 Y88b.
83+
// 888 888 888 888Y88888P888 888Y88888P888 888 888 888Y88b 888 "Y888b.
84+
// 888 888 888 888 Y888P 888 888 Y888P 888 888 888 888 Y88b888 "Y88b.
85+
// 888 888 888 888 888 Y8P 888 888 Y8P 888 888 888 888 Y88888 "888
86+
// Y88b d88P Y88b. .d88P 888 " 888 888 " 888 Y88b. .d88P 888 Y8888 Y88b d88P
87+
// "Y8888P" "Y88888P" 888 888 888 888 "Y88888P" 888 Y888 "Y8888P"
88+
89+
90+
91+
static inline int error(const char *n)
92+
{
93+
perror(n);
94+
return -1;
95+
}
96+
97+
static int __check_pipe(int pfd)
98+
{
99+
struct stat sb;
100+
101+
if (fstat(pfd, &sb) < 0)
102+
return error("stat");
103+
if (!S_ISFIFO(sb.st_mode))
104+
return 1;
105+
106+
return 0;
107+
}
108+
109+
static inline int check_input_pipe(void)
110+
{
111+
if (!__check_pipe(STDIN_FILENO))
112+
return 0;
113+
114+
fprintf(stderr, "stdin must be a pipe\n");
115+
return 1;
116+
}
117+
118+
static inline int check_output_pipe(void)
119+
{
120+
if (!__check_pipe(STDOUT_FILENO))
121+
return 0;
122+
123+
fprintf(stderr, "stdout must be a pipe\n");
124+
return 1;
125+
}
126+
127+
static inline void test_string_askii()
128+
{
129+
int a = 'a';
130+
int A = 'A';
131+
int z = 'z';
132+
int Z = 'Z';
133+
printf("a = %d, A = %d, z = %d, Z = %d\n", a,A,z,Z);
134+
return;
135+
}
136+
137+
static inline double time_calc(clock_t en, clock_t st, char* str){
138+
double cpu_time_used = ((double) (en - st)) / CLOCKS_PER_SEC;
139+
printf("in %s, time_calc: %f\n", str, cpu_time_used);
140+
return cpu_time_used;
141+
}
142+
143+
// static inline void clock_init(){
144+
// start = (clock_t*) malloc(1);
145+
// end = (clock_t*) malloc(1);
146+
// clock_counter = (int*) malloc(1);
147+
// }
148+
149+
static inline clock_t clocker(int cnt, char* str)
150+
{
151+
if(cnt == 0){
152+
start = clock();
153+
printf("in %s: started: %f\n", str, (double)start);
154+
return start;
155+
}
156+
else{
157+
end = clock();
158+
printf("in %s:finished: %f\n", str, (double)end);
159+
return end;
160+
}
161+
}
162+
163+
static inline void size_printer(char* str){
164+
printf("int %s, size of data is: %d*%d\n", str, K_MULTIPLY, SPLICE_SIZE);
165+
printf("--------------------------------------------------------------------\n");
166+
}
167+
168+
/**
169+
* TO DO...
170+
*/
171+
static inline void file(){
172+
FILE *fptr;
173+
int num;
174+
175+
fptr = fopen("./result.txt","w");
176+
177+
if(fptr == NULL)
178+
{
179+
printf("Error!");
180+
exit(1);
181+
}
182+
183+
printf("Enter num: ");
184+
scanf("%d",&num);
185+
186+
fseek(fptr, 0, SEEK_END);
187+
fprintf(fptr,"--------------------------------------\n %d\n",num);
188+
189+
fclose(fptr);
190+
}
191+
192+
193+
194+
// 8888888b. d8888 88888888888 d8888 8888888888 888 888 888b 888 .d8888b. .d8888b.
195+
// 888 "Y88b d88888 888 d88888 888 888 888 8888b 888 d88P Y88b d88P Y88b
196+
// 888 888 d88P888 888 d88P888 888 888 888 88888b 888 888 888 Y88b.
197+
// 888 888 d88P 888 888 d88P 888 8888888 888 888 888Y88b 888 888 "Y888b.
198+
// 888 888 d88P 888 888 d88P 888 888 888 888 888 Y88b888 888 "Y88b.
199+
// 888 888 d88P 888 888 d88P 888 888 888 888 888 Y88888 888 888 "888
200+
// 888 .d88P d8888888888 888 d8888888888 888 Y88b. .d88P 888 Y8888 Y88b d88P Y88b d88P
201+
// 8888888P" d88P 888 888 d88P 888 888 "Y88888P" 888 Y888 "Y8888P" "Y8888P"
202+
203+
/**
204+
* Benchmarking Data
205+
*/
206+
void random_char_selector(char* ch)
207+
{
208+
time_t t;
209+
srand((unsigned) time(&t));
210+
int chars [58]= {};
211+
212+
for (int i = 0; i < 58; i++){
213+
chars[i] = (rand() % 58) + 65;
214+
ch[i] = chars[i];
215+
// printf("[%d, %c], ", chars[i], ch[i]);
216+
}
217+
return;
218+
}
219+
220+
void k_generator(char* chars){
221+
char ch[58] = {};
222+
random_char_selector(ch);
223+
224+
for(int i = 0; i < 1024; i++){
225+
chars[i] = ch[(rand() % 58)];
226+
}
227+
return;
228+
}
229+
230+
char** empty_allocator(){
231+
char** buf = malloc(sizeof(char*) * K_MULTIPLY);
232+
for(int i = 0; i < K_MULTIPLY ;i++)
233+
buf[i] = malloc(sizeof(char) * SPLICE_SIZE);
234+
return buf;
235+
}
236+
237+
void free_allocator(char** mem){
238+
for(int i = 0; i < K_MULTIPLY; i++){
239+
mem[i];
240+
}
241+
free(mem);
242+
return;
243+
}
244+
245+
void fake_data_generator(char** container){
246+
char ch[1024] = {};
247+
for(int i = 0; i < K_MULTIPLY; i++){
248+
k_generator(ch);
249+
container[i] = ch;
250+
// printf("container[%d]: %s\n", i, container[i]);
251+
}
252+
}
253+
254+
char** initializer(){
255+
char** buf = empty_allocator();
256+
fake_data_generator(buf);
257+
return buf;
258+
}
259+
260+
static int usage(char *name)
261+
{
262+
fprintf(stderr, "%s: [-u(unalign)] [-g(gift)]| ...\n", name);
263+
return 1;
264+
}
265+
266+
267+
268+
269+
// .d8888b. Y88b d88P .d8888b. .d8888b. d8888 888 888 .d8888b.
270+
// d88P Y88b Y88b d88P d88P Y88b d88P Y88b d88888 888 888 d88P Y88b
271+
// Y88b. Y88o88P Y88b. 888 888 d88P888 888 888 Y88b.
272+
// "Y888b. Y888P "Y888b. 888 d88P 888 888 888 "Y888b.
273+
// "Y88b. 888 "Y88b. 888 d88P 888 888 888 "Y88b.
274+
// "888 888 "888 888 888 d88P 888 888 888 "888
275+
// Y88b d88P 888 Y88b d88P Y88b d88P d8888888888 888 888 Y88b d88P
276+
// "Y8888P" 888 "Y8888P" "Y8888P" d88P 888 88888888 88888888 "Y8888P"
277+
278+
/**
279+
* VMSPLICE
280+
*/
281+
static inline int svmsplice(int fd, const struct iovec *iov,
282+
unsigned long nr_segs, unsigned int flags)
283+
{
284+
return syscall(__NR_sys_vmsplice, fd, iov, nr_segs, flags);
285+
}

geeks_shm/writer.c

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#define _GNU_SOURCE
2+
#include <stdio.h>
3+
#include <sys/ipc.h>
4+
#include <sys/shm.h>
5+
#include "splicer-benchmarking.h"
6+
7+
8+
int main()
9+
{
10+
// ftok to generate unique key
11+
key_t key = ftok("shmfile",65);
12+
// printf("12");
13+
char** dataset = initializer();
14+
// shmget returns an identifier in shmid
15+
int shmid = shmget(key,1024*40000,0666|IPC_CREAT);
16+
// shmat to attach to shared memory
17+
char *str = (char*) shmat(shmid,(void*)0,0);
18+
19+
for (int j = 0;j<40000;j++)
20+
for (int i = 0,k=j*1024; i<1024;i++,k++)
21+
str[k] = dataset[j][i];
22+
23+
// printf("Data written in memory: NULL and pointer is %s\n", str);
24+
25+
//detach from shared memory
26+
shmdt(str);
27+
28+
return 0;
29+
}

0 commit comments

Comments
 (0)