-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.c
54 lines (48 loc) · 1.64 KB
/
server.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* server.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: smagalha <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/04/08 20:57:33 by smagalha #+# #+# */
/* Updated: 2023/04/08 22:45:36 by smagalha ### ########.fr */
/* */
/* ************************************************************************** */
#include "minitalk.h"
static void handle_signal(int sig, siginfo_t *info, void *context);
int main(void)
{
struct sigaction action;
ft_putstr_fd("Server PID: ", 1);
ft_putnbr_fd(getpid(), 1);
ft_putchar_fd('\n', 1);
action.sa_sigaction = handle_signal;
action.sa_flags = SA_SIGINFO;
sigaction(SIGUSR1, &action, 0);
sigaction(SIGUSR2, &action, 0);
while (1)
pause();
return (0);
}
static void handle_signal(int sig, siginfo_t *info, void *context)
{
static int bit_count;
static unsigned char c;
(void)context;
c |= (sig == SIGUSR2);
if (++bit_count == 8)
{
bit_count = 0;
if (!c)
{
kill(info->si_pid, SIGUSR2);
info->si_pid = 0;
return ;
}
ft_putchar_fd(c, 1);
c = 0;
}
else
c <<= 1;
}