【发布时间】:2014-05-30 22:10:06
【问题描述】:
我正在尝试创建 ls 命令。首先,如果我输入“ls”,则代码不起作用,只有当我输入完整路径时它才起作用。其次,它没有在 exevcp() 之后循环。为什么? 谢谢。
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<unistd.h>
int main(void){
char *line;
char *args[32];
memset(args, 0, sizeof(args));
while (1){
line = (char*)malloc(1024);
printf("$ ");
fgets(line,1024,stdin);
args[0] = strtok(line, " ");
args[1] = strtok(NULL, " ");
execvp(args[0], args);
perror("execvp");
}
}
【问题讨论】:
-
it's not looping after the exevcp(). why?- exec() 系列函数用新的过程映像替换当前过程映像。所以execvp()之后,你的初始进程就不存在了。