Skip to content

Commit

Permalink
Create server2.c
Browse files Browse the repository at this point in the history
  • Loading branch information
izzypt authored Apr 6, 2023
1 parent e3f18b9 commit 81ade11
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions server2.c
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;
}

0 comments on commit 81ade11

Please sign in to comment.