Thread: Programs launched from C don't stay open after launcher closes

  1. #1
    Registered User
    Join Date
    Oct 2021
    Posts
    10

    Programs launched from C don't stay open after launcher closes

    I'd like to be able to launch a program from this C program and have that launched program stay open after this C program closes.

    I've tried various solutions, and none seem to do the above. Here is some code that at least shows what's going on, and I know it doesn't work, but I wanted to at least put some effort code in here that might help.

    Code:
    #include <unistd.h>
    #include <sys/types.h>
    #include <sys/wait.h>
    #include <stdio.h>
    #include <stdlib.h>
    
    int main () {
    
        pid_t pid = -1;
    
        printf("\nParent is forking a child.");
    
        pid = fork();
    
        if(pid < 0){
    
            perror("fork");
            exit(1);
    
        }else if(pid == 0){
    
            printf("Starting child process...\n");
            printf("If I were to press ctrl-c here, both programs would close, but I want the launched program to stay open.\n");
    
            execlp("/usr/bin/xterm", "xterm", "-e", "top", NULL);
    
            // Code will never reach this point
    
        }else{
    
            printf("Parent is waiting for child id %d to complete...\n", pid);
    
            // Wait for procress to complete
            wait(NULL);
    
            printf("The child process is complete and has terminated!\nBut this program had to stay open the entire time.");
        }
    
        printf("Parent is now exiting...\n");
    
        return 0;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,815
    Just delete the call to wait(NULL);
    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.

  3. #3
    Registered User
    Join Date
    Oct 2021
    Posts
    10
    Quote Originally Posted by Salem View Post
    Just delete the call to wait(NULL);
    Works perfect now. Thank you.

    SOLVED!

  4. #4
    Registered User
    Join Date
    Oct 2021
    Posts
    10
    The child now remains open after the parents closes, but I noticed that in a loop, the child still dies if the parent is killed via ctrl-c.

    How can I keep the child alive regardless of how the parent is killed?

    Code:
    #include <stdlib.h>
    #include <string.h>
    #include <unistd.h>
    #include <sys/wait.h>
    #include <stdio.h>
    #include <signal.h>
    
    /* Compile and run with:
    
    gcc -Wall run.c -o run -lncursesw && run 
    
    */
    
    void handler(int signum) {
    
        write(STDOUT_FILENO, "\nExiting gracefully.", 20);
        printf("\n");
        exit(1);
    
    }
    
    void execute(char **arg) {
    
         pid_t  pid;
         int exec;
    
         if ((pid = fork()) < 0) {
    
              exit(1);
    
         } else if (pid == 0) {
    
              exec = execvp(*arg, arg);
    
              if (exec < 0)
                   exit(1);
    
         } else {
    
            //while (waitpid(-1, NULL, WNOHANG) != pid);
    
         }
    
    }
    
    #define MAX_LINE 256 
    
    int main(int argc, char* argv[], char*envp[]) {
    
       char text[MAX_LINE];
       char *args[MAX_LINE/2 + 1]; // Calculate max elements possible
    
       signal(SIGINT, handler);
       signal(SIGKILL, handler);
       signal(SIGTERM, handler);
    
       do {
    
          printf("\nShell: ");
          fgets(text, MAX_LINE, stdin);
    
          text[strlen(text) - 1] = '\0'; // strip return carriage at end of text
          args[0] = strtok(text, " ");
    
          for (int i = 0; args[i] != NULL; )
             args[++i] = strtok(NULL, "\"' " ); // consider quotations too
    
          execute(args);
    
       } while (strcmp(text, "exit") != 0);
    
       return 0;
    }

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,815
    You could do the double fork.
    Code:
    if ( fork() == 0 ) {
      if ( fork() == 0 ) {
        execvp(*arg, arg);
      } else {
        _exit(0);
      }
    }

    daemon(3) - Linux manual page
    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.

  6. #6
    Registered User
    Join Date
    Oct 2021
    Posts
    10
    Quote Originally Posted by Salem View Post
    You could do the double fork.
    Code:
    if ( fork() == 0 ) {
      if ( fork() == 0 ) {
        execvp(*arg, arg);
      } else {
        _exit(0);
      }
    }

    daemon(3) - Linux manual page
    Nice. I haven't thought of forking within a fork. I'll try it out tomorrow.

  7. #7
    Registered User
    Join Date
    Feb 2019
    Posts
    892
    Notice that exec?() WILL return in case of error.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Why wont this program stay open?
    By Xiotis in forum C++ Programming
    Replies: 3
    Last Post: 10-16-2013, 12:41 AM
  2. Why does my program only stay open like this?
    By rayne117 in forum C++ Programming
    Replies: 3
    Last Post: 09-28-2010, 01:36 AM
  3. Window Wont Stay Open
    By Wes in forum C++ Programming
    Replies: 4
    Last Post: 08-26-2004, 12:50 AM
  4. Forcing a program to stay open.
    By johnnabn in forum C++ Programming
    Replies: 2
    Last Post: 02-23-2003, 10:32 AM
  5. program open and closes
    By Musicdip in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 06-27-2002, 04:34 PM

Tags for this Thread