-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathneonate.c
72 lines (61 loc) · 1.32 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include "headers.h"
// struct termios orig_termios;
void enableRawMode()
{
struct termios raw;
tcgetattr(STDIN_FILENO, &raw);
raw.c_lflag &= ~(ECHO | ICANON);
tcsetattr(STDIN_FILENO, TCSAFLUSH, &raw);
}
void disableRawMode()
{
struct termios raw;
tcgetattr(STDIN_FILENO, &raw);
raw.c_lflag |= (ECHO | ICANON);
tcsetattr(STDIN_FILENO, TCSAFLUSH, &raw);
}
int kbhit()
{
struct timeval tv = {0L, 0L};
fd_set fds;
FD_ZERO(&fds);
FD_SET(0, &fds);
return select(1, &fds, NULL, NULL, &tv);
}
int neonate(char *command)
{
char *tokens[100];
int number_of_tokens = 0;
char *token = strtok(command, " ");
while (token != NULL)
{
tokens[number_of_tokens++] = strdup(token);
token = strtok(NULL, " ");
}
int time_arg = atoi(tokens[2]);
enableRawMode();
pid_t pid;
while (!kbhit())
{
pid = fork();
if (pid == -1)
{
perror("fork");
return 1;
}
else if (pid == 0)
{
// Child process
printf("%d\n", getpid());
sleep(time_arg);
exit(0);
}
else
{
// Parent process
wait(NULL); // Wait for the child process to complete
}
}
disableRawMode();
return 0;
}