-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
42 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
#include "minitalk.h" | ||
|
||
static void handle_signal(int sig, siginfo_t *info, void *context); | ||
|
||
int main(void) | ||
{ | ||
struct sigaction action; | ||
|
||
ft_putstr_fd("Server PID: ", 1); | ||
ft_putnbr_fd(getpid(), 1); | ||
ft_putchar_fd('\n', 1); | ||
action.sa_sigaction = handle_signal; | ||
action.sa_flags = SA_SIGINFO; | ||
sigaction(SIGUSR1, &action, 0); | ||
sigaction(SIGUSR2, &action, 0); | ||
while (1) | ||
pause(); | ||
return (0); | ||
} | ||
|
||
static void handle_signal(int sig, siginfo_t *info, void *context) | ||
{ | ||
static int bit_count; | ||
static unsigned char c; | ||
|
||
(void)context; | ||
c |= (sig == SIGUSR2); | ||
if (++bit_count == 8) | ||
{ | ||
bit_count = 0; | ||
if (!c) | ||
{ | ||
kill(info->si_pid, SIGUSR2); | ||
info->si_pid = 0; | ||
return ; | ||
} | ||
ft_putchar_fd(c, 1); | ||
c = 0; | ||
} | ||
else | ||
c <<= 1; | ||
} |