-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlistproc.c
executable file
·219 lines (161 loc) · 3.73 KB
/
listproc.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
/*
TITLE: OPERATING SYSTEMS LABS
AUTHOR 1: MARTIÑO RIVERA DOURADO
AUTHOR 2: CARMEN CORRALES CAMELLO
DATE: 13/12/2017
*/
/*
* Process list, implementation of linked list with a header node (1)
*
* */
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <signal.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <sys/types.h>
#include <sys/wait.h>
#include "listproc.h"
struct node * proc_createNode(){
struct node * tmp = malloc(sizeof(struct node));
if(tmp == NULL){
printf("Out of space\n");
exit(EXIT_FAILURE);
}
return tmp;
}
procList proc_createList(){
struct node * node = proc_createNode();
node->next = NULL;
return node;
}
int proc_isEmptyList(procList l){
return (l->next == NULL);
}
posP proc_first(procList l){
posP p;
if(proc_isEmptyList(l))
p = l;
else
p = l->next;
return p;
}
posP proc_next(posP p){
return p->next;
}
int proc_isLast(struct node * p){
return(p->next == NULL);
}
void proc_insert(elemP * e, procList l){
struct node * tmp = proc_createNode();
posP p = proc_first(l);
while(!proc_isLast(p))
p = p->next; // We get the last position
tmp->element = e;
tmp->next = p->next;
p->next = tmp;
}
void proc_clear(procList l){
if(proc_isEmptyList(l))
return;
posP p = proc_first(l);
posP nextPos;
while(p != NULL){
nextPos = p->next;
free(p);
p = nextPos;
}
l->next = NULL;
}
posP proc_find_pid(procList l, pid_t reqPid){
posP p = proc_first(l);
while(p != NULL){
if(p->element->pid == reqPid)
break;
p = p->next;
}
return p;
}
elemP * proc_createElement(pid_t pid, int prio, char * args[], char * date,
int status, int signal){
elemP * aux = malloc(sizeof(elemP));
int i;
// Copy of values in the element
aux->pid = pid;
aux->prio = prio;
// Copy of cmd field
strcpy(aux->cmd,"");
for(i = 0; args[i]!=NULL; i++){
strcat(aux->cmd, args[i]);
strcat(aux->cmd, " ");
}
strcpy(aux->date, date);
aux->status = status;
aux->signal = signal;
return aux;
}
elemP proc_getElement(posP p){
return *(p->element);
}
void proc_updateList(procList l){
/*
Update the list asking the state and signals to the processes in the list
*/
if(proc_isEmptyList(l))
return;
posP p = proc_first(l);
elemP* e;
int prio, state;
while(p != NULL){
e = p->element;
if((prio = getpriority(PRIO_PROCESS, (id_t)e->pid))!=-1)
e->prio = prio;
if (waitpid(e->pid, &state, WNOHANG | WUNTRACED | WCONTINUED)==e->pid){
e->status = enumStatus(state);
if(WIFSIGNALED(state))
e->signal = WTERMSIG(state);
else if(WIFEXITED(state))
e->signal = WEXITSTATUS(state);
else if(WIFSTOPPED(state))
e->signal = WSTOPSIG(state);
}
p = proc_next(p);
}
}
void proc_show(elemP e){
/*
Show the element of the list, the info of a process
*/
char signalField[10];
char * statusField;
strcpy(signalField, "");
if(e.status != ACTIVE){ // If the process is NOT active
if(e.status != TERMINATED){ // If the processes is not terminated
strcat(signalField, "(");
strcat(signalField, nameSignal(e.signal)); // We show the signal name
strcat(signalField, ")");
}
else{
sprintf(signalField, "(%d)", e.signal); // We get the exit status
}
}
statusField = nameStatus(e.status); // We get the name of the state from the enumeration value
printf("%d %s %s p=%d %s %s\n", e.pid, statusField,
signalField, e.prio, e.date, e.cmd);
}
void proc_showList(procList l){
/*
Show the list of processes
*/
if(proc_isEmptyList(l))
return;
posP p = proc_first(l);
elemP e;
proc_updateList(l); // Before, we update the list asking all processes their states
while(p != NULL){
e = proc_getElement(p);
proc_show(e); // Show each independent element
p = proc_next(p);
}
}