4 Replies - 794 Views - Last Post: 03 December 2014 - 01:42 PM Rate Topic: -----

#1 key01023   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 30-September 14

use c++ to call programme and terminate conditionally

Posted 03 December 2014 - 12:12 PM

Currently, I have a c++ program to call an external program, wait for it to finish, and return the string that was supposed to print on the screen by the external program;

string result_now=exec("./CCLS_to_akmaxsat "+softer_filename);
    
    std::string exec(std::string command) {
        //cout << "\n\nEvaluating command: " << command << "\n\n" << endl;
        const char* cmd = command.c_str();
        FILE* pipe = popen(cmd, "r");
        if (!pipe) return "ERROR";
        char buffer[256];
        std::string result = "";
        while(!feof(pipe)) {
        	if(fgets(buffer, 256, pipe) != NULL)
        		result += buffer;
        }
        pclose(pipe);
        return result;
    }


And here is the output:
    c This is the CCLS_to_akmaxsat solver, Version: MAXSAT EVALUATION 2014 (2014.03.28)
    c Many thanks to the akmaxsat team!
    c start CCLS
    c stop CCLS
    c start akmaxsat
    c initialized bestCost to 924115047
    o 924115047
    c first lower bound: 850209662
    c 3406 branches 129 propagates
    c total generalized unit propagation = 6941, success = 49.07%
    s OPTIMUM FOUND
    c Optimal Solution = 924115047
    v -60 -58 -56 -54 52 50 48 46 44 42 40 38 36 34 32 30 28 -26 -24 -22 -20 -18 -16 -14 -12 -10 -8 -6 -4 -2 21 23 -25 -27 -29 -31 -33 -35 -37 -39 -41 -43 -45 -47 -49 -51 -53 -55 -57 59 19 17 15 13 11 9 7 5 3 1
    c stop akmaxsat


However, if I see that the lower bound is larger than some value. I no longer want to spend time waiting to program to finish, rather I would terminate the program and provide some information back to my main c++ solver. How do I achieve this? Or if you have some very related tutorials, please don't hesitate to tell me. Thank you.

This post has been edited by modi123_1: 03 December 2014 - 12:18 PM
Reason for edit:: please use the 'code' button in the editor


Is This A Good Question/Topic? 0
  • +

Replies To: use c++ to call programme and terminate conditionally

#2 modi123_1   User is offline

  • Suitor #2
  • member icon



Reputation: 16479
  • View blog
  • Posts: 65,313
  • Joined: 12-June 08

Re: use c++ to call programme and terminate conditionally

Posted 03 December 2014 - 12:22 PM

Quote

if I see that the lower bound is larger than some value.

You should probably make that 'if' condition in your code... though your snippet seems to indicate you other program is doing all the grunt work and this just gets the output at the end so you wouldn't have a chance to short circuit anything.

.
..
Was This Post Helpful? 1
  • +
  • -

#3 key01023   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 30-September 14

Re: use c++ to call programme and terminate conditionally

Posted 03 December 2014 - 12:25 PM

View Postmodi123_1, on 03 December 2014 - 12:22 PM, said:

Quote

if I see that the lower bound is larger than some value.

You should probably make that 'if' condition in your code... though your snippet seems to indicate you other program is doing all the grunt work and this just gets the output at the end so you wouldn't have a chance to short circuit anything.

.
..


Thank you very much, actually the blackbox program output the result line by line as its computation goes. And the short circuit is possible...
Was This Post Helpful? 0
  • +
  • -

#4 BetaWar   User is offline

  • #include "soul.h"
  • member icon

Reputation: 1695
  • View blog
  • Posts: 8,592
  • Joined: 07-September 06

Re: use c++ to call programme and terminate conditionally

Posted 03 December 2014 - 12:27 PM

I don't have any tutorials to provide, but here are the possible solutions I have come up with from the top of mind (in order from easiest, to most complicated):

1. Have the calling program do the check and just never execute the child program if the condition is met. (I don't believe this will be acceptable in your case)
2. Have the child program make the check and return a specific error string to the parent program. You are presumably already getting the output of the child program in the parent to print it, at which point you just need to look at that output and ensure that it isn't your error case(s). Your child application can exit early on before doing complex things that way and give you back information on what happened.
3. Shared memory. You could potentially create a shared memory pool (such as a memory mapped file) out there that both the parent and child have access to, then split off a thread on the parent program observing the memory pool and looking for a known change to alert you that there was an error at which point that thread kills off the process that the main thread of the parent program is waiting on (the child) and can proceed as normal.
4. Create a simple server in the parent program and have it listening on a specific port that the child then connects to. Have the parent with a second thread to look for incoming information via the socket and the child writes to that socket in the case of an error, at which point the socket thread kills the process (the child) and can proceed as normal.

NOTE - 3 and 4 may be backwards depending on what you have skill with.
Was This Post Helpful? 1
  • +
  • -

#5 Salem_c   User is offline

  • void main'ers are DOOMED
  • member icon

Reputation: 2555
  • View blog
  • Posts: 4,739
  • Joined: 30-May 10

Re: use c++ to call programme and terminate conditionally

Posted 03 December 2014 - 01:42 PM

If you want more control over the data flow, then you're going to need something lower level than the easy comfort of popen().

http://rachid.koucha...r/pty_pdip.html
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1