/
linked.c
130 lines (104 loc) · 2.94 KB
/
linked.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/* CMPSC 311, Spring 2013, Project 7
*
* Author: Jacob Jones
* Email: jaj5333@psu.edu
*
* Author: Scott Cheloha
* Email: ssc5145@psu.edu
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdbool.h>
#include <errno.h>
#include <string.h>
#include <ctype.h>
#include "wrapper.h"
#include "linked.h"
#include "pr7.h"
//------------------------------------------------------------------------------
struct process_list *process_list_allocate(void)
{
struct process_list *list = Malloc(sizeof(struct process_list), __func__, __LINE__);
list->head = list->tail = NULL;
list->reference_count = 0;
return list;
}
//------------------------------------------------------------------------------
void process_list_deallocate(struct process_list * const list)
{
if (--list->reference_count > 0)
{ return; }
struct node *prev = NULL;
for (struct node *p = list->head; p != NULL; p = p->next)
{
Free(prev, __func__, __LINE__); // Free(NULL) is harmless
Free(p->command, __func__, __LINE__);
prev = p;
}
Free(prev, __func__, __LINE__); // now, prev == list->tail
Free(list, __func__, __LINE__);
}
//------------------------------------------------------------------------------
void process_list_print(const struct process_list * const list)
{
if (list->head == NULL)
{ ; }
else
{
puts("Processes:");
puts(" PID PGID STATUS CMD");
for (struct node *p = list->head; p != NULL; p = p->next)
{
printf(" [%d] [%d] [%s] %s\n",
(int) p->pid, (int) p->pgid, p->status, p->command);
}
}
}
//------------------------------------------------------------------------------
void process_list_append(struct process_list * const list, const char *command, pid_t pid, pid_t pgid)
{
struct node *p = Malloc(sizeof(struct node), __func__, __LINE__);
p->next = NULL;
p->status = Strdup("RUNNING", __func__, __LINE__);
p->pid = pid;
p->pgid = pgid;
p->command = Strdup(command, __func__, __LINE__);
if (list->head == NULL) // empty list, list->tail is also NULL
{
list->head = list->tail = p;
}
else
{
list->tail->next = p;
list->tail = p;
}
}
//------------------------------------------------------------------------------
void process_list_pop(struct process_list * const list, pid_t pid)
{
struct node *prev = NULL;
for (struct node *p = list->head; p != NULL; p = p->next)
{
if (p->pid == pid)
{
if (prev == NULL)
{
list->head = p->next;
}
else
{
prev->next = p->next;
}
printf("\n--%s: %s (%d) has terminated.\n",
prog, p->command, (int) p->pid);
/* Free allocated resources for node */
Free(p->command, __func__, __LINE__);
Free(p->status, __func__, __LINE__);
Free(p, __func__, __LINE__);
return;
}
prev = p;
}
fprintf(stderr, "--%s: process %d is not currently running.\n", prog, (int) pid);
}