简体   繁体   中英

how can I send a signal between a child and a parent processes in linux

I have two cods the first one is for the parent which sends a signal ( SIGUSER1 ) to the child and when the child receive it he should print that he received it.

Parent code

#include <stdio.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <unistd.h>
#include <sys/types.h>

void sighand(int);
int main()
{
    int cpid, ppid;
    ppid = getpid();
    printf("My process ID is %d\n", ppid);

    FILE *fp1;
    fp1 = fopen("cpid.txt", "w+");

    cpid = fork();

    if ( cpid == 0 ) {
        printf("I am the child  => PID = %d\n", getpid());
    }
    else
        printf("I am the parent => PID = %d, child ID = %d\n", getpid(), cpid);

    fprintf(fp1, "%d\n", cpid);
    // kill(cpid, SIGUSR1);//id, signal, send
    sigset(SIGUSR2, sighand);

    return 0;
}  

void sighand(int the_sig){
    if (the_sig == SIGUSR2){
        printf("sigusr2 received");
        exit(1);
    }
}

Child code

#include <stdio.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <unistd.h>
#include <sys/types.h>

void sighand1(int);
int main()
{
    FILE *fp1;
    int pid;
    fp1 = fopen("cpid.txt", "r");
    fscanf(fp1, "%d,", &pid);

    sigset(SIGUSR1,sighand1);

    while(1) {
        printf("Waiting..");
        sigpause(SIGUSR1);
    }

    return 0;
}

void sighand1(int the_sig)
{
    if (the_sig == SIGUSR1){
        printf("sigusr1 received");
        exit(1);
    }
}

When I start the code it prints that the process (child) was created then when I send a signal it wont do any thing the child stuck in a loop or the wait and the parent wont do anything can any one tell me where did i go wrong in my code or logic.

Your code has several problems:

  • You try to pass some pid through a file, but you can use the getppid() function (get parent id)
  • You have some child code, but it is not called
  • no signal is launched

So your code can be corrected this way:

#include <stdio.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <unistd.h>
#include <sys/types.h>
#include <signal.h>

void parent_handler(int the_sig)
{
    if (the_sig == SIGUSR2){
        printf("sigusr2 received in parent\n");            
    }
}

void child_handler(int the_sig)
{
    if (the_sig == SIGUSR1){
        printf("sigusr1 received in child\n");
        kill(getppid(), SIGUSR2);
        exit(1);
    }
}

int child_function()
{
    /* prepare to receive signal */
    sigset(SIGUSR1,child_handler);

    while(1) {
        printf("Waiting..");
        fflush(stdout);
        /* wait for signal */
        sigpause(SIGUSR1);
    }

    return 0;
}

int main()
{
    int cpid, ppid;
    ppid = getpid();
    printf("My process ID is %d\n", ppid);

    cpid = fork();

    if ( cpid == 0 ) {
        printf("I am the child  => PID = %d\n", getpid());
        child_function();
        return 0;
    }
    else
        printf("I am the parent => PID = %d, child ID = %d\n", getpid(), cpid);

    /* child will never reach this point */
    sleep(1);

    /* prepare parent to received signal */
    sigset(SIGUSR2, parent_handler);

    /* send signal to child */
    kill(cpid, SIGUSR1);

    sleep(1);

    return 0;
}  

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM