簡體   English   中英

execvp系統調用問題

[英]execvp system call problems

我正在制作一個小的外殼程序,以更好地理解C。我使用POSIX getline函數獲取字符串,並通過空格將其拆分為標記。 但是,當我調用execvp()進行系統調用時,什么也沒發生。.如果有人指出我的問題所在,顯然我會漏掉一些小問題。.(我沒有提供完整的代碼因此底部將缺少一些大括號,請忽略此抱歉)非常感謝

        char *args[3];  // array for the command and arguments

        cmd = strtok(line, " ");            
        args[0] = cmd;              // put the first command in the array

        for(int i = 1; i < whitespace+1; ++i){

            cmd = strtok('\0', " \n");
            args[i] = cmd;              // fill the array of strings with the arguments
        }
        args[2] = '\0';    // assign last element to NULL


        pid = fork();

        if(pid != 0){        
            waitpid(-1, &stat, 0); 
        }
        else{

            char *const *test[1];
            test[0] = '\0';
            execvp("/bin/ls", test[0]);
            execvp(args[0], &args[1]);

最終在我遇到問題的地方,我分別嘗試了兩個版本的execvp,但都無法正常工作,並且在此問題上已經停留了2天。

這是一個使execvp工作的最小示例。

#include <stdio.h>
#include <unistd.h>

int main( void )
{
    char *test[2];              // declare an array of pointers
    test[0] = "/bin/ls";        // first arg is the path to the executable
    test[1] = NULL;             // NULL terminator indicates no additional args
    execvp( test[0], test );
}

這是execvp手冊頁中的說明

按照慣例,第一個參數應指向與正在執行的文件關聯的文件名。 指針數組必須以NULL指針終止。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM