-
Notifications
You must be signed in to change notification settings - Fork 3
/
client.c
72 lines (66 loc) · 1.73 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
72
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* client.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hbaddrul <hbaddrul@student.42kl.edu.my> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/08/08 17:58:19 by hbaddrul #+# #+# */
/* Updated: 2021/11/24 16:06:30 by hbaddrul ### ########.fr */
/* */
/* ************************************************************************** */
#include <signal.h>
#include <stdlib.h>
#include <unistd.h>
#include "libft/libft.h"
static void action(int sig)
{
static int received = 0;
if (sig == SIGUSR1)
++received;
else
{
ft_putnbr_fd(received, 1);
ft_putchar_fd('\n', 1);
exit(0);
}
}
static void mt_kill(int pid, char *str)
{
int i;
char c;
while (*str)
{
i = 8;
c = *str++;
while (i--)
{
if (c >> i & 1)
kill(pid, SIGUSR2);
else
kill(pid, SIGUSR1);
usleep(100);
}
}
i = 8;
while (i--)
{
kill(pid, SIGUSR1);
usleep(100);
}
}
int main(int argc, char **argv)
{
if (argc != 3 || !ft_strlen(argv[2]))
return (1);
ft_putstr_fd("Sent : ", 1);
ft_putnbr_fd(ft_strlen(argv[2]), 1);
ft_putchar_fd('\n', 1);
ft_putstr_fd("Received: ", 1);
signal(SIGUSR1, action);
signal(SIGUSR2, action);
mt_kill(ft_atoi(argv[1]), argv[2]);
while (1)
pause();
return (0);
}