Thread: fork() background execution hang

  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    8

    fork() background execution hang

    Hey C Boarders,
    I'm coding a 'simplistic' shell environment and I'm currently implementing background execution. I know that after you have successfully fork()ed that in order to have background execution you just have the parent not wait. Well when I compile and run, everything prints out as it should, but the prompt never returns and it seems as if the program hangs. I can however, type in another command in the "hang space" and it accepts and prints it out like it normally would with a command prompt there. If quit and do it again with either the same or different command that uses fork() again and hit enter during the hang, the prompt shows up.

    I've tried fflushing stdout, stdin, and no luck. Has anyone encountered this problem before? I've added a command function (which uses the spawn_proc()) and the spawn_proc(..) function itself as a reference.



    Thanks in advance!



    Code:
    /* Clear current screen display
    *   param  @ void
    *   return @ void
    *  This function clears the current shell display window.
    */
    void clear_scrn(void)
    {
      char* arg_list[] = {NULL};
      spawn_proc("clear", arg_list);
    }
    
    
    /* Spawning a new process with fork() and exec()
    *   param  @ name - name of the command to be executed
    *   param  @ args - the arguments that follow the executing command
    *   return @ void
    *  This function spawns a process using fork() and exec()'s in the child using the given command, and args.
    */
    void spawn_proc(char* name, char** args)
    {
      int status;
      pid_t child = fork();
    
      if(child == 0) // child
        {
          execvp(name, args);
          fprintf(stderr, "Invalid command.\n");
        }
    
      else if(child < 0) // error
        {
          fprintf(stderr, "fork err.\n");
          exit(1);
        }
    
      //else // parent
         //waitpid(child, &status, 0); // no waitpid() for background execution
    
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Where's your code for displaying the prompt and calling clear_scrn ?

    Maybe you got a prompt, then it was cleared away by the forked process.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. fork() a background task
    By HighSeraphim in forum C Programming
    Replies: 1
    Last Post: 11-04-2009, 10:04 PM
  2. fork(), exit() - few questions!
    By s3t3c in forum C Programming
    Replies: 10
    Last Post: 11-30-2004, 06:58 AM
  3. Daemon programming: allocated memory vs. fork()
    By twisgabak in forum Linux Programming
    Replies: 2
    Last Post: 09-25-2003, 02:53 PM
  4. Background Execution
    By raj in forum Windows Programming
    Replies: 8
    Last Post: 04-15-2003, 12:44 PM
  5. Background Execution
    By raj in forum C Programming
    Replies: 1
    Last Post: 04-09-2003, 02:48 AM

Tags for this Thread