Saturday, April 30, 2011

What should I do to modify my fork() in C to run well?

I dont understand why my code does not work.

This is my code. I don't know why I'm get an error segment. Could somebody explain the reason to me?

#include <iostream>
#include <string>
#include <sys/types.h>
#include <unistd.h>

int id_process;

void manager_signal () {
    kill (id_process, SIGKILL);
    kill (getppid(),SIGKILL);
}

int main () {
    id_process = fork ();
    if (id_process==-1) {
        perror("ERROR to create the fork");
    } else {
        if ( id_process != 0 ) {
            printf("Father´s ID is %d \n", getpid());   
            alarm(5);
            (void) signal (SIGALRM, manager_signal);
            sleep (20);
            printf ("Running to where the father can be\n");
            alarm (0);    
        } else {
            printf ("CHildren´s ID is %d \n", getpid ());
            for (;;) {
                printf ( "Children RUN FOREVER ^^");
                sleep (2);
            }
        }
    }
    return 0;
}
From stackoverflow
  • Your question is a little difficult to understand since you don't really explain what the error is, but I do have one question which I'm sure will be pertinent.

    Why is the "father" process killing its child and its parent? Shouldn't it kill its child and itself (id_process and getpid() rather than getppid() which is the parent PID)?

    That appears to be the problem. When I run that under Cygwin, it kills off my shell (darned annoying). If I change it to kill (getpid(),SIGKILL);, it terminates okay after five seconds with the following output:

    $ vi qq.cpp ; g++ -o qq qq.cpp ; ./qq.exe
    Fathers ID is 6016
    Childrens ID is 4512
    Children RUN FOREVER ^^
    Children RUN FOREVER ^^
    Children RUN FOREVER ^^
    Children RUN FOREVER ^^
    Children RUN FOREVER ^^
    Killed
    

    This is with the program modified as follows:

    #include <iostream>
    #include <string>
    #include <sys/types.h>
    #include <unistd.h>
    
    int id_process;
    
    void manager_signal (int x) {
        kill (id_process, SIGKILL);
        kill (getpid(),SIGKILL);
    }
    
    int main () {
        id_process = fork ();
        if (id_process==-1) {
            perror("ERROR to create the fork");
        } else {
            if ( id_process != 0 ) {
                printf("Fathers ID is %d\n", getpid());
                alarm(5);
                (void) signal (SIGALRM, manager_signal);
                sleep (20);
                printf ("Running to where the father can be\n");
                alarm (0);
            } else {
                printf ("Childrens ID is %d\n", getpid ());
                for (;;) {
                    printf ( "Children RUN FOREVER ^^\n");
                    sleep (1);
                }
            }
        }
        return 0;
    }
    
    Jonathan Leffler : Run the faulty program as: sh -c "./testprog"; then your main shell isn't killed. Good spotting on the incorrect definition for manager_signal(); can you still do: void manager_signal(int) to indicate that the function takes an int (the signal number) but ignores it? It was a nice feature in C++ as I recall it, one that C would do well to adopt too.
  • I don't think

        kill (id_process, SIGKILL);
    

    is required either. You're killing the same process in the next instruction.

0 comments:

Post a Comment