【发布时间】:2011-10-27 05:40:00
【问题描述】:
我正在尝试用 c 编写一个 shell,它除了 grep 之外大部分都可以工作。每当我在 shell 中发出 grep 命令时,它就不会输出任何东西。这是我用来创建新子进程然后在其中运行 execvp() 的部分代码。
dup2 中的文件描述符(fd_in 和 fd_out)作为参数传递给具有此代码的函数。最有趣的是,当我输入 'grep' 或 'grep --help' 时,它会像往常一样显示。我错过了什么吗?还是必须用 grep 做一些特别的事情?
这就是我的 shell 发生的情况:从 bash 运行时的最后一个命令输出。
--> grep
Usage: grep [OPTION]... PATTERN [FILE]...
Try `grep --help' for more information.
--> wc /etc/hosts
11 33 314 /etc/hosts
--> grep -i "perror" shell.c
-->
代码如下:
void
create_process(char *cmd_argv[], int fd_in, int fd_out, char *buffer_copy) {
/*Flag bit for Background processes*/
int FLAG = 0;
pid_t cpid;
int status;
int i = 0,j = 0;
/*Find the no. of arguments*/
while(cmd_argv[j] != NULL)
j++;
/*Set the flag bit*/
if(strcmp("&", cmd_argv[j-1]) == 0) {
FLAG = 1;
cmd_argv[j-1] = NULL;
}
//Create a child process
cpid = fork();
if (cpid == -1) {
perror("fork");
exit(EXIT_FAILURE);
}
//In the child...
if (cpid == 0) {
/*Checking if the file descriptors are already assigned*/
/*For stdin*/
if (fd_in != STDIN_FILENO) {
dup2(fd_in, STDIN_FILENO);
close(fd_in);
}
/*For stdout*/
if (fd_out != STDOUT_FILENO) {
dup2(fd_out, STDOUT_FILENO);
close(fd_out);
}
/*Run the cmd specified*/
status = execvp(cmd_argv[0], cmd_argv);
/*In case of errors*/
if(status < 0) {
perror("execvp ");
exit(1);
}
}
//In the parent...
else {
if(FLAG == 1) {
/*Find where the new bg process can be inserted*/
while(1) {
if (bgprocess[i].pid == 0) {
bgprocess[i].pid = cpid;
strcpy(bgprocess[i].cmd, buffer_copy);
break;
}
i++;
}
printf("[%d] : %s\n", cpid, cmd_argv[0] );
}
/*If not bg, wait for the process to exit*/
else
waitpid(cpid, NULL, 0);
}
}
【问题讨论】:
-
告诉我们你是如何设置
cmd_argv的。 -
char *cmd_argv[10];代码的前一部分用适当的参数填充它,然后是 NULL。
-
在你运行 grep 时显示你的 shell。此外,您可能应该关心 stderr 以及 stdin/stdout。
-
shell.c 中有
"perror"的实例吗?您的玩具外壳是否正确或完全处理引号 (")?当 grep 不匹配时,它的行为是什么?当它什么也没找到时,grep 的返回值是什么?您的外壳如何读取返回值值(提示:waitpid 的第二个参数)?