title: Simple shell implementation with C in linux, fork causing infinite loop
body: <p>I'm trying to implement a simple shell in C and my program is causing an infinite loop and creating a ton of new processes/printing <code>Myshell&gt;</code> before the user has a chance to enter in any commands. I can't seem to find a way to prevent this and if anyone could help that would be great! (Didn't paste the <code>#include</code> headers at top</p>
  
  <pre><code>int main(int argc, char *argv[]){
  
      char buffer[512];
      int *status;
      size_t nargs;
      pid_t pid;
      char delim[] = "\n";
  
      while(1){
  
          pid = fork();       
          if(pid){
  
          printf("Myshell&gt; ");
          fgets(buffer, 512, stdin);
          //parse(buffer, argv);
  
          argv[0] = strtok(buffer, delim);
          for(argc=1; argv[argc-1]; argc++){
              argv[argc] = strtok(NULL, delim);
          }       
  
          if(!strcmp(argv[0], "exit"))
              exit(0);
  
          printf("Waiting for child (%d)\n", pid);
          pid = wait(status);
          printf("Child (%d) finished\n", pid);
          exit(0);
          }else{
              if(execvp(argv[0], argv)){
                  printf("error");
                  exit(1);
              }else{
                  exit(0);
              }
          }
      }
  
      return 0;
  }
  </code></pre>