-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackgroundprocesses.c
More file actions
54 lines (46 loc) · 1.65 KB
/
backgroundprocesses.c
File metadata and controls
54 lines (46 loc) · 1.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <bits/types/time_t.h>
#include <malloc.h>
#include <time.h>
#include <string.h>
#include <stdbool.h>
#define MAX_BACKGROUND_PROCESSES 150
struct BackgroundProcess {
int pid;
char *name;
char *cmd;
};
extern struct BackgroundProcess backgroundProcesses[];
double currentTimeInMilliseconds() {
struct timespec spec;
clock_gettime(CLOCK_REALTIME, &spec);
return spec.tv_sec * 1000 + spec.tv_nsec / 1.0e6;
}
void addBackgroundProcess(int pid, const char *name, const char *cmd) {
int indexToInsert = 0;
for (; indexToInsert< 150; indexToInsert++) {
if (backgroundProcesses[indexToInsert].name == NULL) {
break;
}
}
backgroundProcesses[indexToInsert].pid = pid;
backgroundProcesses[indexToInsert].name = malloc(sizeof(char) * strlen(name));
strcpy(backgroundProcesses[indexToInsert].name, name);
backgroundProcesses[indexToInsert].cmd = malloc(sizeof(char) * strlen(cmd));
strcpy(backgroundProcesses[indexToInsert].cmd, cmd);
}
void removeBackgroundProcess(int index) {
free(backgroundProcesses[index].name);
free(backgroundProcesses[index].cmd);
backgroundProcesses[index].name = NULL;
backgroundProcesses[index].cmd = NULL;
backgroundProcesses[index].pid = -1;
// Shift all the processes after this one
for (int i = index; i < MAX_BACKGROUND_PROCESSES - 1; i++) {
backgroundProcesses[i].cmd = backgroundProcesses[i + 1].cmd;
backgroundProcesses[i].name = backgroundProcesses[i + 1].name;
backgroundProcesses[i].pid = backgroundProcesses[i + 1].pid;
if (backgroundProcesses[i].name == NULL) {
break;
}
}
}