-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.c
57 lines (52 loc) · 1.7 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* server.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: adantas- <adantas-@student.42sp.org.br> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/01/19 22:41:51 by adantas- #+# #+# */
/* Updated: 2023/01/23 15:57:15 by adantas- ### ########.fr */
/* */
/* ************************************************************************** */
#include "minitalk.h"
#include "libft/ft_printf.h"
void which_signal(int sig, siginfo_t *siginfo, void *context)
{
static unsigned char letter = 0;
static int bit = 0;
(void)context;
if (sig == SIGUSR1)
letter |= 1;
bit++;
if (bit == 8)
{
write(1, &letter, 1);
letter = 0;
bit = 0;
}
letter <<= 1;
if (sig == SIGUSR1)
kill(siginfo->si_pid, SIGUSR1);
else
kill(siginfo->si_pid, SIGUSR2);
}
int main(void)
{
int pid;
struct sigaction s_sa;
sigset_t sigset;
sigemptyset(&sigset);
sigaddset(&sigset, SIGUSR1);
sigaddset(&sigset, SIGUSR2);
s_sa.sa_handler = NULL;
s_sa.sa_mask = sigset;
s_sa.sa_flags = SA_SIGINFO;
s_sa.sa_sigaction = which_signal;
sigaction(SIGUSR1, &s_sa, NULL);
sigaction(SIGUSR2, &s_sa, NULL);
pid = getpid();
ft_printf("Waiting input for pid: %d\n", pid);
while (1)
pause();
}