-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_server.c
78 lines (70 loc) · 1.72 KB
/
ft_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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_server.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: yopi <yopi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/01/08 04:17:16 by yopi #+# #+# */
/* Updated: 2022/01/11 03:00:00 by yopi ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_minitalk.h"
int g_t;
int bi_conv(int pos)
{
int base;
int ret;
if (!pos)
return (1);
base = 2;
ret = 2;
while (pos > 1)
{
ret *= base;
pos--;
}
return (ret);
}
void handle(int sig, siginfo_t *data, void *empty)
{
static int pos = 7;
static int sum = 0;
(void)empty;
if (g_t != data->si_pid)
{
sum = 0;
pos = 7;
}
if (sig == SIGUSR1)
{
sum += bi_conv(pos);
pos--;
}
else
pos--;
if (pos == -1)
{
write(1, &sum, 1);
pos = 7;
sum = 0;
}
g_t = data->si_pid;
}
int main(void)
{
pid_t procces;
struct sigaction sig;
write(1, "Server PID: ", 12);
procces = getpid();
ft_putnbr_fd(procces, 1);
write(1, "\n", 1);
sig.sa_sigaction = &handle;
sigemptyset(&sig.sa_mask);
sig.sa_flags = SA_RESTART;
sigaction(SIGUSR1, &sig, NULL);
sigaction(SIGUSR2, &sig, NULL);
while (1)
pause();
return (0);
}