-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient_bonus.c
84 lines (74 loc) · 1.73 KB
/
client_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
76
77
78
79
80
81
82
83
84
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* client_bonus.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: abdel-ou <abdel-ou@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/12/06 13:04:03 by abdel-ou #+# #+# */
/* Updated: 2022/12/25 15:52:43 by abdel-ou ### ########.fr */
/* */
/* ************************************************************************** */
#include "minitalk_bonus.h"
int g_ll;
void cov(int nb, char *bit)
{
int i;
i = 0;
if (nb < 2)
{
while (i < 7)
{
bit[i] = bit[i + 1];
i++;
}
bit[7] = nb + 48;
}
else
{
cov(nb / 2, bit);
cov(nb % 2, bit);
}
}
void char_send(char *pid, int nb)
{
int i;
char *bit;
i = 0;
bit = ft_strdup("00000000");
cov(nb, bit);
while (i < 8)
{
if (bit[i] == '0')
kill(atoi(pid), SIGUSR1);
if (bit[i] == '1')
kill(atoi(pid), SIGUSR2);
i++;
usleep(300);
}
free(bit);
}
void message_received(int sig)
{
static int l;
(void)sig;
l++;
if (l == g_ll)
{
ft_printf("the signal has been received successfully \n");
}
}
int main(int argc, char **argv)
{
int i;
(void)argc;
i = 0;
g_ll = ft_strlen(argv[2]);
signal(SIGUSR1, message_received);
while (argv[2][i])
{
char_send(argv[1], (unsigned char)argv[2][i]);
i++;
}
return (0);
}