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);
}
For starters, this
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:Use it in the following way:
Details can be read in the documentation.
Also this code somehow does not make sense:
You are calling
wait()
from the child, and only after it returned youexec*
isPrime.exe
.You may want to do it like this
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
should be
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.