使用从等待子进程检索回报code()? [英] Retrieving return code from child process using wait()?
问题描述
我有2个文件prime.c和singlePrime.c和singlePrime.c里面我想创建本身摇身一变成了isPrime.exe,这是造出来的黄金的可执行的孩子。 C。我想要做的就是从isPrime.exe得到回报号码,以便取决于如果输入的数是素与否1或0,然后保存它childNumsinglePrime.c的main()函数中,使我可以打印到终端是否是一个素数或不根据1或0是从isPrime.exe返回。下面是我的2个文件:
prime.c:
/ *
文件isPrime.c目的:
一个程序,检查是否一个给定的数字是一个素数输入:
号 - 积极INT通过命令行参数输入。例如isPrime 1234输出:
0 - 如果输入数不是质数
1 - 如果输入数是素数
2 - 如果命令行参数不正确假设:
程序不检查,如果数字是正整数* /
的#includestdio.h中
#包括stdlib.h中
#包括LT&;&unistd.h中GT;
#包括prime.hINT主(INT ARGC,CHAR *的argv [])
{
INT I; INT编号; 如果(的argc 2){
的printf(回归2);
返回(2);
}
其他数=的atoi(ARGV [1]);
如果(isPrime(编号)){
出口(1);
}
出口(0);
}
/ *
目的:检查输入的数字是一个素数
输入:
数 - 数进行检查返回:
0 - 如果数目是不是素数
1 - 如果数目为一个质数
* /
INT isPrime(INT数)
{
INT I;
对于(i = 2; I * I<数;我++){
usleep(100);
如果(编号%我== 0){
的printf(%d为不是素数!,数);
返回(0);
}
}
的printf(%d是一个素数!,数);
返回(1);
}
singlePrime.c:
的#includestdio.h中
#包括stdlib.h中
#包括LT&;&unistd.h中GT;
#包括prime.hINT主(INT ARGC,CHAR *的argv [])
{
INT编号,childNum; 如果(!与atoi(argv的[1])){
的printf(请使用有效的整数作为参数!);
出口(1);
} INT PID =叉(); 如果(PID == 0){
等待(childNum);
变身(ARGV [1]);
} 其他{
如果(childNum == 0){
输出(输入数不是质数!);
出口(1);
}
否则如果(childNum == 1){
输出(输入数为素数!);
出口(1);
}
否则如果(childNum == 2){
的printf(请输入一个数大于2!);
出口(1);
}
}
}无效变身(字符*号)
{
字符* primeArgv [] = {
isPrime.exe
数,
空值,
}; execvp(isPrime.exe,primeArgv);
}
对于新手来说,这个
等待(childNum);
应该让编译器发出明确警告,从中你能已制定,有什么不对的。
事实上你正在使用等待()
错误。它需要一个指针:
等待(INT *状态);
使用它的方式如下:
{
INT状态;
INT结果; 做
{
结果= 0;
如果((将为pid_t)-1)==等待(安培;状态)))
{
结果= -1;
如果(错误== EINTR)
{
继续;
}
其他
{
PERROR(等待()失败);
}
} 打破;
}而(1); 如果(-1!=结果)
{
如果(WIFEXITED(状态))
{
诠释EC = WEXITSTATUS(状态);
的printf(孩子的退出code为%d \\ n,EC);
}
其他
{
的printf(孩子没有提供退出code这可能崩溃\\ n?);
}
}
}
详细信息可在的文档读取。
另外这款code不知何故没有意义:
如果(PID == 0){
等待(childNum);
变身(ARGV [1]);
}
您在呼唤等待()
从孩子,以后只有它返回你 EXEC *
isPrime.exe
。
您可能想要做这样
PID =叉();
如果(-1 == PID)
{
PERROR(fork()的失败);
}
否则,如果(PID == 0)
{
变身(ARGV [1]);
}
其他
{
如上图所示/ *等待code放在这里。
childNum需要得到assigend。 * /
如果(childNum == 0){
....
}
此外^ 2 code不测试调用 execvp()是否
成功了。
将一个 PERROR(execvp()失败。);
的后立即
此外^ 3本
的#includestdio.h中
#包括stdlib.h中
应
的#include<&stdio.h中GT;
#包括LT&;&stdlib.h中GT;
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
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>
这篇关于使用从等待子进程检索回报code()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!