2014-12-25 68 views
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 []。

+0

'envp'是用於環境變量的,它們是從父環境繼承的,所以沒有必要這樣做。你真的想用它來改變環境嗎?如果是這樣,你想要設置什麼變量? – rodrigo

+0

我不知道我應該在envp中正確運行程序。我已經把/ usr/lib,因爲我想列出該文件夾中的文件,但我認爲我沒有關於envp變量的正確知識...... – vls1

+2

閱讀手冊[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/) –

回答

0

但是在什麼情況下可以使用這個 系統調用(execle()),而不是execl()? –   V í男星

那麼,在情況下,你希望新工藝有這樣精確的環境,而不是父之一。這在創建新會話的安全敏感應用程序,登錄管理器等中很有用。普通應用程序應該繼承父環境,所以最好使用execl()或者execlp()。 –   rodrigo