cd命令不能与execvp一起使用 [英] cd command not working with execvp

查看:250
本文介绍了cd命令不能与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):

  • pushdpopd
  • exitlogoutbye
  • fgbgjobs&后缀
  • history
  • setunset, export
  • pushd and popd
  • exit, logout, bye, etc
  • fg, bg, jobs, and the & suffix
  • history
  • set, unset, export

这篇关于cd命令不能与execvp一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆