简体   繁体   中英

child-process signal handling in c

I need your help in an exercise i have about signal handling between processes. I think that it's a trivial question but i can't find the answer anywhere. I want to print something from the parent in a file, send a signal from the parent to the child, the child has to print in a file and then send a signal from the child to the parent.

The code i am using is this:

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

#define WRITEF 123451  //Random number

FILE *infile;

void writef() {
    fprintf(infile, "Child Starting (%d)\n", getpid());
    printf("Child Starting (%d)\n", getpid());
}

int main() {
    pid_t pid;

    infile = fopen("pid_log.txt","w");
    pid = fork();
    signal(WRITEF, writef);
    if ( pid == 0 ) {
            pause();
            printf("enter child\n");
    }
    else {
            fprintf(infile, "Parent (%d)\n", getpid());
            printf("Parent (%d)\n", getpid());
            kill(pid, WRITEF);
            pause();
            wait((int*)1);
    }

    fclose(infile);
    return 1;
}

PROBLEM SOLVED!!! The key is that you have to register the signal (use the singal function) before every pause(). Also you cannot use a "user-made" signal and in my case i used SIGCONT.

Here is the final (almost) version of my program:

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

FILE *infile;

void noth() {

}

void writec() {
    infile = fopen("pid_log.txt","a+");
    fprintf(infile, "Child (%d)\n", getpid());
    printf("Child (%d)\n", getpid());
    fclose(infile);
}

void writep() {
    infile = fopen("pid_log.txt","a+");
    fprintf(infile, "Parent (%d)\n", getpid());
    printf("Parent (%d)\n", getpid());
    fclose(infile);
}

main() {
    pid_t pid = fork();

    if ( pid == 0 ) {                                               //child process
            signal(SIGCONT,noth);                          //registering signal before pause()
            pause();
            infile = fopen("pid_log.txt","a+");
            printf("Child Starting (%d)\n",getpid());
            fprintf(infile,"Child Starting (%d)\n",getpid());
            fclose(infile);
            while (1) {
                    sleep(2);

                    kill(getppid(), SIGCONT);                       //sending singal to parent

                    signal(SIGCONT, writec);

                    pause();
            }
    }
    else {                                                          //parent process
            infile = fopen("pid_log.txt","a+");
            printf("Parent Starting (%d)\n",getpid());
            fprintf(infile,"Parent Starting (%d)\n",getpid());
            fclose(infile);
            kill(pid, SIGCONT);
            signal(SIGCONT, writep);
            pause();
            while (1) {
                    sleep(2);

                    kill(pid, SIGCONT);

                    signal(SIGCONT, writep);

                    pause();
            }
    }
    return 1;
}

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