-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfgbg-15.c
More file actions
68 lines (55 loc) · 1.59 KB
/
fgbg-15.c
File metadata and controls
68 lines (55 loc) · 1.59 KB
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 "header.h"
#include "fgbg-15.h"
#include "activities-13.h"
#include "signals-14.h" // For current_fg_pid global
void execute_fg(char **args) {
if (args[1] == NULL) {
printf("fg: missing pid\n");
return;
}
pid_t pid = atoi(args[1]);
char command_name[1024];
// 1. Check if process exists in our list
if (get_process_command(pid, command_name) == 0) {
printf("No such process found\n");
return;
}
// 2. Remove from background list (it is now foreground)
remove_process(pid);
// 3. Send SIGCONT (in case it was stopped)
// Ignore error if it was already running
kill(pid, SIGCONT);
// 4. Update Global State
current_fg_pid = pid;
strcpy(current_fg_name, command_name);
// 5. Wait for it (Block shell)
// We reuse the WUNTRACED logic to handle Ctrl+Z again
int status;
waitpid(pid, &status, WUNTRACED);
// 6. Check if stopped again
if (WIFSTOPPED(status)) {
printf("\n[%d] Stopped %s\n", pid, command_name);
add_process(pid, command_name); // Add back to list
}
// Reset global
current_fg_pid = -1;
}
void execute_bg(char **args) {
if (args[1] == NULL) {
printf("bg: missing pid\n");
return;
}
pid_t pid = atoi(args[1]);
char temp[1024];
// 1. Check existence
if (get_process_command(pid, temp) == 0) {
printf("No such process found\n");
return;
}
// 2. Send SIGCONT
if (kill(pid, SIGCONT) == 0) {
printf("Resumed [%d] %s\n", pid, temp);
} else {
perror("bg");
}
}