Search code examples
csignals

My Program keeps crashing when it gets a SIGUSR1 signal to continue the program from pause()


This is the source code I am working on. I am trying to use signals for string transfer. I don't want to add wait times, so I used the pause() function. When the client sends a signal, it pauses and waits for the server to complete handling it. However, the client doesn't continue when feedback comes and the program crashes. The program crashes with exit code 138. Did I make a mistake about the pause() or this is a different problem?


#define _DEFAULT_SOURCE
#define _XOPEN_SOURCE 700
#define WAIT_TIME 100
#include "libft.h"
#include "ft_printf.h"
#include <unistd.h>
#include <signal.h>

void    send_signal(int pid, char *str)
{
    int i;

    while (*str)
    {
        i = (sizeof(char) * 8) - 1;
        while (i >= 0)
        {
            if (*str & (1 << i))
                kill(pid, SIGUSR1);
            else
                kill(pid, SIGUSR2);
            pause();
            //ft_printf("worked");
            i--;
        }
        str++;
    }
}

int main(int argc, char **argv)
{
    int     i;
    int     pid;

    i = 0;
    if (argc != 3)
    {
        ft_printf("Error: Wrong number of arguments.");
        exit(EXIT_FAILURE);
    }
    while (argv[1][i])
    {
        if (!ft_isdigit(argv[1][i++]))
        {
            ft_printf("Error: Invalid process ID.");
            exit(EXIT_FAILURE);
        }
    }
    pid = ft_atoi(argv[1]);
    ft_printf("%s, %d\n", argv[2], getpid());
    send_signal(pid, argv[2]);
    return (0);
}
#define _DEFAULT_SOURCE
#define _XOPEN_SOURCE 700
#define WAIT_TIME 100
#include "libft.h"
#include "ft_printf.h"
#include <unistd.h>
#include <signal.h>

void    send_signal(int pid, char *str)
{
    int i;

    while (*str)
    {
        i = (sizeof(char) * 8) - 1;
        while (i >= 0)
        {
            if (*str & (1 << i))
                kill(pid, SIGUSR1);
            else
                kill(pid, SIGUSR2);
            pause();
            //ft_printf("worked");
            i--;
        }
        str++;
    }
}

int main(int argc, char **argv)
{
    int     i;
    int     pid;

    i = 0;
    if (argc != 3)
    {
        ft_printf("Error: Wrong number of arguments.");
        exit(EXIT_FAILURE);
    }
    while (argv[1][i])
    {
        if (!ft_isdigit(argv[1][i++]))
        {
            ft_printf("Error: Invalid process ID.");
            exit(EXIT_FAILURE);
        }
    }
    pid = ft_atoi(argv[1]);
    ft_printf("%s, %d\n", argv[2], getpid());
    send_signal(pid, argv[2]);
    return (0);
}

Solution

  • The pause function doesn't automatically handle signals for you. If you receive a signal that you haven't handled while the pause function is running, your program will still terminate (or more accurately, the signal will have its default effect).

    You need to explicitly add a signal handler for at least the signals you're expecting to receive. In your particular case the handler doesn't need to do anything. Then when the signal handler returns, this will allow the pause function to return.