cd命令不能与execvp一起使用 [英] cd command not working with execvp
问题描述
#include <stdio.h>
#include <sys/types.h>
#include <string.h>
int main()
{
char *ip;
char *temp[10];
pid_t pid;
pid = fork();
if (pid == 0) {
int i = 0;
do {
gets(ip);
temp[0] = strtok(ip, " ");
while (temp[++i] != NULL) {
temp[i] = strtok(NULL," ");
}
pid_t pid2;
pid2 = fork();
if (pid2 == 0) {
execvp(temp[0], temp);
}
} while(strcmp(temp[0], "quit"));
if (!strcmp(temp[0],"quit")) {
return;
}
} else if (pid < 0) {
fprintf(stderr,"error in creating child");
} else if (pid > 0) {
wait(NULL);
}
}
此代码似乎无法与cd命令一起使用.我该如何解决它?我对OS的概念还很陌生,我们将不胜感激! :)
this code doesnt seem to work with cd command.how do i go about fixing it?i am fairly new to the concepts of OS and any help would be appreciated! :)
推荐答案
cd
作为可执行命令不存在. (并且它不能,因为进程只能更改自身的工作目录,而不能更改其父目录.)您将需要使用chdir()
系统调用将自己作为内置函数来实现cd
,类似于您的方式. ve已经实现了quit
.
cd
does not exist as an executable command. (And it cannot, because a process can only change the working directory of itself, not of its parent.) You will need to implement cd
yourself as a builtin, using the chdir()
system call, similar to the way you've already implemented quit
.
如果您打算实现它们,则还需要将其作为内置函数来实现,例如(例如,我不打算太详尽):
Other commands you will need to implement as builtins as well, if you plan to implement them, include (for example, I'm not trying to be thorough):
-
pushd
和popd
-
exit
,logout
,bye
等 -
fg
,bg
,jobs
和&
后缀 -
history
-
set
,unset,
export
pushd
andpopd
exit
,logout
,bye
, etcfg
,bg
,jobs
, and the&
suffixhistory
set
,unset,
export
这篇关于cd命令不能与execvp一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!