Thread: Expected Behavior? : Terminal stops a looped program sequence.

  1. #1
    Bored Programmer
    Join Date
    Jul 2009
    Location
    Tomball, TX
    Posts
    428

    Expected Behavior? : Terminal stops a looped program sequence.

    Hey I was just messing around with g++ on mac to make programs that would execute other terminal commands. I ran into a situation that I don't quite understand and was curious of this is expected behavior.

    Shared Lib:
    Code:
    void printAllArgs(int argc, char *argv[]){
      using std::cout;
      using std::endl;
    
      for(int i = 0; i < argc; i++){
        cout<<i<<" => "<<argv[i]<<endl;
      }
    }
    sweet.exe
    Code:
    #include <iostream>
    #include "lib.h"
    
    int main( int argc, char *argv[] ){
      printAllArgs(argc, argv);
      system("./dude What does mine say?");
      return 0;
    }
    dude.exe
    Code:
    #include <iostream>
    #include "lib.h"
    
    int main( int argc, char *argv[] ){
      printAllArgs(argc, argv);
      system("./sweet What does mine say?");
      return 0;
    }
    Expected: To run indefinitely until I killed the terminal thread.

    Saw: After about 30 seconds the terminal output stops.
    Virtual reality hello world http://www.rodneybrothers.com/vr/vrh...rld/index.html in html and javascript.
    Viewable with dodocase, google cardboard, OR, and other compatible VR gear.

  2. #2
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    When you call system, the current process is suspended and a new process is launched, therefore after the first two moments you should see these two processes running if you use a tool like ps:

    sweet
    dude

    Each process spawns the other in turn, so after a few more moments, you should be able to verify the following processes running (e.g. using ps or pstree):

    sweet
    dude
    sweet
    dude
    sweet
    ...


    Most operating systems have a finite limit on processes, so I would expect the operating system to eventually refuse to let new processes be created. You should check the return status of system to see when this is happening. Currently you are ignoring the return value, so when system fails, the last process just exits (and then all the other ancestor processes will exit in turn).

    You could also introduce a short delay in your program to make this experiment easier to see. That way you could use a tool like pstree to see how the process hierarchy grows with time. As it is now, your program will probably create processes too quickly to observe interactively.

  3. #3
    Bored Programmer
    Join Date
    Jul 2009
    Location
    Tomball, TX
    Posts
    428
    Thanks for the response and suggestions on how to analyze it. I was surprised because my assumption was that each execution would be done asynchronously rather than synchronously waiting for the system execution to finish. Ultimately I should be avoiding this usage of system anyways so I will definitely look into some more research about how programs can kick off other programs.
    Virtual reality hello world http://www.rodneybrothers.com/vr/vrh...rld/index.html in html and javascript.
    Viewable with dodocase, google cardboard, OR, and other compatible VR gear.

  4. #4
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    For doing the above asynchronously you should look into fork for Linux/POSIX and CreateProcess for Windows.

  5. #5
    Bored Programmer
    Join Date
    Jul 2009
    Location
    Tomball, TX
    Posts
    428
    Thanks again for your information. I tried using the fork and exec commands and ran into some issues that require prompts. So far I have had to settle for letting the user go through these prompts. Which in this case is probably the best, but I was curious about if there was a way to automate the prompt entries as well.

    Essentially right now I have this code, which still hasn't avoided the use of system as the fork wasn't showing me the prompts:

    Code:
    #include <iostream>
    #include <fstream>
    
    void createEntry(){
      std::ofstream newFile;
      newFile.open("index.js");
      newFile << "require(\"babel-core/register\");\n";
      newFile << "var program = require(\"./run\");\n";
      newFile << "program.default()\n";
      newFile.close();
      newFile.open("run.js");
      newFile << "export default function run() {\n";
      newFile << "  /* Start your code here */\n";
      newFile << "  console.log(\"hello es6 modules\")\n";
      newFile << "}\n";
      newFile.close();
      newFile.open(".babelrc");
      newFile << "{\n";
      newFile << "\"presets\": [\"es2015\"]\n";
      newFile << "}\n";
      newFile.close();
    }
    
    int main(int argc, char *argv[]){
      system("npm init");
      system("npm i babel-core -S");
      system("npm i babel-preset-es2015 -S");
      createEntry();
      return 0;
    }
    The first command `npm init` basically sets up a npm project and requires 8 prompts to finish any ideas how to automate the inputs through that?
    Virtual reality hello world http://www.rodneybrothers.com/vr/vrh...rld/index.html in html and javascript.
    Viewable with dodocase, google cardboard, OR, and other compatible VR gear.

  6. #6
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Is there a reason you aren't using bash, or perl, or anything simpler? C/C++ really isn't streamlined for these tasks.

    system() isn't really how you want to handle this.
    Last edited by whiteflags; 01-25-2016 at 06:56 AM.

  7. #7
    Bored Programmer
    Join Date
    Jul 2009
    Location
    Tomball, TX
    Posts
    428
    Is there a reason you aren't using bash, or perl, or anything simpler? C/C++ really isn't streamlined for these tasks.
    Particularly I have been using JavaScript, Ruby, Sass, ect for so long I wanted to come back to where I learned to program and brush up on some c++. I find it hard to program something pointlessly so I put my efforts towards automating some of my commonly done tasks. I could have just as easily accomplished this using the node v8 engine as well. I just wanted to remember what c++ was like xD.

    system() isn't really how you want to handle this.
    Correct I did read about some of the inherent issues with system. In this case it would be if someone were to replace what the task `npm` was with some other potentially malicious program then I would be granting open access.

    None the less for the sake of learning is there a known way to accomplish automating user input using something from the fork/exec family of commands?
    Virtual reality hello world http://www.rodneybrothers.com/vr/vrh...rld/index.html in html and javascript.
    Viewable with dodocase, google cardboard, OR, and other compatible VR gear.

  8. #8
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    Quote Originally Posted by Lesshardtofind View Post

    The first command `npm init` basically sets up a npm project and requires 8 prompts to finish any ideas how to automate the inputs through that?
    You might look into a tool called "expect" for this sort of problem. In the simplest example, you wait for specific prompts and then give them to the other program:

    6 Expect Script Examples to Expect the Unexpected (With Hello World)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program Stops Responding.
    By KittenAqua in forum C++ Programming
    Replies: 5
    Last Post: 12-16-2011, 05:26 PM
  2. MessageBox stops program flow
    By Ducky in forum Windows Programming
    Replies: 3
    Last Post: 06-20-2011, 10:40 PM
  3. program stops before EOF
    By doia in forum C Programming
    Replies: 2
    Last Post: 03-22-2010, 12:10 PM
  4. Program stops executing
    By bargomer in forum C++ Programming
    Replies: 16
    Last Post: 04-26-2008, 12:05 PM
  5. Checking to see if the value has looped?
    By Trauts in forum C++ Programming
    Replies: 12
    Last Post: 11-18-2002, 07:59 PM