2019-11-08 in class
This commit is contained in:
parent
d5928cbe06
commit
0de237cd78
2 changed files with 15 additions and 9 deletions
|
@ -67,7 +67,6 @@ int add_from_file(struct processes* ps, const char* filename) {
|
||||||
// printf("%d %d %s %d %f %ld %ld %ld %s\n", p.pid, p.ppid,
|
// printf("%d %d %s %d %f %ld %ld %ld %s\n", p.pid, p.ppid,
|
||||||
// p.user, p.priority, p.cpu, p.resident_size, p.size,
|
// p.user, p.priority, p.cpu, p.resident_size, p.size,
|
||||||
// p.virtual_size, p.command);
|
// p.virtual_size, p.command);
|
||||||
printf("%ld\n", ps->size);
|
|
||||||
if (ps->size == ps->capacity) {
|
if (ps->size == ps->capacity) {
|
||||||
ps->capacity *= 2;
|
ps->capacity *= 2;
|
||||||
struct process* new_procs = realloc(ps->procs,
|
struct process* new_procs = realloc(ps->procs,
|
||||||
|
@ -117,16 +116,17 @@ struct query_result* search(struct processes* ps, const struct query* q) {
|
||||||
add = add && (q->rss == 0 || (q->rss < 0 &&
|
add = add && (q->rss == 0 || (q->rss < 0 &&
|
||||||
(q->rss * -1) < p->resident_size) ||
|
(q->rss * -1) < p->resident_size) ||
|
||||||
(q->rss > 0 && q->rss == p->resident_size));
|
(q->rss > 0 && q->rss == p->resident_size));
|
||||||
add = (q->size == 0 || (q->size < 0 &&
|
add = add && (q->size == 0 || (q->size < 0 &&
|
||||||
(q->size * -1) < p->size) ||
|
(q->size * -1) < p->size) ||
|
||||||
(q->size > 0 && q->size == p->size));
|
(q->size > 0 && q->size == p->size));
|
||||||
add = (q->vsize == 0 || (q->vsize < 0 &&
|
add = add && (q->vsize == 0 || (q->vsize < 0 &&
|
||||||
(q->vsize * -1) < p->virtual_size) ||
|
(q->vsize * -1) < p->virtual_size) ||
|
||||||
(q->vsize > 0 && q->vsize == p->virtual_size));
|
(q->vsize > 0 && q->vsize == p->virtual_size));
|
||||||
add = (q->cpu_usage == 0 || (q->cpu_usage < 0 &&
|
add = add && (q->cpu_usage == 0 || (q->cpu_usage < 0 &&
|
||||||
q->cpu_usage * -1 < p->cpu) ||
|
q->cpu_usage * -1 < p->cpu) ||
|
||||||
(q->cpu_usage > 0 && q->cpu_usage == p->cpu));
|
(q->cpu_usage > 0 && q->cpu_usage == p->cpu));
|
||||||
|
|
||||||
|
|
||||||
if (add) {
|
if (add) {
|
||||||
res->procs[res->size] = p;
|
res->procs[res->size] = p;
|
||||||
res->size++;
|
res->size++;
|
||||||
|
@ -144,15 +144,21 @@ struct query_result* search(struct processes* ps, const struct query* q) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if(res->size == 0) {
|
||||||
|
free(res->procs);
|
||||||
|
free(res);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
struct query_result* next(struct query_result* q) {
|
struct query_result* next(struct query_result* q) {
|
||||||
|
q->i++;
|
||||||
if (q->i == q->size) {
|
if (q->i == q->size) {
|
||||||
terminate_query(q);
|
terminate_query(q);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
q->i++;
|
|
||||||
return q;
|
return q;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -17,10 +17,10 @@ int main() {
|
||||||
q.size = 0;
|
q.size = 0;
|
||||||
q.vsize = 0;
|
q.vsize = 0;
|
||||||
q.cpu_usage = 0;
|
q.cpu_usage = 0;
|
||||||
|
|
||||||
struct query_result * r1;
|
struct query_result * r1;
|
||||||
struct query_result * r2;
|
struct query_result * r2;
|
||||||
|
|
||||||
int count = 0;
|
int count = 0;
|
||||||
|
|
||||||
for (r1 = search(p1, &q); r1 != 0; r1 = next(r1))
|
for (r1 = search(p1, &q); r1 != 0; r1 = next(r1))
|
||||||
|
@ -33,7 +33,7 @@ int main() {
|
||||||
|
|
||||||
assert(add_from_file(p2, "tests/proc1.txt"));
|
assert(add_from_file(p2, "tests/proc1.txt"));
|
||||||
assert(add_from_file(p2, "tests/proc2.txt"));
|
assert(add_from_file(p2, "tests/proc2.txt"));
|
||||||
|
|
||||||
r1 = search(p1, &q);
|
r1 = search(p1, &q);
|
||||||
r2 = search(p2, &q);
|
r2 = search(p2, &q);
|
||||||
|
|
||||||
|
@ -56,7 +56,7 @@ int main() {
|
||||||
|
|
||||||
clear(p1);
|
clear(p1);
|
||||||
assert(search(p1, &q) == 0);
|
assert(search(p1, &q) == 0);
|
||||||
|
|
||||||
count = 0;
|
count = 0;
|
||||||
for (r2 = search(p2, &q); r2 != 0; r2 = next(r2))
|
for (r2 = search(p2, &q); r2 != 0; r2 = next(r2))
|
||||||
count += 1;
|
count += 1;
|
||||||
|
|
Reference in a new issue