-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprocess_launch.c
68 lines (57 loc) · 1.59 KB
/
process_launch.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
#include "main.h"
int bg_cnt = 0;
char bg_proc_name[100][100];
void sigint_handler(int sig) {
int state;
pid_t pid = waitpid(-1, &state, WNOHANG);
if (pid != -1) {
if(!WIFSIGNALED(state)) {
for (int i = 0; i < bg_cnt; i++)
if (pid == bg_proc[i]) {
printf("\n[%d] + %d done\t%s\n", i + 1, pid, bg_proc_name[i]);
bg_proc[i] = -1;
bg_cnt--;
break;
}
}
}
}
int proc_launch(char **argv) {
int bg = 0;
for (int i = 0; argv[i] != NULL; i++) {
int len = strlen(argv[i]);
if (argv[i][len - 1] == '&') {
bg = 1;
argv[i][len - 1] = '\0';
if (argv[i][len - 2] == ' ')
argv[i][len - 2] = '\0';
}
}
pid_t pid = fork(), wpid;
int state;
if (pid == -1)
perror("ush");
else if (!pid) {
if (execvp(argv[0], argv) == -1)
perror("ush");
exit(EXIT_FAILURE);
} else {
if (!bg) {
wpid = waitpid(pid, &state, WUNTRACED);
while (1) {
if (!WIFEXITED(state) && !WIFSIGNALED(state)) {
wpid = waitpid(pid, &state, WUNTRACED);
continue;
}
break;
}
} else {
bg_proc[bg_cnt] = pid;
strcpy(bg_proc_name[bg_cnt++], argv[0]);
printf("[%d] %d\n", bg_cnt, pid);
signal(SIGCHLD, sigint_handler);
bg = 0;
}
}
return 1;
}