-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.c
More file actions
77 lines (68 loc) · 1.73 KB
/
server.c
File metadata and controls
77 lines (68 loc) · 1.73 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* server.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: achahdan <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/01/04 15:21:59 by achahdan #+# #+# */
/* Updated: 2022/01/06 15:31:48 by achahdan ### ########.fr */
/* */
/* ************************************************************************** */
#include "utls/minitalk.h"
int power_of_two(int pow)
{
int res;
res = 1;
while (pow > 0)
{
res = res * 2;
pow--;
}
return (res);
}
int binary_to_char(int *byte)
{
int i;
int c;
i = 0;
c = 0;
while (i < 8)
{
if (byte[i] == 1)
c += (byte[i] * power_of_two(7 - i));
i++;
}
return (c);
}
void sig_handler(int signal, siginfo_t *info)
{
static int byte[8];
static int count = 0;
int c;
if (signal == SIGUSR1)
byte[count++] = 1;
else if (signal == SIGUSR2)
byte[count++] = 0;
if (count == 8)
{
c = binary_to_char(byte);
ft_printf("%c", c);
kill(info->si_pid, SIGUSR1);
count = 0;
}
}
int main(void)
{
struct sigaction sa;
sa.sa_handler = (void *)sig_handler;
sa.sa_flags = SA_SIGINFO;
ft_printf("PID : %d\n", getpid());
while (1)
{
sigaction(SIGUSR1, &sa, NULL);
sigaction(SIGUSR2, &sa, NULL);
pause();
}
return (0);
}