-
Notifications
You must be signed in to change notification settings - Fork 0
/
neonate.c
60 lines (38 loc) · 1.05 KB
/
neonate.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
#include "headers.h"
void die(const char *s) {
perror(s);
exit(1);
}
struct termios orig_termios;
void disableRawMode() {
if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &orig_termios) == -1)
die("tcsetattr");
}
void enableRawMode() {
if (tcgetattr(STDIN_FILENO, &orig_termios) == -1) die("tcgetattr");
atexit(disableRawMode);
struct termios raw = orig_termios;
raw.c_lflag &= ~(ICANON | ECHO);
if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &raw) == -1) die("tcsetattr");
}
void e_neonate(char ic,int n){
setbuf(stdout, NULL);
enableRawMode();
int p=fork();
if(p==0){
while(1){
FILE* f = fopen("/proc/sys/kernel/ns_last_pid","r");
char pid[10];
fgets(pid,10,f);
printf("%s",pid);
sleep(n);}
}
else if(p>0){
char c;
while (read(STDIN_FILENO, &c, 1) == 1 && c != ic) {
continue;
}
kill(p,SIGKILL);
}
disableRawMode();
}