gpt4 book ai didi

c - 如何干净地重新开发系统功能 "system()"?

转载 作者:太空宇宙 更新时间:2023-11-04 08:12:50 25 4
gpt4 key购买 nike

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

我写的似乎工作正常(编译和执行没有任何错误)。

问题涉及我的函数 system 的返回(在我的代码中称为 mySystem)。例如,在我的子进程中,如果我将一个不存在的 shell 提供给 exec(后者返回 -1),我的子进程将停止,退出代码为 -1,因为我告诉它这样做。但是:我的父进程通过 wait(&status) 检索了这个退出代码,返回... 255 而不是 -1 !

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

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

我的src(有很多评论):

#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
}

最佳答案

请看下图: enter image description here

其中 2 个 block 中的每一个都是 8 位(因此总共 16 位)。现在你通过 exit(-1)在二进制中使用 8 位是:11111111(二补码)这就是为什么使用 WEXITSTATUS(status) 得到 255 的原因.

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

关于c - 如何干净地重新开发系统功能 "system()"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37990593/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com