-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsignals-14.c
More file actions
49 lines (41 loc) · 1.03 KB
/
signals-14.c
File metadata and controls
49 lines (41 loc) · 1.03 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
#include "header.h"
#include "signals-14.h"
#include "activities-13.h"
// Global Definitions
pid_t current_fg_pid = -1;
char current_fg_name[1024] = "";
void execute_ping(char **args) {
if (args[1] == NULL || args[2] == NULL) {
printf("ping: missing arguments\n");
return;
}
pid_t pid = atoi(args[1]);
int sig_num = atoi(args[2]);
// Modulo 32 as per spec
sig_num = sig_num % 32;
if (kill(pid, 0) == -1) {
printf("No such process found\n");
return;
}
if (kill(pid, sig_num) == 0) {
printf("Sent signal %d to process with pid %d\n", sig_num, pid);
} else {
perror("ping");
}
}
void handle_sigint(int sig) {
if (current_fg_pid != -1) {
// Pass signal to the foreground child
kill(current_fg_pid, SIGINT);
} else {
printf("\n");
}
}
void handle_sigtstp(int sig) {
if (current_fg_pid != -1) {
kill(current_fg_pid, SIGTSTP);
} else {
// Do nothing if no foreground process
printf("\n");
}
}