C “如何重新开发系统功能”;系统();干净的?

C “如何重新开发系统功能”;系统();干净的?,c,process,exit-code,C,Process,Exit Code,我必须开发自己的C函数系统。为此,我使用调用系统fork创建一个子进程,它必须执行给system的命令,调用exec 我所写的似乎工作得很好(编译和执行时没有任何错误) 该问题与我的函数system的返回有关(在我的代码中称为mySystem)。例如,在我的子进程中,如果我给exec(后者返回-1)一个不存在的shell,我的子进程将以-1的退出代码停止,因为我告诉它这样做。但是:我的父进程由于等待(状态)而检索此退出代码,返回。。。255而不是-1 我不明白为什么。我注意在我的mySystem

我必须开发自己的C函数
系统
。为此,我使用调用系统
fork
创建一个子进程,它必须执行给
system
的命令,调用
exec

我所写的似乎工作得很好(编译和执行时没有任何错误)

该问题与我的函数
system
的返回有关(在我的代码中称为
mySystem
)。例如,在我的子进程中,如果我给
exec
(后者返回-1)一个不存在的shell,我的子进程将以-1的退出代码停止,因为我告诉它这样做。但是:我的父进程由于等待(状态)而检索此退出代码,返回。。。255而不是-1

我不明白为什么。我注意在我的
mySystem
返回中使用宏WEXISTATUS

你能帮我知道为什么我的父进程不返回-1(它的子进程的退出代码)吗?先谢谢你

我的src(有很多评论):

#包括
#包括
#包括
#包括
pid_t pid;
int mySystem(char*);
int main(int argc,char*argv[]){
int result=mySystem(“ls”);
fprintf(标准输出,“%i”,结果);
返回0;
}
int mySystem(char*命令){
pid=fork();
如果(pid==-1){
佩罗尔(“福克”);
return-1;//发生错误=>return-1
}否则,如果(pid==0){//子进程将执行以下操作
execl(“bla-MAUVAIS SHELL blablaa”,“sh”,“-c”,command,NULL);//如果此调用没有失败,则不会读取以下行
perror(“Exec”);//如果(且仅当)无法调用execl(坏shell的路径等)。。。
退出(-1);/…,我们停止子进程,这个进程的退出代码等于-1
}
/*
*现在,子进程结束是因为…:
*1.要么是因为“perror”(我们的源代码)后面的“exit(-1)”
*2.或由于传入EXCL的命令的“退出(-1”)(EXCL命令的源代码)
*3.或者由于传入EXCL的命令的“exit(0)”(EXCL命令的源代码)
*/
//父进程将执行以下行(子进程已结束)
int status=-1;
如果(wait(&status)==-1){//我们将子进程的退出代码存储到变量“status”中:-1或0
perror(“Wait”);//注意,因为我们只有一个进程子进程,所以不需要执行:while(Wait(&status)>0){;}
返回-1;
}
return WEXITSTATUS(status);//我们的函数mySystem返回这个退出代码
}

看下图:

其中,2个块中的每个块是8位(因此总共16位)。 现在通过
exit(-1)
,它在二进制中使用8位表示为:11111111(两个补码),这就是为什么使用
WEXITSTATUS(status)
得到255


另一个需要说明的例子是:我们假设调用退出(-6),二进制二补中的-6是11111 010,对应于250,如果您让程序运行,您将看到打印在标准输出上的250。

请看下图:

其中,2个块中的每个块是8位(因此总共16位)。 现在通过
exit(-1)
,它在二进制中使用8位表示为:11111111(两个补码),这就是为什么使用
WEXITSTATUS(status)
得到255


另一个需要说明的例子是:假设调用
exit(-6)
,二进制补码中的-6是11111 010,对应于250,如果你让你的程序运行,你会看到250打印在标准输出上。

文档中说的可能重复?你在文档中没有理解什么?啊,我再次阅读了文档,我认为我的错误必然是“这由最低有效8位组成”。。。嗯,我不能返回一个正值。我将返回到
signed char
。顺便说一句,shell中“program not found”的退出代码为127。文档中可能有重复的内容?你在文档中没有理解什么?啊,我再次阅读了文档,我认为我的错误必然是“这由最低有效8位组成”。。。嗯,我不能返回一个正值。我将返回到
signed char
。顺便说一句,shell中“program not found”的退出代码为127。非常感谢。“状态”是否为“返回主”和“退出”值?我认为是这样;那么,错误代码是什么?是“呃,不是”吗?我想它也可以是“returnin main”或“exit”值。不exit()调用没有设置errno值我建议您查找errno谢谢,我已经知道了。我只是不太清楚用于进程的词汇表(图像中的状态和错误代码)。我认为“status”是进程的状态(因此不是main/exit中的返回值)和错误代码errno。但不是,非常感谢。“状态”是否为“返回主”和“退出”值?我认为是这样;那么,错误代码是什么?是“呃,不是”吗?我想它也可以是“returnin main”或“exit”值。不exit()调用没有设置errno值我建议您查找errno谢谢,我已经知道了。我只是不太清楚用于进程的词汇表(图像中的状态和错误代码)。我认为“status”是进程的状态(因此不是main/exit中的返回值)和错误代码errno。但事实并非如此。
#include <stdio.h>
#include <unistd.h>
#include <sys/wait.h>
#include <stdlib.h>

pid_t pid;

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

    int result = mySystem("ls");
    fprintf(stdout, "%i", result);

    return 0;
}

int mySystem(char* command) {
    pid = fork();
    if(pid == -1) {
        perror("Fork");
        return -1; // An error occurred => return -1
    } else if (pid == 0) { // The child process will do the following
        execl("BLABLABLABLA MAUVAIS SHELL BLABLABLAA", "sh", "-c", command, NULL); // If this call doesn't fail, the following lines are not read
        perror("Exec"); // If (and only if) execl couldn't be called (bad shell's path, etc.)...
        exit(-1); // ..., we stop the child process and this one has an exit code equaled to -1
    }

    /*
     * NOW, the child process ended because... :
     * 1. Either because of our "exit(-1)" after the "perror"               (our source-code)
     * 2. OR because of an "exit(-1") of the command passed into the execl  (source-code of the execl's command)
     * 3. OR because of the "exit(0)" of the command passed into the execl  (source-code of the execl's command)
     */

    // The parent process will execute the following lines (child process ended)
    int status = -1;
    if(wait(&status) == -1) { // We store into the var 'status' the exit code of the child process : -1 or 0
        perror("Wait"); // Note that because we have only one process child, we don't need to do : while(wait(&status) > 0) {;;}
        return -1;
    }

    return WEXITSTATUS(status); // Our function mySystem returns this exit code
}