-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient_bonus.c
66 lines (60 loc) · 1.72 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* client_bonus.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jilai <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/28 15:11:39 by jilai #+# #+# */
/* Updated: 2024/04/28 15:11:41 by jilai ### ########.fr */
/* */
/* ************************************************************************** */
#include "client_bonus.h"
void handle_signal(int sig, siginfo_t *i, void *context)
{
(void)sig;
(void)i;
(void)context;
if (sig == SIGUSR1)
ft_printf("String received by PID: %i\n", i->si_pid);
exit(EXIT_SUCCESS);
}
void send_char_bits(int pid, unsigned char ch)
{
int i;
unsigned char tmp;
i = 8;
tmp = ch;
while (i > 0)
{
i--;
tmp = ch >> i;
if (tmp % 2 == 0)
kill(pid, SIGUSR2);
else
kill(pid, SIGUSR1);
usleep(42);
}
}
int main(int argc, char *argv[])
{
int pid;
int i;
struct sigaction s;
if (argc != 3)
exit(EXIT_FAILURE);
pid = atoi(argv[1]);
i = 0;
s.sa_sigaction = handle_signal;
s.sa_flags = SA_SIGINFO;
sigaction(SIGUSR1, &s, NULL);
while (argv[2][i])
{
send_char_bits(pid, (unsigned char)(argv[2][i]));
i++;
}
send_char_bits(pid, (unsigned char)('\0'));
while (1)
pause();
return (0);
}