【发布时间】:2017-08-03 00:46:31
【问题描述】:
大家好,我正在一个 shell 中创建一个批处理命令函数,该函数从一个 txt 文件中读取并将其通过管道传递给一个子进程以执行。
我遇到了执行问题。我怀疑它与空终止符有关。如果我使用 L 和显式 (char*)NULL 执行,则执行运行。如果我 execvp(argIn[0],argIn) 没有运行并返回-1。如果我使用显式 (char*)NULL 执行 execvp,则会收到错误,无法将 char* 转换为 char* 常量*。我在某处读到它可能是 g++ 编译器给了我错误,但 gcc 编译器不会给我错误。现在它不会用 gcc 编译,所以我不确定这是否属实。但无论如何它不应该需要显式终止符。我不确定我存储的 '\0' 是否正在传递给 exec 权限。当我将它传递给其他函数时它会检查出来,所以也许这不是解决方案。
其次,我的 for 循环不会多次执行,我认为这与第一个解决方案有关。我可以让 execl 使用索引进行分叉,但我无法在下次通过时递增索引以指向正确的标记,因为孩子应该清除我的索引,对吗?
无论如何,为了找出问题所在,我们已经挖掘了 3 周。我的任务失败了。很可能会不及格。我不知道还能尝试什么。因此,我将不胜感激。
我的问题是为什么 exec 函数不会执行程序?我正在传递 execvp(program name, program name, option, option, '\0') 但没有得到结果。
或
execl(program name, program name[index], option[index+1], option[index+1], (char*)NULL) 并得到结果。他们似乎都遵循参数,但只有一个给了我结果。
#include<string.h>
#include<iostream>
#include<ctype.h>
#include<stdlib.h>
#include<stdio.h>
#include<sys/types.h>
#include<sys/wait.h>
#include<unistd.h>
using namespace std;
int makearg(char s[], char**args[]);
int main(int argc, char *argv[])
{
char **argIn;
int argCount;
int pos = 0;
char str[500];
pid_t pid = fork();
for(int i = 0; i < 4; i++)
{
if(pid == 0)
{
while(fgets(str, 500, stdin) != NULL)
{
cout << "String loaded: " << str;
argCount = makearg(str, &argIn);
execvp(argIn[0],argIn); //nothing exec
// execl(argIn[0],argIn[0],argIn[1],argIn[2],(char*)NULL); //exec but requires index.
else if(pid < 0)
{
perror("fork() error");
exit(-1);
}
else if(pid > 0)
{
cout << "Parent waiting" << endl;
wait(NULL);
}
}
return 0;
}
int makearg(char s[], char**args[])
{
int counter = 1;
int tokenLen = 1;
int i = 0;
int j = 0;
int k = 0;
char arg1[50];
char * arg2;
strcpy(arg1, s);
//Count white space.
while (arg1[j] != '\0')
{
if (arg1[j] == ' ' || arg1[j] == '\0' || arg1[j] == '\n')
{
counter++;
}
j++;
}
//Allocate the number of rows to be pointed to.
args[0] = (char**) malloc(counter + 1);
if(args[0] == NULL)
exit(1);
//Allocate the size of the c string arrays
j = 0;
while(arg1[j] != '\0')
{
if (arg1[j] == ' ' || arg1[j] == '\0' || arg1[j] == '\n')
{
(*args)[i] = (char*)(malloc(tokenLen));
if((*args)[i] == NULL)
exit(1);
tokenLen = 0;
i++;
}
j++;
tokenLen++;
}
(*args)[i] = (char*)(malloc(tokenLen));
if ((*args)[i] == NULL)
exit(1);
//reset values
i = 0;
j = 0;
//Set arg2 to point to args row head. Transfer values from arg1 to arg2.
arg2 = ((*args)[i]);
while(arg1[j] != '\0')
{
if (arg1[j] != ' ' && arg1[j] != '\0' && arg1[j] != '\n')
{
arg2[k] = arg1[j];
k++;
}
else
{
arg2[k] = '\0';
i++;
k = 0;
arg2 = ((*args)[i]);
}
j++;
}
arg2[k] = '\0';
if (counter < 1)
{
return -1;
}
return counter;
}
【问题讨论】:
-
什么。是你的问题吗?
-
问题是。给定所描述的参数,为什么 exec 函数不起作用。
-
会发生什么而不是“某事执行”?
-
另外你为什么不知道你使用的是哪种语言?
-
@cba1067950 这不是拖钓!没有语言 c/c++,选择一个认真!