2019-11-08 in class

This commit is contained in:
Claudio Maggioni 2019-11-08 10:05:10 +01:00
parent d5928cbe06
commit 0de237cd78
2 changed files with 15 additions and 9 deletions

View file

@ -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,
// p.user, p.priority, p.cpu, p.resident_size, p.size,
// p.virtual_size, p.command);
printf("%ld\n", ps->size);
if (ps->size == ps->capacity) {
ps->capacity *= 2;
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 &&
(q->rss * -1) < 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 > 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 > 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 > 0 && q->cpu_usage == p->cpu));
if (add) {
res->procs[res->size] = p;
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;
}
struct query_result* next(struct query_result* q) {
q->i++;
if (q->i == q->size) {
terminate_query(q);
return NULL;
}
q->i++;
return q;
}

View file

@ -17,10 +17,10 @@ int main() {
q.size = 0;
q.vsize = 0;
q.cpu_usage = 0;
struct query_result * r1;
struct query_result * r2;
int count = 0;
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/proc2.txt"));
r1 = search(p1, &q);
r2 = search(p2, &q);
@ -56,7 +56,7 @@ int main() {
clear(p1);
assert(search(p1, &q) == 0);
count = 0;
for (r2 = search(p2, &q); r2 != 0; r2 = next(r2))
count += 1;