0

So, I'm exiting from the child thread back to the parent. I am using the _exit() system call. I was wondering a few things. One was what parameter for the _exit for my child. Here is the code that my child process is executing:

printf("\n****Child process.****\n\nSquence: ");

 do{
        //Print the integer in the sequence.
        printf("%d\t",inputInteger);
        if((inputInteger%2) == 0){
            //printf("inputInteger = %d\n", inputInteger);
            inputInteger = inputInteger / 2;
        }else{
            inputInteger = 3*inputInteger +1;
            //printf("%d\t",inputInteger);
        }

    }while(inputInteger != 1);

    //Makes sure we print off 1!
    printf("%d\n\n", inputInteger);

    //Properly exit
    _exit(status);

I use status because back in my parent thread I use it in the waitpid() system call. Here is the code for parent process that is executed after the child is completed.

waitpid_check = waitpid(processID, &status, 0);
        printf("\n****Parent process.****\n");

        if(waitpid_check == -1){
            printf("Error in waitpid.\n");
            exit(EXIT_FAILURE);
        }

        if(WIFEXITED(status)){
            printf("Child process terminated normally!\n");
        }

Here I'm using waitpid() system call that ensures that the child was exited, then use status to check if it was exited properly. I was wondering if I was going about this in the right way of creating the child and exiting it.

Then I was also wondering if I was correctly checking the exiting of the child in the parent.

Thanks for your help!

pmac89
  • 418
  • 1
  • 6
  • 23

2 Answers2

0

I'd love to help but I'm really rusty on these calls. If you've read through the documentation on these API calls and you're checking everywhere for error returns, then you should be in good shape.

The idea seems good at a high level.

One thing to keep in mind is you might want to surround the meat of your child method in a try/catch. With threads, you often don't want an exception to mess up your main flow.

You won't have that problem with multiple processes, but think about whether you want _exit to be called in the face of an exception, and how to communicate (to the parent or to the user) that an exception occurred.

jia103
  • 1,116
  • 2
  • 13
  • 20
0

From the waitpid linux manual.

"If status is not NULL, wait() and waitpid() store status information in the int to which it points."

You don't need the return value of wait paid to check if the child failed. You need to check to value of status. There are a handful of macros to check status.

WIFEXITED(status)
returns true if the child terminated normally, that is, by calling exit(3) or _exit(2), or by returning from main().
WEXITSTATUS(status)
returns the exit status of the child. This consists of the least significant 8 bits of the status argument that the child specified in a call to exit(3) or _exit(2) or as the argument for a return statement in main(). This macro should only be employed if WIFEXITED returned true.
WIFSIGNALED(status)
returns true if the child process was terminated by a signal.
WTERMSIG(status)
returns the number of the signal that caused the child process to terminate. This macro should only be employed if WIFSIGNALED returned true.
WCOREDUMP(status)
returns true if the child produced a core dump. This macro should only be employed if WIFSIGNALED returned true. This macro is not specified in POSIX.1-2001 and is not available on some UNIX implementations (e.g., AIX, SunOS). Only use this enclosed in #ifdef WCOREDUMP ... #endif.
WIFSTOPPED(status)
returns true if the child process was stopped by delivery of a signal; this is only possible if the call was done using WUNTRACED or when the child is being traced (see ptrace(2)).
WSTOPSIG(status)
returns the number of the signal which caused the child to stop. This macro should only be employed if WIFSTOPPED returned true.
WIFCONTINUED(status)
(since Linux 2.6.10) returns true if the child process was resumed by delivery of SIGCONT.

As for whether or not you are exiting the child process right that really depends. You would exit like you would in any other program since when you fork a process you are really just duplicating an address space and the child when run as its own independent program (of course with the same open FD's, already declared values etc as parent). Below is typical implementation for this problem (although NULL is being passed to the wait instead of a status so I think you are doing it right.)

/* fork a child process */
pid = fork();

if (pid < 0) { /* error occurred */
    fprintf(stderr, "Fork Failed\n");
    return 1;
}
else if (pid == 0) { /* child process */
    printf("I am the child %d\n",pid);
    execlp("/bin/ls","ls",NULL);
}
else { /* parent process */
    /* parent will wait for the child to complete */
    printf("I am the parent %d\n",pid);
    wait(NULL);

    printf("Child Complete\n");
}

return 0;
PandaRaid
  • 618
  • 1
  • 9
  • 22
  • Thanks, I missed the _exit(2) part in the man page. That was my main question. I just didn't want to show all of my code as it is an assignment. – pmac89 Feb 18 '14 at 20:24