-
Notifications
You must be signed in to change notification settings - Fork 3
/
server.c
58 lines (53 loc) · 1.66 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* server.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hbaddrul <hbaddrul@student.42kl.edu.my> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/08/08 12:20:54 by hbaddrul #+# #+# */
/* Updated: 2021/11/24 16:06:35 by hbaddrul ### ########.fr */
/* */
/* ************************************************************************** */
#include <signal.h>
#include <unistd.h>
#include "libft/libft.h"
static void action(int sig, siginfo_t *info, void *context)
{
static int i = 0;
static pid_t client_pid = 0;
static unsigned char c = 0;
(void)context;
if (!client_pid)
client_pid = info->si_pid;
c |= (sig == SIGUSR2);
if (++i == 8)
{
i = 0;
if (!c)
{
kill(client_pid, SIGUSR2);
client_pid = 0;
return ;
}
ft_putchar_fd(c, 1);
c = 0;
kill(client_pid, SIGUSR1);
}
else
c <<= 1;
}
int main(void)
{
struct sigaction s_sigaction;
ft_putstr_fd("Server PID: ", 1);
ft_putnbr_fd(getpid(), 1);
ft_putchar_fd('\n', 1);
s_sigaction.sa_sigaction = action;
s_sigaction.sa_flags = SA_SIGINFO;
sigaction(SIGUSR1, &s_sigaction, 0);
sigaction(SIGUSR2, &s_sigaction, 0);
while (1)
pause();
return (0);
}