-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.c
63 lines (56 loc) · 1.77 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* server.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ialdidi <ialdidi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/20 10:36:25 by ialdidi #+# #+# */
/* Updated: 2024/04/18 16:48:43 by ialdidi ### ########.fr */
/* */
/* ************************************************************************** */
#include "minitalk.h"
void show_pid(pid_t pid);
void receive_byte(int sig, siginfo_t *info, void *context);
int main(int ac, char **av)
{
struct sigaction sa;
if (ac != 1)
return (ft_putstr("Invalid Format\nUse : ./server"), EXIT_FAILURE);
show_pid(getpid());
sa.sa_flags = SA_SIGINFO;
sa.sa_sigaction = &receive_byte;
sigaction(SIGUSR1, &sa, NULL);
sigaction(SIGUSR2, &sa, NULL);
while (1)
pause();
return (EXIT_SUCCESS);
}
void show_pid(pid_t pid)
{
ft_putstr("Process Id : ");
ft_putnbr(pid);
ft_putchar('\n');
}
void receive_byte(int sig, siginfo_t *info, void *context)
{
static unsigned char c = 0;
static char counter = 0;
static pid_t client_pid = 0;
(void)context;
if (client_pid != info->si_pid)
{
c = 0;
counter = 0;
}
client_pid = info->si_pid;
if (sig == SIGUSR1)
c += 1 << counter;
counter++;
if (counter == 8)
{
write(1, &c, 1);
counter = 0;
c = 0;
}
}