-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.c
60 lines (55 loc) · 1.71 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* server.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: juhtoo-h <juhtoo-h@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/29 11:10:41 by juhtoo-h #+# #+# */
/* Updated: 2024/11/01 15:22:45 by juhtoo-h ### ########.fr */
/* */
/* ************************************************************************** */
#include "minitalk.h"
void ft_signal_handler(int sig, siginfo_t *info, void *context)
{
static char letter;
static int bit;
(void)context;
if (bit == 0)
bit++;
if (sig == SIGUSR1)
letter += 0;
else if (sig == SIGUSR2)
letter += bit;
bit <<= 1;
if (bit == 256)
{
bit = 1;
if (letter == 0)
if (kill(info->si_pid, SIGUSR2) == -1)
ft_error_handler(0);
if (letter != 0)
write(1, &letter, 1);
letter = 0;
}
if (kill(info->si_pid, SIGUSR1) == -1)
ft_error_handler(0);
}
int main(void)
{
struct sigaction action;
action.sa_flags = SA_SIGINFO;
action.sa_sigaction = ft_signal_handler;
if (sigaction(SIGUSR1, &action, NULL) == -1
|| sigaction(SIGUSR2, &action, NULL) == -1)
{
ft_error_handler(1);
return (1);
}
write(1, "PID = ", 6);
ft_putnbr(getpid());
write(1, "\n", 1);
while (1)
pause();
return (0);
}