Executing background processes

Status
Not open for further replies.
I'm writing a shell in C for POSIX which should be able to handle background processes similar to bash. I've run into a problem getting background processes to work. The general idea was to fork a new process and execute whatever command the user entered and then do a waitpid with WNOHANG, but the background process writes to the terminal, which it isn't supposed to do. I want background process to run without interacting with terminal.

So to disable access to the terminal to the background process, I'm handling like so:
Code:
 if(pid == 0 && bg == 1)            /* Child process */
            {

              //signal(SIGTTIN, term_handler);
              //setsid();
              close(STDIN_FILENO);
              close(STDOUT_FILENO);
              close(STDERR_FILENO);
              x = open("/dev/null", O_RDWR);
              dup(x);
              dup(x);

              execvp(args[0], args);
              printf("Command not found\n");

              exit(1);


            }

This code does indeed take terminal away from the process (not sure if it's the right way though) but it also kills the process which I don't want. Basically if I do: emacs& in my shell, it'll make emacs a zombie process, I want it running on the background instead.

Now I've looked into what might be causing this (been spending long hours googling), and I believe it is the SIGTTIN signal. Wiki also tells me daemon processes are not supposed to get this signal, but I believe my process is indeed getting it because when I allow STDERR open, the process writes to my terminal: standard input is not tty.

So my question basically is how to make a background process that runs but does not interact with the terminal. If it means handling the SIGTTIN signal, then how would I do that? if it's another way completely and I'm just way off then please explain how to do it. If you've read this far, you're awesome, if you provide some help, you're even more awesome.

I've also looked into process groups, different process groups for foreground and background processes but don't know how it works exactly. After hours of google I have a lot of abstract information in my head with many ideas of what may be wrong but nothing concrete. I've tried many different implementations and so far all of them have problems.


I think the routine above should be enough but here's the entire code on a minishell version of my main program just in case:

Code:
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <fcntl.h>

const int BG_PGID = 9999;
int go = 0;

void printPrompt()
{
  printf("[SHELL]> ");
}

void zombie_handler(int signum)
{

}

int main(int argc, char** argv)
{
  char command[1024] = "";
  char in;
  int i = 0;
  int bg = 0;
  int pid;
  int status;
  char *args[5];
 
  int pid2;
  int k = 0;
  int x;
  
  signal(SIGCHLD, zombie_handler);

  printPrompt();
  while(1)
    {
      in = getchar();
      switch(in)
        {
        case '\n':
          go = 0;
          if(strcmp(command, "exit") == 0)
            exit(0);
          
          if(command[strlen(command)-1] == '&')
            {
              bg = 1;
              command[strlen(command) - 1] = '\0';
            }
          args[0] = (char*)malloc(strlen(command)+1);
          strcpy(args[0], command);
          args[1] = NULL;

          
          pid = fork();                      // Fork new process
          /*                                    //uncomment to wait without hold for background child. 
          if(pid != 0 && bg == 1)
            {
              waitpid(-1, &status, WNOHANG);
            }
          */
          if(pid == 0 && bg == 1)            /* Child process */
            {
              close(STDIN_FILENO);
              close(STDOUT_FILENO);
              close(STDERR_FILENO);
              x = open("/dev/null", O_RDWR);   // Redirect input, output and stderr to /dev/null
              dup(x);
              dup(x);
              execvp(args[0], args);       // Execute background process - WILL GO INTO ZOMBIE/DEFUNCT STATE -- 
              exit(1);
            }  
          if(pid == 0 && bg != 1)
            {
              execvp(args[0], args);
            }
          else if(bg != 1)
            {
              waitpid(pid, &status, WUNTRACED); 
            }

          printPrompt();
          free(args[0]);
          memset(command, '\0', strlen(command));
          bg = 0;
          break;

        default:
          strncat(command, &in, 1);
        }
      
    }

  
}
 
Thanks for suggestion, I'll definitely check it out.

After hours of Google I think I may have finally solved it, a single line of code no less (don't I feel stupid). Thanks to the great great great etc.. -- > http://www.win.tue.nl/~aeb/linux/lk/lk-10.html .
"The Linux Kernel" website I found in the vast sea of information had my answer.

Code:
 if(pid == 0 && bg == 1)            /* Child process */
            {
              setpgid(pid, 0);                 //creates a new process group leader pid
            

              execvp(args[0], args);
              printf("Command not found\n");

              exit(1);


            }

I only had to make a new process group for my background process.

Not celebrating yet but I've been testing it so far and it's working as expected.
 
Status
Not open for further replies.