2019-11-08 in class

This commit is contained in:
Claudio Maggioni 2019-11-08 09:53:02 +01:00
parent c19b1f83ea
commit d5928cbe06

View file

@ -8,14 +8,14 @@ typedef unsigned long int ulong;
const ulong INIT_CAP = 1024;
struct process {
uint pid;
uint ppid;
int pid;
int ppid;
char user[9];
uint priority;
int priority;
float cpu;
ulong resident_size;
ulong size;
ulong virtual_size;
long resident_size;
long size;
long virtual_size;
char command[16];
};
@ -61,10 +61,13 @@ int add_from_file(struct processes* ps, const char* filename) {
}
struct process p;
while (fscanf(f, "%u %u %8s %u %f %lu %lu %lu %15s\n", &p.pid, &p.ppid,
while (fscanf(f, "%d %d %8s %d %f %ld %ld %ld %15s\n", &p.pid, &p.ppid,
p.user, &p.priority, &p.cpu, &p.resident_size, &p.size,
&p.virtual_size, p.command) == 8) {
&p.virtual_size, p.command) == 9) {
// 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,
@ -109,17 +112,17 @@ struct query_result* search(struct processes* ps, const struct query* q) {
struct process* p = &(ps->procs[i]);
add = (q->priority == 0 || (q->priority < 0 &&
(uint)(q->priority * -1) < p->priority) ||
(q->priority > 0 && (uint) q->priority == p->priority));
(q->priority * -1) < p->priority) ||
(q->priority > 0 && q->priority == p->priority));
add = add && (q->rss == 0 || (q->rss < 0 &&
(ulong)(q->rss * -1) < p->resident_size) ||
(q->rss > 0 && (ulong) q->rss == p->resident_size));
(q->rss * -1) < p->resident_size) ||
(q->rss > 0 && q->rss == p->resident_size));
add = (q->size == 0 || (q->size < 0 &&
(ulong)(q->size * -1) < p->size) ||
(q->size > 0 && (ulong) q->size == p->size));
(q->size * -1) < p->size) ||
(q->size > 0 && q->size == p->size));
add = (q->vsize == 0 || (q->vsize < 0 &&
(ulong)(q->vsize * -1) < p->virtual_size) ||
(q->vsize > 0 && (ulong) q->vsize == p->virtual_size));
(q->vsize * -1) < p->virtual_size) ||
(q->vsize > 0 && q->vsize == p->virtual_size));
add = (q->cpu_usage == 0 || (q->cpu_usage < 0 &&
q->cpu_usage * -1 < p->cpu) ||
(q->cpu_usage > 0 && q->cpu_usage == p->cpu));
@ -157,3 +160,14 @@ void terminate_query(struct query_result* q) {
free(q->procs);
free(q);
}
int get_pid(struct query_result* r) { return r->procs[r->i]->pid; }
int get_ppid(struct query_result* r) { return r->procs[r->i]->ppid; }
const char* get_user (struct query_result* r) { return r->procs[r->i]->user; }
int get_priority(struct query_result* r) { return r->procs[r->i]->priority; }
float get_cpu_usage(struct query_result* r) { return r->procs[r->i]->cpu; }
long get_rss(struct query_result* r) { return r->procs[r->i]->resident_size; }
long get_size(struct query_result* r) { return r->procs[r->i]->size; }
long get_vsize(struct query_result* r) { return r->procs[r->i]->virtual_size; }
const char* get_command(struct query_result* r) { return r->procs[r->i]->command; }