-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuserapp.c
More file actions
163 lines (131 loc) · 4.03 KB
/
userapp.c
File metadata and controls
163 lines (131 loc) · 4.03 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
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
#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include "userapp.h"
#include <sys/time.h>
#include <string.h>
#define SIZE 200
#define LINE_SIZE 100
const float MIN_CPU_TIME = 167.94;
static int pid;
static FILE * writefh = NULL;
static int writefno = -1;
long long unsigned fac(long long n) {
if (n<1) return 1;
return n*fac(n-1);
}
void REGISTER(unsigned long period, unsigned long job_process_time) {
char message[LINE_SIZE];
memset(message, 0, LINE_SIZE);
sprintf(message, "R, %d, %lu, %lu\n", pid, period, job_process_time);
write(writefno, message, LINE_SIZE);
// int ret = fprintf(writefh, "R, %d, %lu, %lu\n", pid, period, job_process_time);
}
void YIELD() {
char message[LINE_SIZE];
memset(message, 0, LINE_SIZE);
sprintf(message, "Y, %d\n", pid);
write(writefno, message, LINE_SIZE);
// int ret = fprintf(writefh, "Y, %d\n", pid);
}
void DEREGISTER() {
char message[LINE_SIZE];
memset(message, 0, LINE_SIZE);
sprintf(message, "D, %d\n", pid);
write(writefno, message, LINE_SIZE);
// fprintf(writefh, "D, %d\n", pid);
}
void do_job(){
int i;
for (i = 0; i < 1000000; i++) {
long long x = 0;
x = fac(33);
}
}
int process_in_list() {
char needle[32];
memset(needle, 0, 32);
sprintf(needle, "PID: %u", pid);
printf("searching for string: %s\n", needle);
FILE *f = fopen("/proc/mp2/status", "r");
if (f == NULL) {
perror("Could not open MP2 status file.");
exit(1);
}
size_t sz = 0;
char * lin = 0;
int found = 0;
do {
// fprintf(stderr, "Beginning readline\n");
ssize_t lsz = getline (&lin, &sz, f);
// fprintf(stderr, "Got linesize %zu \n", lsz);
// fprintf(stderr, "contents: %s \n", lin);
if (lsz >= 0) {
char *find = strstr(lin, needle);
if (find != NULL) {
found = 1;
}
}
free(lin);
lin = 0;
} while (!feof (f));
fclose (f);
return found;
}
int main(int argc, char* argv[]) {
// get pid
pid = getpid();
// write pid as a string to the proc filesystem
printf("This is my PID: %d\n", pid);
if (argc != 3) {
perror("usage: ./userapp period job_process_time (times in milliseconds)");
exit(1);
}
unsigned long period = strtoul(argv[1], NULL, 10);
unsigned int job_process_time = strtoul(argv[2], NULL, 10);
if (job_process_time < MIN_CPU_TIME) {
perror("Job process time must be at least 167.94 ms");
exit(1);
}
if (job_process_time > period) {
perror("Period must be bigger than job process time.");
exit(1);
}
writefh = fopen("/proc/mp2/status", "w");
if (writefh == NULL) {
perror("Could not open MP2 status file.");
exit(1);
}
writefno = fileno(writefh);
REGISTER(period, job_process_time);
int in_list = process_in_list();
// passed registration successfully
if (!in_list) {
perror("Process rejected by admission control.");
exit(1);
} else {
fprintf(stderr, "Accepted by admission control!\n");
}
YIELD();
// printf("woken up from yield\n");
// // struct timeval start, end;
// // long unsigned secs_used,micros_used;
// // gettimeofday(&start, NULL);
int j;
for (j=0; j < 5; j++) { // while exist jobs
// printf("Hey i'm awake!\n");
do_job();
YIELD();
}
// // gettimeofday(&end, NULL);
// // printf("start: %lu secs, %lu usecs\n",start.tv_sec,start.tv_usec);
// // printf("end: %lu secs, %lu usecs\n",end.tv_sec,end.tv_usec);
// // secs_used=(end.tv_sec - start.tv_sec); //avoid overflow by subtracting first
// // micros_used= ((secs_used*1000000) + end.tv_usec) - (start.tv_usec);
// // printf("micros_used: %lu\n",micros_used);
// // printf("millis_used: %lu\n",micros_used / 1000);
// // printf("avg time: %G\n",micros_used / 1000 / 100.0);
DEREGISTER();
return 0;
}