0
我想了解這個系統調用(execle()),但我不知道它是如何工作的。我不知道如何使用char * envp [],我們必須將它作爲參數傳遞。我曾經試過,但它不工作:execle()系統調用如何工作?
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main(void)
{
int pid;
int* status;
char* envp[] = {"/usr/lib", 0};
//Child process
if((pid=fork())==0)
{
printf("I'm the child and I'm going to list...\n");
execle("/bin/ls", "ls", "-l", "-a", 0, envp);
printf("Error\n");
}
//Parent process
else
{
printf("I'm the parent...\n");
wait(status);
}
printf("Child status: %d\n", *status);
}
我不知道我應該放什麼的char * envp []。
'envp'是用於環境變量的,它們是從父環境繼承的,所以沒有必要這樣做。你真的想用它來改變環境嗎?如果是這樣,你想要設置什麼變量? – rodrigo
我不知道我應該在envp中正確運行程序。我已經把/ usr/lib,因爲我想列出該文件夾中的文件,但我認爲我沒有關於envp變量的正確知識...... – vls1
閱讀手冊[exec(3)](http:/ /man7.org/linux/man-pages/man3/exec.3.html)然後閱讀[execve(2)](http://man7.org/linux/man-pages/man2/execve.2.html) 。另請閱讀[高級Linux編程](http://advancedlinuxprogramming.com/) –