-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhandler.c
More file actions
52 lines (49 loc) · 1.48 KB
/
handler.c
File metadata and controls
52 lines (49 loc) · 1.48 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
#include"headers.h"
void handler(int sig){
pid_t pid;
char *exit_status = (char *)calloc(1000, sizeof(char));
char *exit = (char *)calloc(1000, sizeof(char));
int stat;
pid = waitpid(0, &stat, WNOHANG); //WNOHANG means parent does not wait if child does not terminate just check and return waitpid()
sprintf(exit, "\nProcess with pid %d exited ", pid);
if(WIFEXITED(stat)){
delete_job(pid);
sprintf(exit_status, "normally\n");
}
else if(WIFEXITED(stat) == 0){
sprintf(exit_status, "abnormally\n");
}
else if(WIFSIGNALED(stat)){
psignal(WTERMSIG(stat), "Exit signal");
}
if(pid > 0){
write(2, exit, strlen(exit));
write(2, exit_status, strlen(exit_status));
}
free(exit);
return;
}
void signal_handler(int signum){
if(job_no > 0)
czz = 1;
if(signum == SIGTSTP && foreground == 1 && czz == 1){
if(command1 != '\0'){
printf("Pushing it in background");
sleep(0);
printf("\n");
siglongjmp(cz_buf, 1);
}
else{
printf("Hello\n");
kill(job[job_no - 1].id, 9);
delete_job(job[job_no - 1].id);
siglongjmp(cz_buf, 1);
}
}
}
void shandler(int signum){
if(signum == SIGUSR1 || signum == SIGKILL || signum == SIGSTOP || signum == SIGINT || signum == SIGTSTP || signum == SIGCHLD)
sleep(0);
if(signum != SIGCHLD)
siglongjmp(cz_buf, 1);
}