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..
You have a misunderstanding in "In main, in case of a signal, the ft_btoa handler function is launched WITHOUT ARGUMENTS."; it is not.
This
signal(SIGUSR1, ft_btoa);is not a call, not toft_btoa()to be precise.It is a call to
signal()giving the function namedft_btoaas a parameter.The parameter means "at the appropriate time, call this function and give it the parameter which at that time makes sense".
For this to work,
ft_btoahas to match the prototype expected by what is behindsignal(), including accepting a parameter which matches what at the time of callingft_btoa()(notsignal()) needs to be carried as a parameter.The call to
ft_btoa(int)happens later and DOES give a parameter.Judging from your comment, you are aware of the relevant context: