summaryrefslogtreecommitdiffstats
path: root/examples/shm/shm.c
blob: 24691152a090341ccbd3394b869e7ace88815680 (plain)
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#include <unistd.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/wait.h>
#include <sys/time.h>
#include <semaphore.h>
#include <stdio.h>
#include <aes.h>

sem_t *enc_sem, *get_sem;

#define SHM_SIZE 64*1024

void child(pid_t parent, void* mem)
{
	char key[16];
	char iv[16];
	struct crypto_aes_ctx ctx;
	uint32_t mem_size;

	for (;;) {
		memset(key, 0xa3, sizeof(key));
		memset(iv, 0x3, sizeof(iv));

		sem_wait(enc_sem);
		
		crypto_aes_expand_key(&ctx, (void*)key, sizeof(key));
		
		memcpy(&mem_size, mem, sizeof(mem_size));

		crypto_cbc_encrypt(&ctx, mem, mem_size, mem, iv);

		sem_post(get_sem);
	}
}

static int must_finish = 0;

static void alarm_handler(int signo)
{
        must_finish = 1;
}

static double udifftimeval(struct timeval start, struct timeval end)
{
        return (double)(end.tv_usec - start.tv_usec) +
               (double)(end.tv_sec - start.tv_sec) * 1000 * 1000;
}

static void value2human(double bytes, double time, double* data, double* speed,char* metric)
{
        if (bytes > 1000 && bytes < 1000*1000) {
                *data = ((double)bytes)/1000;
                *speed = *data/time;
                strcpy(metric, "Kb");
                return;
        } else if (bytes >= 1000*1000 && bytes < 1000*1000*1000) {
                *data = ((double)bytes)/(1000*1000);
                *speed = *data/time;
                strcpy(metric, "Mb");
                return;
        } else if (bytes >= 1000*1000*1000) {
                *data = ((double)bytes)/(1000*1000*1000);
                *speed = *data/time;
                strcpy(metric, "Gb");
                return;
        } else {
                *data = (double)bytes;
                *speed = *data/time;
                strcpy(metric, "bytes");
                return;
        }
}

void parent(pid_t child, void* mem)
{
struct timeval start, end;
uint32_t chunksize;
double total = 0;
double secs, ddata, dspeed;
char metric[16];

	signal(SIGALRM, alarm_handler);

	/* set a default value in shared memory */

	for (chunksize = 256; chunksize <= (64 * 1024); chunksize *= 2) {
		memset(mem, 0x33, chunksize);

		printf("\tEncrypting in chunks of %d bytes: ", chunksize);
		fflush(stdout);
		
		total = 0;
		must_finish = 0;
		alarm(5);
		gettimeofday(&start, NULL);
		
		do {
			memcpy(mem, &chunksize, sizeof(chunksize));

			sem_post(enc_sem);

			sem_wait(get_sem);
			total+=chunksize;
		} while(must_finish == 0);
		
		gettimeofday(&end, NULL);
		
		secs = udifftimeval(start, end)/ 1000000.0;
		value2human(total, secs, &ddata, &dspeed, metric);
		printf ("done. %.2f %s in %.2f secs: ", ddata, metric, secs);
		printf ("%.2f %s/sec\n", dspeed, metric);
	}

}

int main()
{
	int shmid, shmid2;
	char c, *shm, *s, *semmem;
	pid_t pid;
	
	signal(SIGUSR1, SIG_IGN);
	signal(SIGUSR2, SIG_IGN);
	
	if ((shmid = shmget(IPC_PRIVATE, SHM_SIZE, IPC_CREAT | 0660)) < 0) {
		perror("shmget fail");
		return 1;
	}

	if ((shm = (char *) shmat(shmid, 0, 0)) == (char *) -1) {
		perror("shmat : parent");
		return 2;
	}

	if ((shmid2 = shmget(IPC_PRIVATE, 2*sizeof(sem_t), IPC_CREAT | 0660)) < 0) {
		perror("shmget fail");
		return 1;
	}

	if ((semmem = (char *) shmat(shmid2, 0, 0)) == (char *) -1) {
		perror("shmat : parent");
		return 2;
	}
	
	enc_sem = (void*)semmem;
	get_sem = (void*)semmem + sizeof(sem_t);
	
	sem_init(enc_sem, 1, 0);
	sem_init(get_sem, 1, 0);
	
	printf("Addresses in parent\n");
	printf("shared mem: %p\n", shm);

	s = shm;		// s now references shared mem
	for (c = 'A'; c <= 'Z'; ++c)	// put some info there
		*s++ = c;
	*s = '\0';		// terminate the sequence

	switch (pid=fork()) {
	case -1:
		perror("fork");
		return 3;
	default:
		parent(pid, shm);
		kill(pid, SIGTERM);
		wait(0);	// let the child finish
		shmdt(shm);
		shmctl(shmid, IPC_RMID, (struct shmid_ds *) 0);
		break;
	case 0:
		child(getppid(), shm);
		shmdt(shm);
		break;
	}
	return 0;
}