-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbg.c
70 lines (66 loc) · 1.53 KB
/
bg.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
#include "headers.h"
#include "common.h"
void bg(char *subcom)
{
int child = fork();
if (child == -1)
{
printf(ERROR_COLOR "%s: fork fail!" DEFAULT_COLOR "\n", subcom);
return;
}
else if (child == 0)
{ // child
trimstr(subcom);
setpgrp();
syscom(subcom);
}
else
{ // parent
printf("Child process running in the background with PID: %d\n", child);
bgs[bgi].id = child;
strcpy(bgs[bgi].comm, subcom);
bgi++;
}
}
void handle_sigchld()
{
int status;
int pid;
while ((pid = waitpid(-1, &status, WNOHANG)) > 0)
{
if (WIFEXITED(status))
{
printf("Process with PID %d exited normally.\n", pid);
delete_bg(pid);
// fflush(stdout);
}
else if (WIFSIGNALED(status))
{
printf("Process with PID %d exited due to signal.\n", pid);
delete_bg(pid);
// fflush(stdout);
}
else if (WIFSTOPPED(status))
printf("Process with PID %d stopped.\n", pid);
else if (WIFCONTINUED(status))
printf("Process with PID %d continued.\n", pid);
}
}
void delete_bg(int pid)
{
int i;
for (i = 0; i < bgi; i++)
{
if (bgs[i].id == pid)
{
int j;
for (j = i; j < bgi - 1; j++)
{
bgs[j].id = bgs[j + 1].id;
strcpy(bgs[j].comm, bgs[j + 1].comm);
}
bgi--;
break;
}
}
}