-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsignals.cpp
More file actions
30 lines (25 loc) · 907 Bytes
/
signals.cpp
File metadata and controls
30 lines (25 loc) · 907 Bytes
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
#include <iostream>
#include <signal.h>
#include "signals.h"
#include <cstring>
#include "Commands.h"
// #include "../../../../../AppData/Local/Programs/winlibs-x86_64-posix-seh-gcc-15.1.0-mingw-w64msvcrt-12.0.0-r1/mingw64/include/c++/15.1.0/bits/this_thread_sleep.h"
#include <unistd.h> // for sleep
using namespace std;
void ctrlCHandler(int sig_num) {
const char* msg = "smash: got ctrl-C\n"; // maybe without the \n
write(STDOUT_FILENO, msg, strlen(msg));
SmallShell &sm = SmallShell::getInstance();
pid_t fg_pid = sm.getFgPid();
if (fg_pid > 0) { // there is a child process
if (kill(fg_pid, SIGKILL) == -1) {
return;
}
char buf[1024];
int size = snprintf(buf, sizeof(buf), "smash: process %d was killed\n", fg_pid);
if (size > 0) {
write(STDOUT_FILENO, buf, size);
}
sm.clear_fg_pid();
}
}