C program to send signal (SIGTERM) from the signal handler(SIGINT) of one process to another process which is registered with SIGTERM

C program to send signal (SIGTERM) from the signal handler(SIGINT) of one process to another process which is registered with SIGTERM


Neelkanth_98$ cat signal1.c
#include<stdio.h>
#include<signal.h>
#include<unistd.h>

void sig_handler(int signo)
{
    int process_id;
    char process_id_from_file[10];
    FILE *pid_fd;
    char command[100];

    sprintf(command,"pidof %s", "test_process");
    pid_fd = popen(command, "r");
    if (pid_fd == NULL)
    {
        return;
    }

    if (fgets(process_id_from_file, sizeof process_id_from_file , pid_fd) == NULL)
    {
        return;
    }
    pclose(pid_fd);

    process_id = atoi(process_id_from_file);

    if (signo == SIGINT) {
        printf("received SIGINT from another process\n");
        kill(process_id, SIGTERM);
        printf("send SIGTERM to another process\n");
        return;
    }
}

int main(void)
{    /* register signal handler */
    if (signal(SIGINT, sig_handler) == SIG_ERR)
        printf("\ncan't catch SIGINT\n");

    while(1)
        sleep(1);
    return 0;
}
Neelkanth_98$ cat signal2.c
#include<stdio.h>
#include<signal.h>
#include<unistd.h>

void sig_handler(int signo)
{
    if (signo == SIGTERM)
        printf("received SIGTERM from test process\n");
}

int main(void)
{    /* register signal handler */
    if (signal(SIGTERM, sig_handler) == SIG_ERR)
        printf("\ncan't catch SIGTERM\n");

    while(1)
        sleep(1);
    return 0;
}