minitalk/server.c
Willem Haffmans 1c6b660be0 norm :25lines
2024-07-27 18:47:04 +02:00

62 lines
1.7 KiB
C

/* ************************************************************************** */
/* */
/* ::: o_ :::::: ::: */
/* server.c :+: / :+::+: :+: */
/* +:+ > +:++:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#++#++:++#++ */
/* +#+ +#+#+ +#++#+ +#+ \o/ */
/* Created: 2024/07/18 17:24:54 by whaffman #+#+# #+#+# #+# #+# | */
/* Updated: 2024/07/27 18:23:52 by whaffman ### ### ### ### / \ */
/* */
/* ************************************************************************** */
#include <signal.h>
#include <unistd.h>
#include <stdio.h>
#include "libft/libft.h"
void signal_handler(int signum, siginfo_t *info, void *context)
{
static int count = 0;
static char c = '\0';
static pid_t client_pid;
(void) context;
if (client_pid != info->si_pid)
{
count = 0;
c = '\0';
client_pid = info->si_pid;
}
count++;
if (signum == SIGUSR1)
c |= 1 << (8 - count);
if (count == 8)
{
if (c)
write(1, &c, 1);
else
write(1, "\n", 1);
c = '\0';
count = 0;
}
kill(info->si_pid, SIGUSR1);
return ;
}
int main(void)
{
struct sigaction sa;
ft_putstr("MiniTalk Server started on pid: ");
ft_putnbr(getpid());
ft_putstr("\n");
sa.sa_sigaction = signal_handler;
sigemptyset(&sa.sa_mask);
sa.sa_flags = SA_SIGINFO;
sigaction(SIGUSR1, &sa, NULL);
sigaction(SIGUSR2, &sa, NULL);
while (1)
pause();
}