- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在尝试运行一段将执行一些 UNIX 命令的代码,这些命令存储在数组 lineArray 中,例如:lineArray = {"ls -l", "ls", "pwd ", NULL};
问题是这段代码只会打印出数组中的第一个命令,即使我在调试时看到我的函数根据 execvp MAN 正确地解析了命令及其参数。
我们将不胜感激任何形式的帮助。
int startProcesses(int background) {
int i = 0;
int j = 0;
int pid;
int status;
char *copyProcessName[256];
int len, var=0;
while(lineArray[i] != NULL) {
while(*(copyProcessName+var) != NULL) {
copyProcessName[var] = NULL;
}
j=0;
copyProcessName[j] = strtok(lineArray[i], " ");
while (copyProcessName[j] != NULL){
j++;
copyProcessName[j] = strtok(NULL, " ");
}
pid = fork();
if (pid == 0) {
// Child Process
execvp(copyProcessName[0], copyProcessName);
fflush(stdout);
i++;
continue;
} else if (!background) {
// Parent Process
waitpid(pid, &status, 0);
i++;
if(WEXITSTATUS(status)) {
printf(CANNOT_RUN_ERROR);
return 1;
}
} else {
i++;
continue;
}
}
return 0;
最佳答案
这段代码显然不对:
len = strlen(copyProcessName);
for (var = 0; var < len; ++var) {
copyProcessName[var] = NULL;
}
鉴于如果 len
可能为零,我们不知道 copyProcessName 的内容实际包含什么。
while(*(lineArray+i) != NULL)
有什么问题:
while(lineArray[i] != NULL)
它更短,并且是一个数组,因此您可能希望使用 []
对其进行索引。
您还应该检查 execvp(..)
的返回值 - 如果它返回,您将想要打印返回值是什么,因为这将表明您可能做错了什么.
在外循环的第二次迭代中,当您到达下面的代码时,j
不为零,这可能会导致各种问题:
copyProcessName[j] = strtok(lineArray[i], " ");
while (copyProcessName[j] != NULL){
j++;
copyProcessName[j] = strtok(NULL, " ");
}
这不是您的代码问题的最终列表,只是我在快速阅读代码时发现的问题。
关于c - execvp 不会执行命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16251893/
POSIX 系统公开了一系列 exec 函数,这些函数允许将可能不同的东西加载到当前进程中,保持打开的文件描述符、进程标识符等。 这可以出于多种原因完成,在我的例子中,这是自举——我想更改我自己进程的
我有一个用 c 编写的程序,它使用 execvpe(3) 函数,并且我设置了一行以包含必需的头文件: #include 我使用以下命令编译此文件... gcc foo.c -o foo ...仅收到
我在 C 中有一个函数,它创建一个子进程并使其运行 execvp。 int Execute(char **arg) { pid_t pid; int status; if ((
我正在使用 C 编写一个非常基本的 UNIX shell。在这个项目中,我试图使用 fork() 和 execvp() 来执行实际的 shell命令。我遇到了一个问题,它似乎可以很好地处理具有单个参数
我无法获得使用 execvp 调用的程序的完整输出。在这里,我基本上是在尝试用一个可以过滤输入和输出的程序来包装任何程序: #define BUF_SIZ 8192 int main(int argc
已关闭。此问题需要 debugging details 。目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and the
我正在尝试用 c 语言创建一个 shell,当前正在尝试 ls 命令,但子进程总是返回 execvp 无法工作。这是函数。首先,我使用 exec 获取我所在的路径和从 stdin 获取的命令,这就是为
我正在开发这个程序,使用系统调用 execvp() 和 fork() 来运行通过命令行参数给出的 shell 命令。这里 arglist 是一个二维数组,其中包含命令名称及其参数列表。我将命令名称作为
我尝试使用 execvp 创建一个作为参数给出的存储库。但我遇到了一些无法在以下代码中解决的段错误(核心转储): #include //mkdir #include #include //p
我正在尝试使用 execvp 对文件进行排序,这是我的代码。 char *argv1[]={ "sh", "-c", "sort input.txt > output.txt", NULL }; 问题
我正在c中测试execvpe(),我尝试了下面的代码,这导致错误“函数‘execvpe’的隐式声明在C99 [-Wimplicit-function-declaration]中无效”。 #includ
我有以下代码: int main(int argc, char *argv[]) { int i, a=9; int length = 0; c
我正在使用 execvp() 运行一些系统调用。程序对于有效命令非常有效,对于任何不存在的命令则失败,这是完美的。该程序是,当我在需要额外参数(如 cat)的命令上使用 execvp() 并且我不提供
用户将读取一行,我将保留第一个单词作为 execvp 的命令。 假设他将输入“cat file.txt” ...命令将为 cat 。但我不知道如何使用这个execvp(),我读了一些教程但仍然没有明白
我正在用 C 编程编写一个正在运行的 shell 脚本。我读过有关 exec 函数的内容,虽然不太了解,但我读过一个像这样使用 execvp 的示例 execvp(*argv, argv) ; /*
这是我的输入函数: int i,j,n,len,c; char *buffer = 0; size_t bufsize = 0; ssize_t characters;
我正在尝试编写一个程序,该程序将 fork ,然后打开一个文件并执行它。它应该执行的文件称为子文件,并且它已经被编译。当我输入 ./child 时,它就会运行。但是,当我运行该程序时,它不会执行子程序
所以我有一个 C 程序,它接受命令输入,但偶尔我似乎会遇到某种内存错误。 我如何读取输入: static void parseLine(char* commandLine, Sequence* seq
我尝试在终端中传递命令行参数“ls -l/user/myuser”并在主目录中执行 execvp。 但不知何故,当我调试代码时,它给了我这个错误。 int main(int argc, char *a
已关闭。这个问题是 not reproducible or was caused by typos 。目前不接受答案。 这个问题是由拼写错误或无法再重现的问题引起的。虽然类似的问题可能是 on-top
我是一名优秀的程序员,十分优秀!