-
Notifications
You must be signed in to change notification settings - Fork 3
/
dirty.c
146 lines (127 loc) · 2.79 KB
/
dirty.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#include"main.h"
#define DIRTYSIZE 50
#define BUFSIZE 10000
void blockDesc(int state)
{
// Holds block description
struct termios nextState;
// Put the state of FD into termios
tcgetattr(0, &nextState);
if (state != 0) {
nextState.c_lflag |= ICANON;
tcsetattr(0, 0, &nextState);
}
else {
nextState.c_lflag &= ~ICANON;
nextState.c_cc[6] = 1;
tcsetattr(0, 0, &nextState);
}
}
// Prints error each time
void printDirtyError() {
printf("Invalid command\n");
}
// To print the dirty memory size
void printDirtySize()
{
// Open file
FILE* file = fopen("/proc/meminfo", "r");
// Check error
if (file == NULL) {
perror("File ");
return;
}
// Scan in the buffer
char buffer[BUFSIZE];
for(int i=0; i < DIRTYSIZE;i++)
fscanf(file,"%s",buffer);
printf("%s kB\n",buffer);
}
int kbDescription()
{
// Holds description of each memory unit
struct timeval timeValue;
fd_set fds;
// Initialise value = 0
timeValue.tv_usec = 0;
timeValue.tv_sec = 0;
FD_ZERO(&fds);
FD_SET(0, &fds);
select(1, &fds, NULL, NULL, &timeValue);
return FD_ISSET(0, &fds);
}
int checkDirtyError(ll totalArgsInEachCommand, char *c[]) {
// Checking errors
if(!(totalArgsInEachCommand==2 || totalArgsInEachCommand==4)) {
printDirtyError();
return 0;
}
return 1;
}
int getInputTime(ll totalArgsInEachCommand, char *c[], int *inputTime) {
// Get the input time based on values
if(strcmp(c[1], "dirty") == 0 && totalArgsInEachCommand == 2) inputTime = 2;
else
{
if(strcmp(c[1], "-n") == 0 && totalArgsInEachCommand == 4) {
if(strcmp(c[3],"dirty")==0) inputTime = atoi(c[2]);
// Error
else {
printDirtyError();
return 0;
}
}
else {
printDirtyError();
return 0;
}
}
return 1;
}
// Function to implement dirty
void dirty(ll totalArgsInEachCommand, char *c[])
{
// Check error
if(checkDirtyError(totalArgsInEachCommand, c) == 0) {
return;
}
int inputTime;
if(totalArgsInEachCommand == 2 && strcmp(c[1], "dirty") == 0) inputTime = 2;
else
{
if(totalArgsInEachCommand == 4 && strcmp(c[1], "-n") == 0) {
if(strcmp(c[3],"dirty") == 0) inputTime = atoi(c[2]);
else {
printDirtyError();
return ;
}
}
else {
printDirtyError();
return ;
}
}
// int inputTime;
// if (getInputTime(totalArgsInEachCommand, c, &inputTime) == 0) return;
long long int prevTime = -1;
long long int initime = time(0);
long long middleTime;
char inputKey;
int pos = 0;
blockDesc(0);
while(pos == 0) {
pos = kbDescription();
middleTime = time(0);
if(prevTime != middleTime && (middleTime - initime) % inputTime == 0) {
prevTime = middleTime;
printDirtySize();
}
// Take input from stdin until user presses 'q'
if(pos != 0) {
inputKey = fgetc(stdin);
if(inputKey != 'q') pos = 0;
else pos = 1;
}
}
blockDesc(1);
}