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 "processes.h"
|
||||
|
||||
|
@ -10,7 +11,7 @@ struct process {
|
|||
uint pid;
|
||||
uint ppid;
|
||||
char user[9];
|
||||
double priority;
|
||||
float priority;
|
||||
ulong resident_size;
|
||||
ulong size;
|
||||
ulong virtual_size;
|
||||
|
@ -20,24 +21,56 @@ struct process {
|
|||
struct processes {
|
||||
ulong size;
|
||||
ulong capacity;
|
||||
struct process procs[];
|
||||
struct process* procs;
|
||||
};
|
||||
|
||||
struct processes* new_processes() {
|
||||
struct processes* p = malloc(sizeof(struct processes) + INIT_CAP *
|
||||
sizeof(struct process));
|
||||
struct processes* p = malloc(sizeof(struct processes));
|
||||
if (!p) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
struct process* p_arr = malloc(INIT_CAP * sizeof(struct process));
|
||||
if (!p_arr) {
|
||||
free(p);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
p->size = 0;
|
||||
p->capacity = INIT_CAP;
|
||||
p->procs = p_arr;
|
||||
return p;
|
||||
}
|
||||
|
||||
void delete(struct processes* p) {
|
||||
free(p->procs);
|
||||
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