-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver_bonus.c
83 lines (68 loc) · 2.55 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
76
77
78
79
80
81
82
83
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* server_bonus.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hecmarti <hecmarti@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/13 15:20:00 by hecmarti #+# #+# */
/* Updated: 2024/05/20 16:21:43 by hecmarti ### ########.fr */
/* */
/* ************************************************************************** */
#include "minitalk.h"
// Handles SIGUSR1 and SIGUSR2 signals by
// shifting and ORing the bits of a character,
// and sends a confirmation signal back to the sender
// Counter for the number of bits received
// Character being built from the received bits
// Unused parameter
// Shift the character left and set the least
// significant bit to 1 if the signal is SIGUSR1;
// otherwise, shift the character left and set the
// least significant bit to 0
// If 8 bits have been received, print the character and
// reset the counter and character, and send a SIGUSR1
// signal back to the sender as a confirmation
// si_pid is the process ID of the sender
// Send a SIGUSR2 signal back to the sender as a confirmation
// si_pid is the process ID of the sender
void handler(int sig, siginfo_t *info, void *content)
{
static int i = 0;
static unsigned char c = 0;
(void)content;
if (sig == SIGUSR2)
c = c << 1;
else if (sig == SIGUSR1)
c = (c << 1) | 0b00000001;
i++;
if (i == 8)
{
ft_printf("%c", c);
i = 0;
c = 0;
if (kill(info->si_pid, SIGUSR1) == -1)
ft_putstr_fd("Unable to send SIGUSR1\n", 2);
return ;
}
if (kill(info->si_pid, SIGUSR2) == -1)
ft_putstr_fd("Unable to send SIGUSR2\n", 2);
}
// Print the process ID
// Set up the signal handler for SIGUSR1 and
// SIGUSR2 and wait for signals indefinitely
int main(void)
{
struct sigaction sa_sig;
ft_printf("PID: %d\n", getpid());
while (1)
{
sa_sig.sa_sigaction = &handler;
sa_sig.sa_flags = SA_SIGINFO;
if (sigaction(SIGUSR1, &sa_sig, NULL) == -1)
ft_putstr_fd("Unable to send SIGUSR1\n", 2);
if (sigaction(SIGUSR2, &sa_sig, NULL) == -1)
ft_putstr_fd("Unable to send SIGUSR2\n", 2);
}
return (0);
}