-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.c
More file actions
57 lines (51 loc) · 1.63 KB
/
server.c
File metadata and controls
57 lines (51 loc) · 1.63 KB
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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* server.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: llatrice <llatrice@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/04/23 17:02:45 by llatrice #+# #+# */
/* Updated: 2022/06/11 13:43:01 by llatrice ### ########.fr */
/* */
/* ************************************************************************** */
#include "minitalk.h"
t_data g_data;
void reset_data(void)
{
g_data.i = 0;
g_data.x = 0;
g_data.client_pid = 0;
}
void handler(int sig, siginfo_t *info, void *ucontext)
{
(void)ucontext;
sig -= SIGUSR1;
if (g_data.client_pid != info->si_pid)
reset_data();
g_data.x = g_data.x << 1 | sig;
g_data.i++;
if (g_data.i == 8)
{
write(1, &g_data.x, 1);
reset_data();
}
g_data.client_pid = info->si_pid;
}
int main(void)
{
struct sigaction sa;
reset_data();
ft_putstr("pid : ");
ft_putnbr(getpid());
ft_putstr("\n");
sa.sa_sigaction = &handler;
sa.sa_flags = SA_SIGINFO;
sigemptyset(&sa.sa_mask);
sigaddset(&sa.sa_mask, SIGUSR1);
sigaddset(&sa.sa_mask, SIGUSR2);
sigaction(SIGUSR1, &sa, 0);
sigaction(SIGUSR2, &sa, 0);
while (1)
sleep(1);
}