Я должен разработать свою собственную функцию С system
. Для этого я использую call-систему fork
для создания дочернего процесса и ему необходимо выполнить команду, указанную system
, вызывая exec
.Как переосмыслить системную функцию "system()" чисто?
То, что я написал, кажется, работает нормально (он компилируется и выполняется без какой-либо ошибки).
Проблема связана с моей функцией system
с возвратом (в моем коде) mySystem
. Например, в моем дочернем процессе, если я даю несуществующую оболочку exec
(последний возвращает -1), мой дочерний процесс останавливается с кодом выхода -1, потому что я сказал ему сделать это. Но: мой родительский процесс, который извлекает этот код выхода из-за wait(&status)
, возвращает ... 255, а не -1!
Я не понимаю, почему. Я обратил внимание на использование макроса WEXISTATUS в моем возврате mySystem
.
Не могли бы вы помочь мне узнать, почему мой родительский процесс не возвращает -1 (код выхода его дочернего процесса), пожалуйста? Заранее спасибо.
Мой ЦСИ (есть много комментариев):
#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
}
Возможный дубликат [Что возвращает WEXITSTATUS (статус)?] (Http://stackoverflow.com/questions/20465039/what-does-wexitstatusstatus-return) –
Что говорят в документах? Что вы не понимаете в документации? – Olaf
А я снова прочитал документацию, и я думаю, что моя ошибка связана с «Это состоит из наименее значимых 8 бит» ... Ммм, я не могу вернуть положительное значение. Я вернусь к 'signed char'. –