-
Notifications
You must be signed in to change notification settings - Fork 0
/
server_bonus.c
75 lines (68 loc) · 1.75 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
67
68
69
70
71
72
73
74
75
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* server.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ikgonzal <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/11/26 17:11:52 by ikgonzal #+# #+# */
/* Updated: 2021/12/01 12:57:42 by ikgonzal ### ########.fr */
/* */
/* ************************************************************************** */
#include "minitalk_bonus.h"
int ft_bin_to_dec(char *str)
{
int i;
int result;
int aux;
i = 8;
aux = 1;
result = 0;
while (i-- >= 0)
{
if (str[i] == '1')
result += 1 * aux;
aux *= 2;
}
return (result);
}
void ft_signal_handler(int signum, siginfo_t *info, void *context )
{
static int count;
static char result[8];
char c;
context = NULL;
if (signum == SIGUSR1)
{
result[count] = '1';
usleep(50);
}
else
{
result[count] = '0';
usleep(50);
}
count++;
if (count == 8)
{
c = ft_bin_to_dec(result);
ft_printf("%c", c);
count = 0;
}
kill(info->si_pid, SIGUSR1);
}
int main(void)
{
pid_t pid;
struct sigaction val;
val.sa_mask = 0;
val.sa_flags = 0;
val.sa_sigaction = ft_signal_handler;
pid = getpid();
ft_printf("Proceso nº %d\n", pid);
sigaction(SIGUSR1, &val, NULL);
sigaction(SIGUSR2, &val, NULL);
while (1)
pause();
return (1);
}