-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdemon.c
More file actions
108 lines (83 loc) · 1.85 KB
/
Copy pathdemon.c
File metadata and controls
108 lines (83 loc) · 1.85 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
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
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#define max_proces 1000
const int MTX_OPEN = 331;
const int MTX_CLOSE = 332;
const int MTX_LOCK = 333;
const int MTX_UNLOCK = 334;
const int MTX_LIST = 335;
const int MTX_GRANT = 336;
int last_modify[max_proces];
int main(int argc, char** argv)
{
FILE *fp = NULL;
pid_t process_id = 0;
pid_t sid = 0;
// Create child process
process_id = fork();
// Indication of fork() failure
if (process_id < 0)
{
printf("fork failed!\n");
exit(1);
}
if (process_id > 0)
{
// PARENT PROCESS. Need to kill it.
printf("process_id of child process %d \n", process_id);
// return success in exit status
exit(0);
}
// unmask the file mode
umask(0);
// set new session
sid = setsid();
if(sid < 0)
{
exit(1);
}
// Change the current working directory to root.
chdir("/");
// Close stdin. stdout and stderr
close(STDIN_FILENO);
close(STDOUT_FILENO);
close(STDERR_FILENO);
// fp = fopen ("Log.txt", "w+");
pid_t *p = malloc(max_proces * sizeof(pid_t));
int *d = malloc(max_proces * sizeof(int));
memset(last_modify, 0, sizeof(last_modify));
int check_step = 0;
while (1)
{
// Dont block context switches, let the process sleep for some time
sleep(2);
// fprintf(fp, "Logging info...\n");
// fflush(fp);
int many = syscall(MTX_LIST, d, p, max_proces);
int i;
for(i = 1; i < many; i++) {
int swap_index = rand()%i;
int aux = d[swap_index];
d[swap_index] = d[i];
d[i] = aux;
aux = p[swap_index];
p[swap_index] = p[i];
p[i] = aux;
}
// versioning last_modify
check_step ++;
for(i = 0; i < many; i++) {
if(last_modify[d[i]] != check_step) {
last_modify[d[i]] = check_step;
syscall(MTX_GRANT, d[i], p[i]);
}
}
}
// fclose(fp);
return 0;
}