#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);
}
}
Run Code Online (Sandbox Code Playgroud)
这段代码似乎不适用于cd命令.我是否要修复它?我对操作系统的概念相当新,任何帮助将不胜感激!:)
dus*_*uff 11
cd不存在作为可执行命令.(并且它不能,因为进程只能更改其自身的工作目录,而不能更改其父级.)您需要cd使用chdir()系统调用将自己实现为内置,类似于您已经实现的方式quit.
如果你计划实现它们,你还需要实现内置的其他命令包括(例如,我不是要彻底):
pushd 和 popdexit,logout,bye,等fg,bg,jobs,和&后缀historyset, unset, export| 归档时间: |
|
| 查看次数: |
7029 次 |
| 最近记录: |