【发布时间】:2015-12-17 08:05:53
【问题描述】:
请注意,这是一项家庭作业。目标是创建一个 shell 接口。
问题是为什么我在终端输入命令时看不到 execvp() 的结果?
代码如下:
int getcmd(char *prompt, char *args[], int *background)
{
int length, i = 0;
char *token, *loc;
char *line;
size_t linecap = 0;
printf("%s", prompt);
length = getline(&line, &linecap, stdin);
if (length <= 0) {
exit(-1);
}
// Check if background is specified..
if ((loc = index(line, '&')) != NULL) {
*background = 1;
*loc = ' ';
} else
*background = 0;
while ((token = strsep(&line, " \t\n")) != NULL) {
for (int j = 0; j < strlen(token); j++)
if (token[j] <= 32)
token[j] = '\0';
if (strlen(token) > 0)
args[i++] = token;
}
return i;
}
以下是我的主要功能:
int main()
{
char *args[20];
int bg;
while(1){
bg = 0;
int cnt = getcmd("\n>> ", args, &bg);
pid_t child_pid = fork();
//for (int i = 0; i < cnt; i++)
// printf("\nArg[%d] = %s", i, args[i]);
// a negative number will be returned only
// if the forking was unsuccessful i.e
// no child process is cloned from the original
// process
if(child_pid < 0){
fprintf(stderr, "Fork Failed");
return 1;
}
// the return value for a successful fork of a child is 0
else if(child_pid == 0){
execvp(args[0], args);
}
// this is the parent process here
else {
int status;
if (bg == 1){
printf("\nBackground enabled..\n");
getcmd("\n>> ", args, &bg);
}
else{
printf("\nBackground not enabled \n");
waitpid(child_pid, NULL, 0);
}
}
}
}
当我运行我的代码时,它会编译并给我提示符(>>)。当我运行诸如ls 之类的命令时,它只打印未启用背景(因为我没有指定&)。但是,如果我在另一个终端上执行ps 命令,我会看到有一个新进程正在运行。我忽略了什么吗?当我将 ls 作为参数提供给 execvp() 系统调用时,为什么我看不到它的结果?
【问题讨论】:
-
是否
getcmd()NULL-terminateargs? -
在调用
execvp()之后立即调用perror("exec failed"); exit(1);可能会有所帮助。 -
Yes getcmd() null 终止并且 perror("exec failed") 返回:exec failed: Bad address
-
“地址错误”?什么时候做“ls”?真的吗?你在哪个平台上观察这个?
-
是的.. Mac OSX(更具体地说:El Capitan)
标签: c macos process operating-system fork