-
Notifications
You must be signed in to change notification settings - Fork 0
/
jobs.c
54 lines (49 loc) · 1.32 KB
/
jobs.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
#include <stdio.h>
#include <sys/types.h>
#include <sys/wait.h>
#include "shell.h"
void exec_jobs(struct child_list *root)
{
int status;
int j = 1;
for (struct child_list *i = root->next; i != NULL; i = i->next, j++)
{
int ret = waitpid(i->pid, &status, WNOHANG | WUNTRACED | WCONTINUED); // WHOHANG is non-blocking
if (ret == -1)
{
perror("");
return;
}
else if (ret > 0)
{
if (WIFSTOPPED(status))
{
i->status = 0;
}
else if (WIFSIGNALED(status))
{
printf("\nProcess %s with pid %d ", i->name, i->pid);
fflush(stdout);
psignal(status, "");
// delete node
delete_node(i);
continue;
}
else if (WIFEXITED(status))
{
printf("\nProcess %s with pid %d ", i->name, i->pid);
printf("exited normally\n");
// delete node
delete_node(i);
continue;
}
else
{
i->status = 1;
}
}
printf("[%d] ", j);
printf(i->status ? "Running" : "Stopped");
printf(" %s [%d]\n", i->name, i->pid);
}
}