-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsignals.cpp
More file actions
83 lines (78 loc) · 2.47 KB
/
Copy pathsignals.cpp
File metadata and controls
83 lines (78 loc) · 2.47 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#include <iostream>
#include <signal.h>
#include "signals.h"
#include "Commands.h"
#include "sys/wait.h"
using namespace std;
void ctrlZHandler(int sig_num) {
std::cout<<"smash: got ctrl-Z"<<std::endl;
SmallShell& smash = SmallShell::getInstance();
int currentProcess = smash.getCurrentProcess();
if(currentProcess > 0){
if(kill(currentProcess,SIGSTOP) == -1){
perror("smash error: kill failed");
return;
}
smash.addJob(currentProcess,smash.getCurrentCommand(),true);
smash.setCurrentProcess(-1);
smash.setCurrentCommand("");
smash.stopJob(currentProcess);
std::cout<<"smash: process "<<currentProcess<<" was stopped"<<std::endl;
}
return;
}
void ctrlCHandler(int sig_num) {
std::cout<<"smash: got ctrl-C"<<std::endl;
SmallShell& smash = SmallShell::getInstance();
int currentProcess = smash.getCurrentProcess();
int JID;
if(currentProcess > 0){
if(kill(currentProcess,SIGKILL) == -1){
perror("smash error: kill failed");
return;
}
smash.removeJob(smash.getJobJID(currentProcess));
std::cout<<"smash: process "<<currentProcess<<" was killed"<<std::endl;
}
return;
}
void alarmHandler(int sig_num) {
std::cout<<"smash: got an alarm"<<std::endl;
SmallShell& smash = SmallShell::getInstance();
while(smash.TimedJobsNum() != 0){
TimedJob toKill = smash.getTimedListHead();
int PID = toKill.getPID();
int JID = smash.getJobJID(PID);
time_t finishingTime = toKill.getFinisingTime();
if(finishingTime <= time(nullptr)){
if(toKill.isFinished() || waitpid(PID,nullptr,WNOHANG) > 0){
smash.removeJob(JID);
smash.popTimedJobsList();
continue;
}
if(isBuiltIn(toKill.getCmdLine()) ){
smash.popTimedJobsList();
continue;
}
if(JID != -1 && kill(PID,SIGKILL) == -1){
perror("smash error: kill failed");
return;
}
if(smash.getCurrentProcess() == PID){
smash.removeJob(JID);
smash.setCurrentProcess(-1);
smash.setCurrentCommand("");
}
std::cout<<"smash: "<<smash.getTimedJobCmd(toKill)<<" timed out!"<<std::endl;
smash.popTimedJobsList();
}else{
break;
}
}
time_t newDuration = (smash.TimedJobsNum()!=0) ? (smash.getTimedListHead().getFinisingTime() - time(nullptr)) : 0;
if(newDuration != 0){
if(alarm(newDuration) == -1){
perror("smash error: alarm failed");
}
}
}