-
Notifications
You must be signed in to change notification settings - Fork 0
/
signal_handling.c
80 lines (75 loc) · 1.99 KB
/
signal_handling.c
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
#include <fcntl.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <termios.h>
#include "init_shell.h"
#include "main.h"
#include "nightswatch.h"
#include "system_commands.h"
// function to handle Ctrl-C
void sigintHandlerC(int sig_num) {
pid_t process = processpid;
if (process > 0) {
kill(process, sig_num);
// signal(SIGTSTP, sigintHandlerZ);
printf("\n");
// // init_shell();
fflush(stdout);
} else {
printf("\n");
init_shell();
fflush(stdout);
}
}
void sigintHandlerZ(int sig_num) {
pid_t process = processpid;
// kill(process, SIGTTIN);
if (process > 0) {
kill(process, sig_num);
// signal(SIGTSTP, sigintHandlerZ);
printf("\n");
// init_shell();
fflush(stdout);
background_pids[no_of_backgroundprocess] = process;
stopped_pids[no_of_backgroundprocess] = process;
char *file = (char *)malloc(sizeof(char) * 100);
char st[1000] = "/proc/";
char b[] = "/stat";
char *snum = (char *)malloc(sizeof(char) * 200);
sprintf(snum, "%d", process);
strcat(file, st);
strcat(file, snum);
strcat(file, b);
int fd = open(file, O_RDONLY);
if (fd == -1) {
perror("Ctrl-Z");
} else {
char *buff = (char *)calloc(300, sizeof(char));
read(fd, buff, 300);
char **parts = (char **)malloc(100 * sizeof(char *));
int i = 0;
while (i < 50) {
parts[i] = strsep(&buff, " ");
++i;
}
close(fd);
background_process[no_of_backgroundprocess] = parts[1];
no_of_backgroundprocess++;
}
} else {
printf("\n");
init_shell();
fflush(stdout);
}
}
void signal_handling() {
char c = getchar();
while (1) {
if (c == 'q') {
break;
}
}
}