-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshell.cc
More file actions
51 lines (46 loc) · 984 Bytes
/
shell.cc
File metadata and controls
51 lines (46 loc) · 984 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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <cstdio>
#include <unistd.h>
#include "shell.hh"
#include <signal.h>
#include <sys/wait.h>
int yyparse(void);
void Shell::prompt() {
if (isatty(0)) {
printf("myshell>");
fflush(stdout);
}
//fflush(stdout);
}
extern "C" void ctrlC_handle(int sig) {
printf("\n");
Shell::prompt();
fflush(stdout);
}
extern "C" void zombie_handle(int sig) {
int pid;
while ((pid = waitpid(-1, NULL, WNOHANG)) > 0) { //NULL mean any status //WNOHANG is no block
//printf("[%d] exited.\n", pid);
//fflush(stdout);
}
}
int main() {
//2.1 Ctr-C
struct sigaction sa;
sa.sa_handler = ctrlC_handle;
sigemptyset(&sa.sa_mask);
sa.sa_flags = SA_RESTART;
if(sigaction(SIGINT, &sa, NULL)){
perror("sigaction");
exit(2);
}
//2.2 Handle zombie
sa.sa_handler = zombie_handle;
sigemptyset(&sa.sa_mask);
sa.sa_flags = SA_RESTART;
if (sigaction(SIGCHLD, &sa, NULL)) {
perror("sigchild");
exit(2);
}
Shell::prompt();
yyparse();
}