Linux–exec函數族及system函數

來源:互聯網
上載者:User
參考:http://qzone.qq.com/blog/119994997-1236688022          http://hi.baidu.com/colin719/blog/item/f6ea44e782e1152fb938205c.html

 

exec函數族包括6個函數:

#include <unistd.h>
int execl(const char *path, const char *arg, ...);
int execlp(const char *file, const char *arg, ...);
int execle(const char *path, const char *arg, const char *envp[]);
int execv(const char *path, const char *argv[]);
int execve(const char *path, const char *argv[], const char *envp[];
int execvp(const char *file, const char *argv[]);


參數說明:

execl的第一個參數是包括路徑的可執行檔,後面是列表參數,列表的第一個為命令path,接        著為參數列表,最後必須以NULL結束。
execlp的第一個參數可以使用相對路徑或者絕對路徑。
execle,最後包括指向一個自訂環境變數列表的指標,此列表必須以NULL結束。
execv,v表示path後面接收的是一個向量,即指向一個參數列表的指標,注意這個列表的最後        一項必須為NULL。
execve,path後面接收一個參數列表向量,並可以指定一個環境變數列表向量。
execvp,第一個參數可以使用相對路徑或者絕對路徑,v表示後面接收一個參數列表向量。

    exec被調用時會替換調用它的進程的程式碼片段和資料區段(但是檔案描述符不變),直接返回到調用它的進程的父進程,如果出錯,返回-1並設定errno。


例子:
#include <unistd.h>
int main(int argc, char *argv[])
{
        char *envp[]={"PATH=/tmp", "USER=lei", "STATUS=testing", NULL};
        char *argv_execv[]={"echo", "excuted by execv", NULL};
        char *argv_execvp[]={"echo", "executed by execvp", NULL};
        char *argv_execve[]={"env", NULL};
        if(fork()==0) {
                if(execl("/bin/echo", "echo", "executed by execl", NULL)<0)
                        perror("Err on execl");
        }
        if(fork()==0) {
                if(execlp("echo", "echo", "executed by execlp", NULL)<0)
                        perror("Err on execlp");
        }
        if(fork()==0) {
                if(execle("/usr/bin/env", "env", NULL, envp)<0)
                        perror("Err on execle");
        }
        if(fork()==0) {
                if(execv("/bin/echo", argv_execv)<0)
                        perror("Err on execv");
        }
        if(fork()==0) {
                if(execvp("echo", argv_execvp)<0)
                        perror("Err on execvp");
        }
        if(fork()==0) {
                if(execve("/usr/bin/env", argv_execve, envp)<0)
                        perror("Err on execve");
        }
}

    程式裡調用了2 個Linux 常用的系統命令,echo和env。echo會把後面跟的命令列參數原封不動的列印出來,env用來列出所有環境變數。
    由於各個子進程執行的順序無法控制,所以有可能出現一個比較混亂的輸出--各子進程列印的結果交雜在一起,而不是嚴格按照程式中列出的次序。
最常見的錯誤:
    平時的編程中,如果用到了exec 函數族,一定記得要加錯誤判斷語句。因為與其他系統調用比起來,exec很容易受傷,被執行檔案的位置,許可權等很多因素都能導致該調用的失敗。
    最常見的錯誤是:
    1)找不到檔案或路徑,此時errno 被設定為ENOENT;
    2)數組argv和envp忘記用NULL結束,此時errno被設定為EFAULT;
    3)沒有對要執行檔案的運行許可權,此時errno被設定為EACCES。







system函數:



原型:
#include <stdlib.h>
int system (const char *string);
功能:
The system function runs the command passed to it as
string and waits for it to complete. The command is
executed as if the command $ sh −c string has been given
to a shell.
【也就是說,system要使用一個shell來啟動指定的程式】


例:
#include <stdlib.h>
#include <stdio.h>
int main()
{
    printf("Running ps with system/n");
    system("ps −ax");
    printf("Done./n");
    exit(0);
}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.