-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.bonus.c
75 lines (70 loc) · 1.97 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
66
67
68
69
70
71
72
73
74
75
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* server.bonus.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: Asandy <abasifrekesandy@gmail.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/08/14 17:33:04 by Asandy #+# #+# */
/* Updated: 2022/08/14 17:33:04 by Asandy ### ########.fr */
/* */
/* ************************************************************************** */
#include <unistd.h>
#include <ctype.h>
#include <stdio.h>
#include <sys/types.h>
#include <signal.h>
#include "libft/libft.h"
void reciever(int sig, siginfo_t *siginfo, void *context)
{
static int i;
static char c;
(void)context;
if (sig == SIGUSR1)
{
c = (c << 1) | 1;
}
else if (sig == SIGUSR2)
{
c = (c << 1) | 0;
}
i++;
if (i == 8)
{
ft_putchar_fd(c, 1);
if (c == '\0')
{
usleep(300);
kill(siginfo->si_pid, SIGUSR1);
}
i = 0;
c = 0;
}
}
int main(int argc, char **argv)
{
int pid;
struct sigaction sa1;
struct sigaction sa2;
(void)argv;
if (argc == 1)
{
pid = getpid();
ft_putstr_fd("PID: ", 1);
ft_putnbr_fd(pid, 1);
ft_putchar_fd('\n', 1);
sa1.sa_flags = SA_SIGINFO;
sa1.sa_sigaction = reciever;
sigemptyset(&sa1.sa_mask);
sa2.sa_flags = SA_SIGINFO;
sa2.sa_sigaction = reciever;
sigemptyset(&sa2.sa_mask);
sigaction(SIGUSR1, &sa1, NULL);
sigaction(SIGUSR2, &sa2, NULL);
while (1)
pause();
}
else
ft_putendl_fd("Wrong number of arguments : ./server", 2);
return (0);
}