-
Notifications
You must be signed in to change notification settings - Fork 0
/
server_bonus.c
65 lines (58 loc) · 1.79 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
57
58
59
60
61
62
63
64
65
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* server_bonus.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ialdidi <ialdidi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/21 14:43:10 by ialdidi #+# #+# */
/* Updated: 2024/04/18 16:50:03 by ialdidi ### ########.fr */
/* */
/* ************************************************************************** */
#include "minitalk_bonus.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_sigaction = &receive_byte;
sa.sa_flags = SA_SIGINFO;
sigaction(SIGUSR1, &sa, NULL);
sigaction(SIGUSR2, &sa, NULL);
while (1)
pause();
return (0);
}
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 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)
{
if (!c)
kill(info->si_pid, SIGUSR2);
write(1, &c, 1);
counter = 0;
c = 0;
}
}