-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient_bonus.c
More file actions
67 lines (61 loc) · 1.69 KB
/
Copy pathclient_bonus.c
File metadata and controls
67 lines (61 loc) · 1.69 KB
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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* client_bonus.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jaiane <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/15 15:15:14 by jaiane #+# #+# */
/* Updated: 2025/04/15 17:42:42 by jaiane ### ########.fr */
/* */
/* ************************************************************************** */
#include "minitalk.h"
void send_signal(int pid, unsigned char c)
{
int i;
unsigned char temp_c;
i = 8;
temp_c = c;
while (i > 0)
{
i--;
temp_c = c >> i;
if (temp_c % 2 == 0)
kill(pid, SIGUSR2);
else
kill(pid, SIGUSR1);
usleep(600);
}
}
void handle_read_receipt(int sig)
{
if (sig == SIGUSR1)
write(1, "[1]", 3);
if (sig == SIGUSR2)
write(1, "[0]", 3);
}
int main(int argc, char *argv[])
{
pid_t server_pid;
char *message;
int i;
signal(SIGUSR1, handle_read_receipt);
signal(SIGUSR2, handle_read_receipt);
if (argc != 3)
{
ft_putstr("Usage: ");
ft_putstr(argv[0]);
ft_putstr(" <pid> <message>\n");
exit(0);
}
server_pid = atoi(argv[1]);
message = argv[2];
i = 0;
while (message[i])
{
send_signal(server_pid, message[i]);
i++;
}
send_signal(server_pid, '\0');
return (0);
}