The program is a server that receives messages from the client by transmitting SIGUSR1 or SIGUSR2 signals and decrypts them using bitwise operations (but this is not important yet). The server shows the PID and then waits for a SIGUSR1 or SIGUSR2 signal.
- The question is simple: In main, in case of a signal, the ft_btoa handler function is launched WITHOUT ARGUMENTS. Then how does function "know" that the variable "sig" in the description of the ft_btoa function is a number of signal?
- Question - in what cases it is allowed to use signal, but not sigaction?
void ft_btoa(int sig)
{
static int bit;
static int i;
if (sig == SIGUSR1)
i |= (0x01 << bit);
bit++;
if (bit == 8)
{
ft_printf("%c", i);
bit = 0;
i = 0;
}
}
int main(int argc, char **argv)
{
int pid;
(void)argv;
if (argc != 1)
{
ft_printf("Error\n");
return (1);
}
pid = getpid();
ft_printf("%d\n", pid);
while (argc == 1)
{
signal(SIGUSR1, ft_btoa);
signal(SIGUSR2, ft_btoa);
pause ();
}
return (0);
}
I'm trying to understand how it works..