-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata_structures.c
More file actions
88 lines (76 loc) · 1.51 KB
/
Copy pathdata_structures.c
File metadata and controls
88 lines (76 loc) · 1.51 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include "functions.h"
// Linked list to deal with jobs
void insert(node** list, int pid, int status){
node* new_node = (node*) malloc(sizeof(node));
new_node->pid = pid;
new_node->status = status;
new_node->prox = NULL;
if(*list == NULL){
*list = new_node;
}else{
node* ptr = *list;
while(ptr->prox != NULL){
ptr = ptr->prox;
}
ptr->prox = new_node;
}
}
void update_status(node** list, int pid, int status){
node* ptr = *list;
while(ptr != NULL){
if(ptr->pid == pid){
ptr->status = status;
return;
}
ptr = ptr->prox;
}
}
void del(node** list, int pid){
if(*list == NULL) return;
node* ptr = *list;
if(ptr->pid == pid){
(*list) = (*list)->prox;
free(ptr);
}else{
while(ptr->prox != NULL && (ptr->prox)->pid != pid){
ptr = ptr->prox;
}
if(ptr->prox != NULL){
node *aux = ptr->prox;
ptr->prox = aux->prox;
free(aux);
}
}
}
void kill_jobs_and_free_memory(node* lista){
if(lista == NULL)
return;
kill_jobs_and_free_memory(lista->prox);
kill(lista->pid, 9);
free(lista);
}
// Linked list of commands
void add_cmd(cnode** list, char * val){
cnode* new_node = (cnode*) malloc(sizeof(cnode));
strcpy(new_node->cmd, val);
new_node->prox = NULL;
if(*list == NULL){
*list = new_node;
}else{
cnode* ptr = *list;
while(ptr->prox != NULL){
ptr = ptr->prox;
}
ptr->prox = new_node;
}
}
void clear_cmd(cnode* list){
if(list == NULL)
return;
clear_cmd(list->prox);
free(list);
}