OiO.lk Blog C# There are some techniques that i'm missing out in creating this ICPC more robust?
C#

There are some techniques that i'm missing out in creating this ICPC more robust?


[C language]\

Hello there! I’m trying to do a simple IPC between two files ".c" but I’m facing some unexpected output.

  1. I’m compiling the server file, nothing wrong, the PID is clearly printed.
  2. I’m compiling the client with PID of the server and a string of 100 chars
    -> usleep(3000) -> expected output
    -> usleep(100) -> undefined behaviour.

Here are the scripts:

server.c

void    handle_signal(int signo, siginfo_t *info, void *context)
{
        static unsigned char     ch = 0;
        static int               idx = 0;
    
        (void)context;
        if (signo == SIGUSR1)
            ch <<= 1;   
        else if (signo == SIGUSR2)
            ch = (ch << 1) | 1;
        idx++;
        if (idx == 8)
        {
            kill(info->si_pid, SIGUSR1);
            write(1, &ch, 1);
            idx = 0;
        }
}
int main(){
        struct sigaction sa;
    
        printf("Server PID: %d\n", getpid());
    
        sa.sa_handler = (void (*)(int))handle_signal;
        sigemptyset(&sa.sa_mask);
        sa.sa_flags = SA_SIGINFO;
    
        sigaction(SIGUSR1, &sa, NULL);
        sigaction(SIGUSR2, &sa, NULL);
    
        while (1) {
            pause();
        }
        return 0;
}

client.c

void    handle_input(int signo){
    static int  number = 0;

    if (signo == SIGUSR1)
        printf("Received letter %d!\n", ++number);
    else
        number = 0;
}

void    handle_output(char **input, pid_t pid){
    int             i = 2;
    int             j = 0;
    int             bits = 8;
    unsigned char   send;

    while (input[i])
    {
        while (input[i][j] != '\0')
        {
            while (bits-- > 0)
            {
                send = input[i][j] >> bits & 1;
                if (send == 0)
                    kill(pid, SIGUSR1);
                if(send == 1)
                    kill(pid, SIGUSR2);
                usleep(100);
            }
            bits = 8;
            j++;
        }
        j = 0;
        i++;
    }
}

int main(int argc, char **argv){
    char                *ptr;
    struct sigaction    sa;
    
    if (argc < 3 || !argv[2][0])
        return (printf("Not enough values!\n"));
    ptr = argv[1];
    while (isdigit(*ptr))
        ptr++;
    if (*ptr != '\0')
        return (printf("Wrong pid format!\n"));
    
    sa.sa_handler = handle_input;
    sigemptyset(&sa.sa_mask);
    sa.sa_flags = SA_RESTART;

    sigaction(SIGUSR1, &sa, NULL);

    handle_output(argv, (pid_t)atoi(argv[1]));

    return (0);
}

What I’m missing to control so that my program works with every kind of input? I don’t want to use any other includes beside signal and sigaction to send and receive signals 😀 Thanks a lot for the help!



You need to sign in to view this answers

Exit mobile version