Retrieving return code from child process using wa

2019-08-09 17:53发布

问题:

I have 2 files "prime.c" and "singlePrime.c" and inside of singlePrime.c I am trying to create a child that morphs itself into "isPrime.exe" which is an executable made out of "prime.c". What I want to do is get the return number from isPrime.exe so either 1 or 0 depending on if the input number is prime or not and then store it in childNum inside of the main() function of "singlePrime.c" so that I can print to the terminal whether it's a prime number or not based on 1 or 0 that is returned from "isPrime.exe". Below are my 2 files:

prime.c:

/*
File is isPrime.c

Purpose:
a program that checks if a given number is a prime number

input:
number - a positive int  entered via the command line parameters.  For example isPrime 1234

output:
0 - if the input number is not a prime number
1 - if the input number is a prime number
2 - if the command line parameter is not correct

Assumption:
the program does not check if the number is a positive integer

*/


#include "stdio.h"
#include "stdlib.h"
#include <unistd.h>
#include "prime.h"

int main(int argc, char *argv[])
{
        int i;

        int number;

        if (argc < 2) {
            printf("Returning 2");
            return(2);
        }
        else number = atoi(argv[1]);
        if (isPrime(number)) {
                exit(1);
        }
        exit(0);
}


/*
Purpose: check if the input number is a prime number
input:
number - the number to be checked

return:
0 - if the number is not a prime number
1 - if the number is a prime number
*/
int isPrime(int number)
{
        int i;
        for(i = 2; i*i < number; i++) {
                usleep(100);
                if (number % i == 0) {
                        printf("%d is not a prime number!",number);
                        return(0);
                }
        }
        printf("%d is a prime number!",number);
        return(1);
}

singlePrime.c:

#include "stdio.h"
#include "stdlib.h"
#include <unistd.h>
#include "prime.h"

int main(int argc, char *argv[])
{
        int number, childNum;

        if (!atoi(argv[1])) {
                printf("Please use a valid integer as an argument!");
                exit(1);
        }

        int pid = fork();

        if (pid == 0) {
                wait(childNum);
                morph(argv[1]);
        }

        else {
                if (childNum == 0) {
                        printf("The input number is not a prime number!");
                        exit(1);
                }
                else if (childNum == 1) {
                        printf("The input number is a prime number!");
                        exit(1);
                }
                else if (childNum == 2) {
                        printf("Please input a number greater than 2!");
                        exit(1);
                }
        }
}

void morph(char *number)
{
        char *primeArgv[] = {
                "isPrime.exe",
                number,
                NULL,
        };

        execvp("isPrime.exe",primeArgv);               
}

回答1:

For starters, this

wait(childNum);

should have made the compiler issue a clear warning, from which you could have drawn that there is something wrong.

And indeed you are using wait() wrongly. It expects a pointer:

wait(int * status);

Use it in the following way:

  {
    int status;
    int result;

    do
    {
      result = 0;
      if ((pid_t) -1) == wait(&status)))
      {
        result = -1;
        if (errno == EINTR)
        {
          continue;
        }
        else
        {
          perror("wait() failed");
        }
      }

      break;
    } while (1);

    if (-1 != result)
    {
      if (WIFEXITED(status))
      {
        int ec = WEXITSTATUS(status);
        printf("The child's exit code is %d\n", ec);
      }
      else
      {
        printf("The child did not provide an exit code. It probably crashed?\n");
      }
    }
  }

Details can be read in the documentation.


Also this code somehow does not make sense:

        if (pid == 0) {
            wait(childNum);
            morph(argv[1]);
        }

You are calling wait() from the child, and only after it returned you exec* isPrime.exe.

You may want to do it like this

      pid = fork();
      if (-1 == pid)
      {
         perror("fork() failed");
      }
      else if (pid == 0) 
      {
        morph(argv[1]);
      }
      else
      {
        /* wait code as shown above goes here.
           childNum needs to get assigend. */
        if (childNum == 0) {
        ....
      }

Also^2 the code does not test whether the call to execvp() succeeded.

Put a perror("execvp() failed"); right after the it.


Also^3 this

#include "stdio.h"
#include "stdlib.h"

should be

#include <stdio.h>
#include <stdlib.h>


回答2:

You don't need to explicitly use fork(), check the system() function (http://linux.die.net/man/3/system), which gives you nice and easy the return code of the other process.