Thread: Best Way to Start a Second Program?

  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868

    Best Way to Start a Second Program?

    Inside my program, I need to call a second program ("solver.exe") and then the first program can pick back up, right where it left off. This is done about once each minute.

    Right now I'm using:

    Code:
    //my program's other code up here:
    for(i = 0; i < BigNumber; i++)  {
       //miscellaneous other code of mine
       system("solver.exe < inputFile");
       //more code of mine
    }
    This works, but I see a lot of other options for this, in C.

    Would another way be faster, and still reliable?

    This is part of my Sudoku analysis program, and the run time is *absolutely HUGE* - (many years, on the fastest hardware, running multiple work units which break the job into smaller pieces)

    Thanks.

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    I'm not up so much on Windows architecture, but I suspect calling system() has a lot of overhead, starting up a new process, loading the executable (don't know if it's cached or not), and so on.

    Is there a way to load the exe into storage and keep it there, and then just call it without all the overhead of starting a new process over and over?
    Last edited by Dino; 08-12-2009 at 08:31 PM. Reason: typo
    Mainframe assembler programmer by trade. C coder when I can.

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Dino View Post
    Is there a way to load the exe into storage and keep it there, and the just call it without all the overhead of starting a new process over and over?
    Or just leave it sleeping and add a little IPC to both programs. Actually I think that is what you meant.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Yes, Dino, the executable is cached.

    MK, what is ISP?

  5. #5
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Adak View Post
    MK, what is ISP?
    It's dyslexic for "Inter Process Communication", silly. Like (for example) you thread a socket server into one program and a client into the other so they can send instructions back and forth. So solver.exe is started only once, then sleeps waiting for a call. This way, you don't have to keep reloading it. On linux this would be parallel to a "daemon service".
    Last edited by MK27; 08-12-2009 at 09:24 PM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  6. #6
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    This is something that happens about one a minute and we're talking about how to speed it up by a few hundred microseconds?
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by brewbuck View Post
    This is something that happens about one a minute and we're talking about how to speed it up by a few hundred microseconds?
    I brought this up because I was having trouble with the program crashing after about 30 such calls.

    My first thought was "it must be something to do with using system()" over and over. That's what prompted this thread.

    After sleeping on it, I thought "funny, it crashes just about how DOS used to crash when it ran out of file handles..."

    Which led me to the startling discovery that *somehow*, AFTER the data file was closed, and the system("otherprogram") had run, my program was *again* writing data to the file, and that data was good!! <ay carumba!> I had never seen that kind of run time behavior. It was just losing one file handle in the process. After adding the file open block of code, after the call to system(), it is all running fine.

    Right now, I have no idea how to code up MK's suggestion, but it sounds more elegant than calling system(), repeatedly. My "big idea" is to integrate these two programs, but anything that can help in the mean time, is very welcome.

    I'll be back for more substantial algorithmic advice, soon.
    Last edited by Adak; 08-13-2009 at 01:56 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. BOOKKEEPING PROGRAM, need help!
    By yabud in forum C Programming
    Replies: 3
    Last Post: 11-16-2006, 11:17 PM
  2. How to start program from subfolder
    By trancedeejay in forum C Programming
    Replies: 2
    Last Post: 04-01-2006, 03:39 PM
  3. Start program before login with WinXP
    By Icewind2003 in forum Tech Board
    Replies: 3
    Last Post: 02-12-2003, 04:50 PM
  4. Start up program
    By Breetai in forum Windows Programming
    Replies: 2
    Last Post: 01-11-2003, 01:12 PM
  5. Start a program
    By FunkeeMunkee in forum C++ Programming
    Replies: 1
    Last Post: 08-26-2001, 07:18 PM