-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuserapp.c
96 lines (90 loc) · 2.07 KB
/
userapp.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
#include "userapp.h"
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/time.h>
int is_in_list(unsigned int pid){
char buf[5000];
FILE* file_pr;
char* token;
size_t len;
unsigned int temp_pid;
unsigned int pid_list[100];
int counter = 0;
file_pr = fopen("/proc/mp2/status", "r");
len = fread(buf, sizeof(char), 4999, file_pr);
buf[len] = '\0';
fclose(file_pr);
// transfer to a list
for (token = strtok(buf,"\n"); token != NULL; token = strtok(NULL, "\n")){
sscanf(token, "%u", &temp_pid);
pid_list[counter] = temp_pid;
counter++;
}
// check if the current pid in the list
for (int i = 0; i < counter; i ++) {
if (pid_list[i] == pid) {
return 1;
}
}
return 0;
}
void job_yield(unsigned int pid)
{
FILE* file_pr;
file_pr = fopen("/proc/mp2/status", "w+");
fprintf(file_pr, "Y, %u", pid);
fclose(file_pr);
}
void job_deregister(unsigned int pid)
{
char command[500];
memset(command, '\0', 500);
sprintf(command, "echo \"D, %u\" > /proc/mp2/status", pid);
system(command);
}
void do_job(int num) {
long long int ret = 1;
for (int i = 1; i <= num; i ++) {
ret *= i;
}
}
int main(int argc, char *argv[])
{
struct timeval t0, wakeup_time, job_processing_time;
unsigned int pid;
unsigned int factorial_number;
char *period;
char *proc_time;
pid = getpid();
period = argv[1];
proc_time = argv[2];
int num_jobs;
char command[500];
factorial_number = 10;
memset(command, '\0', 500);
sprintf(command, "echo \"R, %u, %s, %s\" > /proc/mp2/status", pid, period, proc_time);
system(command);
// check if in list
if (!is_in_list(pid)){
exit(1);
}
num_jobs = atoi(argv[3]);
// record start time t0
gettimeofday(&t0, NULL);
// yield to Proc filesystem
job_yield(pid);
// do multiple jobs
for (int i = 0; i < num_jobs; i++) {
// record wakeup time wakeup_time
gettimeofday(&wakeup_time, NULL);
do_job(factorial_number);
// record job process time
gettimeofday(&job_processing_time, NULL);
job_yield(pid);
}
// de-register
job_deregister(pid);
return 0;
}