-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.c
70 lines (63 loc) · 1.68 KB
/
server.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
/* ************************************************************************** */
/* */
/* :::::::: */
/* server.c :+: :+: */
/* +:+ */
/* By: ksmorozo <[email protected]> +#+ */
/* +#+ */
/* Created: 2021/09/20 12:15:03 by ksmorozo #+# #+# */
/* Updated: 2021/11/07 14:59:12 by ksmorozo ######## odam.nl */
/* */
/* ************************************************************************** */
#include <unistd.h>
#include "libft/libft.h"
#include <signal.h>
#include <stdlib.h>
void display_binary_to_dec(int binary)
{
int decimal;
int base;
int remainder;
base = 1;
decimal = 0;
remainder = 0;
while (binary)
{
remainder = binary % 10;
binary /= 10;
decimal += remainder * base;
base *= 2;
}
ft_putchar_fd(decimal, 1);
}
void interpret(int signal)
{
static char message[8];
static int i;
if (signal == SIGUSR1)
message[i] = '0';
else
message[i] = '1';
i++;
if (i == 8)
{
display_binary_to_dec(ft_atoi(message));
i = 0;
ft_bzero(message, 8);
}
}
int main(void)
{
int pid;
pid = getpid();
if (pid < 0)
return (-1);
ft_putnbr_fd(pid, 0);
ft_putchar_fd('\n', 0);
signal(SIGUSR1, interpret);
signal(SIGUSR2, interpret);
while ("The prophecy is true")
{
usleep(300);
}
}