-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.c
71 lines (63 loc) · 1.85 KB
/
client.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* client.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ansebast <ansebast@student.42luanda.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/09/04 13:59:32 by ansebast #+# #+# */
/* Updated: 2024/11/21 23:18:57 by ansebast ### ########.fr */
/* */
/* ************************************************************************** */
#include "minitalk.h"
int g_signal_received;
void wait_signal(int signal)
{
if (signal == 10)
g_signal_received = 1;
if (signal == 12)
ft_puterror("There is another process sending message. Wait 1 second!",
2);
}
static void send_signal(long pid, unsigned char bit)
{
if (bit == '0')
kill(pid, SIGUSR1);
else if (bit == '1')
kill(pid, SIGUSR2);
while (g_signal_received == 0)
;
g_signal_received = 0;
}
void send_bits(int count, int ch, long pid, void (f)(long pid,
unsigned char bit))
{
unsigned char bit;
while (count--)
{
bit = ((ch >> count & 1) + '0');
f(pid, bit);
}
}
int main(int ac, char **av)
{
long pid;
int i;
int j;
validate_client_args(ac, av);
pid = ft_atol(av[1]);
i = 0;
j = 7;
signal(SIGUSR1, wait_signal);
signal(SIGUSR2, wait_signal);
while (av[2][i] != '\0')
{
send_bits(j, av[2][i], pid, send_signal);
j = 7;
i++;
}
send_bits(j, '\n', pid, send_signal);
send_bits(j, av[2][i], pid, send_signal);
exit(0);
return (0);
}