-
Notifications
You must be signed in to change notification settings - Fork 0
/
client_bonus.c
72 lines (64 loc) · 1.74 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* client_bonus.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fvieira <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/12/13 12:13:15 by fvieira #+# #+# */
/* Updated: 2022/12/13 17:27:08 by fvieira ### ########.fr */
/* */
/* ************************************************************************** */
#include "minitalk.h"
void sendsignal(char *str, pid_t p)
{
int count;
unsigned char c;
int bit;
int lenstr;
count = 0;
lenstr = ft_strlen(str);
while (count <= lenstr)
{
c = (unsigned char) str[count];
bit = 0;
while (bit < 8)
{
if (c & 0b00000001)
kill(p, SIGUSR1);
else
kill(p, SIGUSR2);
c >>= 1;
bit++;
usleep(30);
}
count++;
}
}
void handler(int signal)
{
if (signal == SIGUSR1)
ft_printf("Message received!");
}
void signaltreatment(void)
{
struct sigaction action;
action.sa_handler = &handler;
action.sa_flags = SA_SIGINFO;
if (sigaction(SIGUSR1, &action, NULL) == -1)
exit(1);
if (sigaction(SIGUSR2, &action, NULL) == -1)
exit(1);
}
int main(int argc, char **argv)
{
pid_t p;
if (argc != 3)
return (0);
p = ft_atoi(argv[1]);
signaltreatment();
sendsignal(argv[2], p);
while (1)
pause();
return (0);
}