2019-11-08 in class
This commit is contained in:
parent
4a5e4170f7
commit
975d25698a
1 changed files with 37 additions and 4 deletions
|
@ -1,3 +1,4 @@
|
||||||
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include "processes.h"
|
#include "processes.h"
|
||||||
|
|
||||||
|
@ -10,7 +11,7 @@ struct process {
|
||||||
uint pid;
|
uint pid;
|
||||||
uint ppid;
|
uint ppid;
|
||||||
char user[9];
|
char user[9];
|
||||||
double priority;
|
float priority;
|
||||||
ulong resident_size;
|
ulong resident_size;
|
||||||
ulong size;
|
ulong size;
|
||||||
ulong virtual_size;
|
ulong virtual_size;
|
||||||
|
@ -20,24 +21,56 @@ struct process {
|
||||||
struct processes {
|
struct processes {
|
||||||
ulong size;
|
ulong size;
|
||||||
ulong capacity;
|
ulong capacity;
|
||||||
struct process procs[];
|
struct process* procs;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct processes* new_processes() {
|
struct processes* new_processes() {
|
||||||
struct processes* p = malloc(sizeof(struct processes) + INIT_CAP *
|
struct processes* p = malloc(sizeof(struct processes));
|
||||||
sizeof(struct process));
|
|
||||||
if (!p) {
|
if (!p) {
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
struct process* p_arr = malloc(INIT_CAP * sizeof(struct process));
|
||||||
|
if (!p_arr) {
|
||||||
|
free(p);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
p->size = 0;
|
p->size = 0;
|
||||||
p->capacity = INIT_CAP;
|
p->capacity = INIT_CAP;
|
||||||
|
p->procs = p_arr;
|
||||||
return p;
|
return p;
|
||||||
}
|
}
|
||||||
|
|
||||||
void delete(struct processes* p) {
|
void delete(struct processes* p) {
|
||||||
|
free(p->procs);
|
||||||
free(p);
|
free(p);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int add_from_file(struct processes* ps, const char* filename) {
|
||||||
|
FILE* f = fopen(filename, "r");
|
||||||
|
if (!f) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
struct process p;
|
||||||
|
while (fscanf(f, "%u %u %8s %f %lu %lu %lu %15s\n", &p.pid, &p.ppid,
|
||||||
|
p.user, &p.priority,&p.resident_size, &p.size,
|
||||||
|
&p.virtual_size, p.command) == 8) {
|
||||||
|
|
||||||
|
if (ps->size == ps->capacity) {
|
||||||
|
ps->capacity *= 2;
|
||||||
|
struct process* new_procs = realloc(ps->procs,
|
||||||
|
ps->capacity * sizeof(struct process));
|
||||||
|
if (!new_procs) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
ps->procs = new_procs;
|
||||||
|
}
|
||||||
|
|
||||||
|
ps->procs[ps->size] = p;
|
||||||
|
ps->size++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
Reference in a new issue