Lyn*_*ynn 3 c shell exit-code exit
为什么当我在 c 中的函数中执行此代码时
int Pandoc(char *file)
{
//printf("Pandoc is trying to convert the file...\n");
// Forking
pid_t pid;
pid = fork();
if (pid == -1)
{
perror("Fork Error");
}
// child process because return value zero
else if (pid == 0)
{
//printf("Hello from Child!\n");
// Pandoc will run here.
//calling pandoc
// argv array for: ls -l
// Just like in main, the argv array must be NULL terminated.
// try to run ./a.out -x -y, it will work
char *output = replaceWord(file, ".md", ".html");
//checking if the file exists
char *ls_args[] = {"pandoc", file, "-o", output, NULL};
// ^
// use the name ls
// rather than the
// path to /bin/ls
// Little explaination
// The primary difference between execv and execvp is that with execv you have to provide the full path to the binary file (i.e., the program).
// With execvp, you do not need to specify the full path because execvp will search the local environment variable PATH for the executable.
if(file_exist(output)){execvp(ls_args[0], ls_args);}
else
{
//Error Handeler
fprintf(stdout, "pandoc should failed with exit 42\n");
exit(42);
printf( "hello\n");
}
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我得到 0 作为返回值?
编辑:所以在这里我将 main 的返回值更改为 5。我上面函数的退出值是 42(idk 为什么 tho)它给了我 5 作为输出..不知道发生了什么。我应该提到我在我的代码中使用 fork() .. 也许是这个原因。
我认为我的退出关闭了子进程,但主进程继续......所以这就是为什么它给了我主进程中的返回值而不是退出进程。