0

I am trying to implement my own shell and am experimenting with background jobs with using WNOHANG option in waitpid. But whenever I run a command in shell it just prints the output next to my prompt like this:

user@hostmachine:/.../$: [output]

Where am I going wrong? `

    while (1) {
      int childPid, status;

      // Display prompt and read input
      char *buffer = print_prompt();
      char *input = readline(buffer);
      check_and_free(buffer)

//    add input to readline history.
      add_history(input);
      time_t t;
      time(&t);
      add_history_time(ctime(&t));

      // Check for EOF.
      if (!input)
         break;

      parseInfo *result = parse(input);
      if (result == NULL) {
         goto free;
      }
      print_info(result);

      commandType *input_command = &result->CommArray[0];

//    execute builtin command in parent process
      int commType = isBuiltInCommand(input_command->command);
      if (commType == EXIT) {
         free_info(result);
         check_and_free(input)
         exit(0);
      }
      else if (commType != NO_SUCH_BUILTIN) {
         executeBuiltInCommand(input_command, commType, history_get_history_state());
      } else {
//       create a child process to execute command
         childPid = fork();
         if (childPid == 0) {
//          calls execvp
            printf("Executing child process...\n\n");
            executeCommand(input_command, result);
         } else {
            waitpid(childPid, &status, WNOHANG);
            if (status != 0) {
               printf("Error! Child exited with error code %d\n", WEXITSTATUS(status));
            }
         }
      }
      // Free buffer that was allocated by readline
      free:
      free_info(result);
      check_and_free(input)
   }
   return 0;
}

`

I tried to execute a job in background and for quick ones like "ls" it just prints the output next to my prompt!

amogh
  • 15
  • 6
  • Wait for the children to finish before exiting the program and the problem should be gone. – Ted Lyngmo Dec 13 '22 at 17:35
  • @TedLyngmo Isn't that just a normal process then? I am trying to execute it background so it doesn't hang up the shell. – amogh Dec 13 '22 at 17:38
  • Ok, but then you must also expect it to print things to the console whenever it likes, just like any program you run in the background. – Ted Lyngmo Dec 13 '22 at 17:38
  • 2
    There isn't really a way to do this. The same problem happens with standard shells. – Barmar Dec 13 '22 at 18:06
  • 1
    The best you can do is use `stty tostop`, which will cause background jobs to be suspended if they try to perform output. But you need to implement job control in your shell for this, by changing the process group of the background processes. – Barmar Dec 13 '22 at 18:07
  • @Barmar Ahh I see, I use fish as my main shell but when I tried with bash I get the same error. But the second part using "stty tostop" how do you do that? Like can you explain it in a code snippet by editing my code? – amogh Dec 13 '22 at 18:26
  • @amogh No, ask a new question about that after having given it a try yourself for an hour. – Ted Lyngmo Dec 13 '22 at 18:28
  • See the code snippet in [this question](https://stackoverflow.com/questions/48533559/no-sigttou-when-background-process-writing-to-stdout) – Barmar Dec 13 '22 at 18:31

0 Answers0