c - Retrieving return code from child process using wait()? -
i have 2 files "prime.c" , "singleprime.c" , inside of singleprime.c trying create child morphs "isprime.exe" executable made out of "prime.c". want return number isprime.exe either 1 or 0 depending on if input number prime or not , store in childnum inside of main() function of "singleprime.c" can print terminal whether it's prime number or not based on 1 or 0 returned "isprime.exe". below 2 files:
prime.c:
/* file isprime.c purpose: program checks if given number prime number input: number - positive int entered via command line parameters. example isprime 1234 output: 0 - if input number not prime number 1 - if input number prime number 2 - if command line parameter not correct assumption: program not check if number 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 input number prime number input: number - number checked return: 0 - if number not prime number 1 - if number prime number */ int isprime(int number) { int i; for(i = 2; i*i < number; i++) { usleep(100); if (number % == 0) { printf("%d not prime number!",number); return(0); } } printf("%d 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 valid integer argument!"); exit(1); } int pid = fork(); if (pid == 0) { wait(childnum); morph(argv[1]); } else { if (childnum == 0) { printf("the input number not prime number!"); exit(1); } else if (childnum == 1) { printf("the input number prime number!"); exit(1); } else if (childnum == 2) { printf("please input number greater 2!"); exit(1); } } } void morph(char *number) { char *primeargv[] = { "isprime.exe", number, null, }; execvp("isprime.exe",primeargv); }
for starters, this
wait(childnum);
should have made compiler issue clear warning, have drawn there wrong.
and indeed using wait()
wrongly. expects pointer:
wait(int * status);
use in following way:
{ int status; int result; { 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 %d\n", ec); } else { printf("the child did not provide exit code. crashed?\n"); } } }
details can read in the documentation.
also code somehow not make sense:
if (pid == 0) { wait(childnum); morph(argv[1]); }
you calling wait()
child, , after returned exec*
isprime.exe
.
you may want this
pid = fork(); if (-1 == pid) { perror("fork() failed"); } else if (pid == 0) { morph(argv[1]); } else { /* wait code shown above goes here. childnum needs assigend. */ if (childnum == 0) { .... }
also^2 code not test whether call execvp()
succeeded.
put perror("execvp() failed");
right after it.
also^3 this
#include "stdio.h" #include "stdlib.h"
should be
#include <stdio.h> #include <stdlib.h>
Comments
Post a Comment