-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
129 lines (105 loc) · 2.96 KB
/
main.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
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
#include <stdio.h>
#include <stdlib.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <stdbool.h>
#include <fcntl.h>
#include <signal.h>
#include <pthread.h>
#include <values.h>
#include <time.h>
#include "util.h"
#ifdef _WIN32
#define PS "\\"
#else
#define PS "/"
#endif
#define MATRIX_WIDTH 24
#define MATRIX_HEIGHT 6
bool shutdown = false;
void handleSignal(int signal) {
shutdown = true;
printf("Received signal %s\n", strsignal(signal));
}
struct matrix_info {
int* matrix;
int size;
int min;
int max;
};
void* check_loop(void* args) {
srand(time(NULL));
struct matrix_info* info = args;
struct hsv_color hsv = {0.0f, 1.0f, 1.0f};
for (float i = 0; i < 360; ++i) {
hsv.hue = i;
struct rgb_color* rgb = hsv_to_rgb(&hsv);
printf("RGB at angle %f: r=%f g=%f b=%f\n", i, rgb->red, rgb->green, rgb->blue);
free(rgb);
}
while (!shutdown) {
long offset = random_at_most(info->size);
int value = (int)random_at_most(MAXINT);
printf("Mutated %li, new value: %d\n", offset, value);
info->matrix[offset] = value;
usleep(1000000);
}
return NULL;
}
int main() {
int matrixSize = MATRIX_WIDTH * MATRIX_HEIGHT;
size_t bufferSize = sizeof(int) * matrixSize;
char *homeDir = getenv("HOME");
if (homeDir == NULL) {
homeDir = ".";
}
char *filePath = concat(concat(homeDir, PS), "test.dat");
int fd = open(filePath, O_RDWR | O_CREAT | O_SYNC);
if (fd < 0) {
printf("Failed to open file: %s! Error: %s (%d)\n", filePath, strerror(errno), errno);
return errno;
}
printf("Opened file %s!\n", filePath);
off_t size = lseek(fd, 0, SEEK_END);
if (size == 0) {
char buf[bufferSize];
memset(&buf, 0, bufferSize);
write(fd, &buf, bufferSize);
lseek(fd, 0, SEEK_SET);
fchmod(fd, 0644);
}
int *keyMatrix = (int *) mmap(NULL, bufferSize, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
if (keyMatrix == MAP_FAILED) {
printf("Failed to map file into memory: %s! Error: %s (%d)\n", filePath, strerror(errno), errno);
return errno;
}
printf("address: %p\n", keyMatrix);
for (int i = 0; i < matrixSize; ++i) {
printf("num at %d: %d\n", i, keyMatrix[i]);
keyMatrix[i] = i;
}
struct matrix_info info = {keyMatrix, matrixSize, MAXINT, MININT};
for (int i = 0; i < info.size; ++i) {
int val = info.matrix[i];
if (val < info.min) {
info.min = val;
}
if (val > info.max) {
info.max = val;
}
}
pthread_t thread;
pthread_create(&thread, NULL, check_loop, &info);
signal(SIGINT, handleSignal);
signal(SIGTERM, handleSignal);
pause();
pthread_join(thread, NULL);
msync(keyMatrix, bufferSize, MS_SYNC);
munmap(keyMatrix, bufferSize);
close(fd);
free(filePath);
return 0;
}