Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/61.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 创建一个简单的shell for my循环会为每个要读入的单词创建一个单独的进程_C_Linux_Shell - Fatal编程技术网

C 创建一个简单的shell for my循环会为每个要读入的单词创建一个单独的进程

C 创建一个简单的shell for my循环会为每个要读入的单词创建一个单独的进程,c,linux,shell,C,Linux,Shell,因此,我试图构建一个简单的shell,当我执行下面的代码时,它将for循环中的每个循环视为一个单独的进程,我不知道为什么 问题: 1) 当我使用for循环附加每个单词以从scanf()读入时,程序将每个单词视为一个单独的进程。我想这与wait()有关,但我该如何解决这个问题呢 2) 那为什么会这样呢 3) 那么,当试图退出时,我必须为每个新的子进程键入exit,这是与问题1相关的还是一个单独的问题 #include <stdio.h> #include <stdlib.h>

因此,我试图构建一个简单的shell,当我执行下面的代码时,它将for循环中的每个循环视为一个单独的进程,我不知道为什么

问题:

1) 当我使用for循环附加每个单词以从scanf()读入时,程序将每个单词视为一个单独的进程。我想这与wait()有关,但我该如何解决这个问题呢

2) 那为什么会这样呢

3) 那么,当试图退出时,我必须为每个新的子进程键入exit,这是与问题1相关的还是一个单独的问题

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>

int main(){
int  end = 1;
char *argv[100];
char progpath[30];
char *path = "/bin/";
pid_t pid;
int loop = 0;

while(1 && end !=0){ //while process has no error and user did't type exit
  int argc=0;
  printf("user>>> ");    //user input
  scanf("%s",string);
  if(strcmp(string,"exit") == 0){   // exits process when user types "exit"
    end = 0;
    exit(0);
  }

  char *token;
  token = strtok(string," ");
  while(token != NULL){
    argv[argc] = token;
    token = strtok(NULL," ");
    argc++;
  }
  argv[argc] = NULL;


  int i;
  for(i = 0; i<argc;i++){
    printf("\n%s\n",argv[i]);
  }

  strcpy(progpath,path);
  strcat(progpath,argv[0]);


  int pid = fork();
  if(pid == 0){
    if(end == 0){
      exit(0);
    }
    else{
      execv(progpath,argv);
      fprintf(stderr, "child could not exicute\n");
    }
  }

  else{
    wait(NULL);
    }
  }
  loop++;



}
return(0);
}
在这里:

从标准输入中读取一个空格分隔的字符串到数组
字符串
。稍后您将尝试在空白处标记该字符串,但其中肯定不会有任何空白。这仅仅是一个词,你最终将其视为一个完整的命令。稍后,你回过头来,读下一个单词,并对它进行同样的处理。你的程序正在做你告诉它要做的事情


如果你想一次读一整行,我建议你使用
fgets()
,或者
getline()
。后者自2008年起由POSIX标准化,但不是C标准的一部分。

您的代码逻辑非常不清楚,例如
while(1 | | end==0)
while(asorry,我必须小心复制了以前的编辑,现在已更新)。
user>>> ls -l

ls
ages.c.save  Desktop       hlepshell    OperatingSystems   shell
a.out        Documents     infile.txt   OS hw1         shell.c
arrays.c~    Downloads     makefile~    OS hw1 errors.odt  shell.c~
bfgminer     hello.html    Music        Pictures           shell.tar.gz
bin      helpshell     newshell     Public         Templates
classScraper.py  helpshell.c   newshell.c   python_games       Videos
cs235        helpshell.c~  newshell.c~  scheduler.py
user>>> 
-l
child could not exicute
user>>> hello world

hello
child could not exicute
user>>> 
world
child could not exicute
user>>> exit
user>>> exit
user>>> 
world
child could not exicute
user>>> exit
user>>> exit
user>>> exit
*program ends*
    scanf("%s",string);