-
Notifications
You must be signed in to change notification settings - Fork 0
/
server_bonus.c
66 lines (59 loc) · 1.65 KB
/
server_bonus.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* server_bonus.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: atangil <[email protected]. +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/09/18 12:20:27 by atangil #+# #+# */
/* Updated: 2023/09/18 15:34:54 by atangil ### ########.fr */
/* */
/* ************************************************************************** */
#include <signal.h>
#include <unistd.h>
void printpid(int pid)
{
if (pid >= 10)
printpid(pid / 10);
write(1, &"0123456789"[pid % 10], 1);
}
void ft_printpid(int pid)
{
write(1, "PID : ", 6);
printpid(pid);
write(1, "\n", 1);
}
void get_bit(int sig, siginfo_t *info, void *context)
{
static int i = 0;
static char c = 0;
(void)(context);
if (sig == SIGUSR1)
c += 1;
i++;
if (i == 8)
{
if (c == '\0')
kill(info->si_pid, SIGUSR1);
else
write(1, &c, 1);
c = 0;
i = 0;
}
else
c = c << 1;
}
int main(void)
{
struct sigaction sig;
sig.sa_sigaction = get_bit;
sig.sa_flags = SA_SIGINFO;
ft_printpid(getpid());
if (sigaction(SIGUSR1, &sig, NULL) == -1)
return (-1);
if (sigaction(SIGUSR2, &sig, NULL) == -1)
return (-1);
while (1)
pause();
return (0);
}