-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.c
64 lines (57 loc) · 914 Bytes
/
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
#include <unistd.h>
#include <stdlib.h>
#include <signal.h>
#include <stdio.h>
#include <string.h>
#include "libft/libft.h"
int g_signal = 0;
static void receive_signal(int signal)
{
g_signal = 1;
}
static int ft_error(const char *msg)
{
ft_printf("%s\n", msg);
return (1);
}
static void send_char(int pid, char c)
{
int bit;
int i;
i = 0;
while (i < 8)
{
bit = (c >> i) & 1;
if (bit)
kill(pid, SIGUSR1);
else
kill(pid, SIGUSR2);
while (g_signal != 1)
;
g_signal = 0;
i++;
}
}
int main(int argc, char **argv)
{
int i;
int pid;
int len;
char *message;
if (argc != 3)
return (ft_error("Format incorrect: ./client <PID> <msg>"));
i = 0;
pid = ft_atoi(argv[1]);
if (pid < 0)
return (0);
message = argv[2];
len = ft_strlen(message);
signal(SIGUSR1, receive_signal);
while (i < len)
{
send_char(pid, message[i]);
i++;
}
send_char(pid, '\0');
return (0);
}