-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.c
66 lines (59 loc) · 1.87 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* client.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rlucio-l <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/01/19 20:47:44 by rlucio-l #+# #+# */
/* Updated: 2022/02/11 15:08:20 by rlucio-l ### ########.fr */
/* */
/* ************************************************************************** */
#include <minitalk.h>
static volatile sig_atomic_t g_got_confirmation = 0;
static void signal_handler(int sig)
{
if (sig == SIGUSR1)
g_got_confirmation = 1;
}
static void send_string_bit_by_bit(char *str, pid_t pid)
{
int bit;
while (*str)
{
bit = 8;
while (bit--)
{
if ((*str >> bit) & 1)
{
if (kill(pid, SIGUSR1) == -1)
ft_error_exit("The kill() system call have failed");
}
else if (*str)
{
if (kill(pid, SIGUSR2) == -1)
ft_error_exit("The kill() system call have failed");
}
while (!g_got_confirmation)
pause();
g_got_confirmation = 0;
}
str++;
}
}
int main(int argc, char *argv[])
{
pid_t pid;
char *str;
struct sigaction sa;
if (argc != 3)
ft_error_exit("Usage: ./client server-PID string-to-send");
pid = ft_atoi(argv[1]);
str = argv[2];
sa.sa_flags = 0;
sa.sa_handler = signal_handler;
if (sigaction(SIGUSR1, &sa, NULL) == -1)
ft_error_exit("Bad address or Invalid argument");
send_string_bit_by_bit(str, pid);
return (0);
}