-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpid_list.c
78 lines (65 loc) · 1.28 KB
/
pid_list.c
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
#include "pid_list.h"
/* this implementation is a doubly-linked, circular
* list with a dummy head node
*/
/* make a new empty list */
node *new_list()
{
node *head = (node *)malloc(sizeof(node));
head->pid = (pid_t)-1;
head->next = head;
head->prev = head;
return head;
}
/* add new pid list */
void add_node(node *head, pid_t pid)
{
if (head == NULL)
{
return;
}
node *new = (node *)malloc(sizeof(node));
new->pid = pid;
// add node to the end of the list
new->prev = head->prev;
new->next = head;
head->prev->next = new;
head->prev = new;
}
/* remove a pid from the list */
void remove_node(node *head, pid_t pid)
{
if (head == NULL)
{
return;
}
node *cur = head->next;
while (cur->pid != pid && cur != head)
{
cur = cur->next;
}
if (cur == head)
{
// didn't find pid, so just return
return;
}
// remove cur from the list
cur->prev->next = cur->next;
cur->next->prev = cur->prev;
free(cur);
}
/* free every node in the list */
void free_list(node *head)
{
if (head == NULL)
{
return;
}
node *cur = head->next;
while (cur != head)
{
cur = cur->next;
free(cur->prev);
}
free(head);
}