-
Notifications
You must be signed in to change notification settings - Fork 0
/
server_bonus.c
56 lines (51 loc) · 1.63 KB
/
server_bonus.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* server_bonus.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: asodor <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/02 18:59:55 by asodor #+# #+# */
/* Updated: 2024/05/06 06:13:19 by asodor ### ########.fr */
/* */
/* ************************************************************************** */
#include "minitalk.h"
void handle_signal(int sig, siginfo_t *info, void *context)
{
static char character;
static int bits_received;
(void)context;
bits_received++;
character = character << 1;
if (sig == SIGUSR1)
character = character | 1;
if (bits_received == 8)
{
if (character == 0)
kill(info->si_pid, SIGUSR1);
else
write(1, &character, 1);
bits_received = 0;
character = 0;
}
}
int main(void)
{
struct sigaction sa;
pid_t pid;
pid = getpid();
ft_putstr("Server PID: ");
ft_putnbr((long)pid);
ft_putstr("\n");
sa.sa_sigaction = handle_signal;
sa.sa_flags = SA_SIGINFO;
sigemptyset(&sa.sa_mask);
if (sigaction(SIGUSR1, &sa, NULL) != 0
|| sigaction(SIGUSR2, &sa, NULL) != 0)
return (1);
while (1)
{
pause();
}
return (0);
}