-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.c
66 lines (59 loc) · 2.08 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
64
65
66
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* server.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dnishsha <[email protected] +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/07/19 14:56:07 by dnishsha #+# #+# */
/* Updated: 2023/07/19 14:56:09 by dnishsha ### ########.fr */
/* */
/* ************************************************************************** */
#include "minitalk.h"
// Global struct to hold bit and current_byte values
t_signal g_signal;
// Convert bytes to char
static void btoc(int sig, siginfo_t *info, void *other)
{
pid_t client_pid;
(void) other;
client_pid = info->si_pid;
if (sig == SIGUSR1)
g_signal.current_byte = g_signal.current_byte | (1 << g_signal.bit);
g_signal.bit --;
if (g_signal.bit == -1)
{
if (g_signal.current_byte == '\0')
ft_printf("\n");
else
write(1, &(g_signal.current_byte), 1);
g_signal.current_byte = 0;
g_signal.bit = 7;
}
if (kill (client_pid, SIGUSR1) == -1)
print_error("ERROR IN SENDING SIGNAL");
}
/*
Alternative Approach
signal(SIGUSR1, btoc);
signal(SIGUSR2, btoc);
sigemptyset() — Initialize a signal mask to exclude all signals
*/
int main(void)
{
int pid;
struct sigaction sa;
pid = getpid();
g_signal.current_byte = 0;
g_signal.bit = 7;
ft_printf("PID: %d\n", pid);
sigemptyset(&sa.sa_mask);
sa.sa_flags = SA_SIGINFO;
sa.sa_sigaction = btoc;
if ((sigaction(SIGUSR1, &sa, NULL) == -1)
|| (sigaction(SIGUSR2, &sa, NULL) == -1))
print_error("ERROR IN SETTING UP SIGNAL HANDLER");
while (1)
pause();
return (0);
}