4


I have very primitive web front-end for my C++ application. Client (web browser) is enters php site and fills form with parameters. Than (after post submit) php calls exec and application does its work. Application can work longer than minute and requires pretty large amount of RAM.

Is there any possibility to detect disconnecting from client (for example closure of tab in web browser). I want to do this, because after disconnecting client will not be able to see result of computations, so I can kill application and free some RAM on server.

Thanks for any help or suggestions.

user1126423
  • 125
  • 1
  • 5

1 Answers1

2

As long as the C++ program produces output while running, rather than generating all the output just prior to termination, use passthru() instead of exec().

This causes PHP to flush the output to the client as the content is produced, which allows PHP to detect when clients disconnect. PHP will terminate when the client disconnects and kill the child process immediately (as long as ignore_user_abort() is not set).

Example:

<?php

  function exec_unix_bg ($cmd) {
    // Executes $cmd in the background and returns the PID as an integer
    return (int) exec("$cmd > /dev/null 2>&1 & echo $!");
  }
  function pid_exists ($pid) {
    // Checks whether a process with ID $pid is running
    // There is probably a better way to do this
    return (bool) trim(exec("ps | grep \"^$pid \""));
  }

  $cmd = "/path/to/your/cpp arg_1 arg_2 arg_n";

  // Start the C++ program
  $pid = exec_unix_bg($cmd);

  // Ignore user aborts to allow us to dispatch a signal to the child
  ignore_user_abort(1);

  // Loop until the program completes
  while (pid_exists($pid)) {

    // Push some harmless data to the client
    echo " ";
    flush();

    // Check whether the client has disconnected
    if (connection_aborted()) {
      posix_kill($pid, SIGTERM); // Or SIGKILL, or whatever
      exit;
    }

    // Could be done better? Only here to prevent runaway CPU
    sleep(1);

  }

  // The process has finished. Do your thang here.

To collect the program's output, redirect the output to a file instead of /dev/null. I suspect you will need pcntl installed as well as posix for this, since the PHP manual indicates the SIGxxx constants are defined by the pcntl extension - although I have never had one installed without the other so I'm not sure either way.

Dave Jarvis
  • 30,436
  • 41
  • 178
  • 315
DaveRandom
  • 87,921
  • 11
  • 154
  • 174
  • I can not use `passthru()`, because my application is producing not text, but image. After computations php script passes to client link to that image (jpeg file). – user1126423 Jul 09 '12 at 11:56
  • OK then you do have more of a problem. There are many hurdles to overcome here, namely that your PHP script needs to keep pushing data to the client in order to detect whether the client has close the connection, which means (amongst other things) you will need to detach the child process from the parent so you can execute other PHP code while the external program is running. Is your server Windows or *nix based? Do you have the pcntl PHP extension available? – DaveRandom Jul 10 '12 at 08:31
  • I have Linux based server and I can recompile PHP to achive pcntl support. Any idea what can I send to client browser during computations? – user1126423 Jul 10 '12 at 11:36
  • @user1126423 What do you send to the client at the end currently? A full HTML page? At the moment I'm thinking whitespace, since HTML is whitespace agnostic, although you'd probably need to send the `` and preferably the start of the `` before you start the external process... – DaveRandom Jul 10 '12 at 12:25
  • Yes, I'm sending whole html page. I have read a bit about pcntl. I've read that I can't use `pcntl_fork()` when using PHP as Apache module. Is that true? Because (of my project manager requirements) I can't to use PHP as CGI application. – user1126423 Jul 10 '12 at 12:33
  • @user1126423 I believe it can be done with 5.3+ but you are correct in that one shouldn't really do it when running as a web server module. I was thinking that one could write a wrapper PHP script with signal handlers using `pcntl_signal()` and just `exec()` it and detach it from the current process, then send it a `SIGTERM` when the client disconnects, but in retrospect you could just put the signal handler in the C++ program (if required). What you actually need here is [`posix_kill()`](http://php.net/manual/en/function.posix-kill.php), which should be safe to use. – DaveRandom Jul 10 '12 at 12:42
  • Thank you so much, I will try to do this that way :) – user1126423 Jul 11 '12 at 07:45
  • @user1126423 I have just realised I omitted a trailing space from the end of the regex passed to `grep`. This is quite important as it means it could match processes other than the one you are interested in. If your system's `ps` tab-separates its columns I'm not actually sure how to fix this as POSIX regex is not my strong point, but I'm sure someone on SO will be able to help. I have edited my code above to add this missing character - I suggest you test thoroughly that the `exec_unix_bg()` and `pid_exists()` functions work correctly on your system before relying on them. – DaveRandom Jul 11 '12 at 08:48
  • There is another way of doing `pid_exists()`, which may or may not be more efficient: http://ro1.php.net/manual/en/function.posix-kill.php#82560 – Artefact2 Jul 11 '12 at 09:27
  • @Artefact2 Interesting, I don't have a *nix box readily available to test (all Windoze in my office) but that does look promising. The OP of that comment does seem to be fixated on the process being owned by another user though, which in this case it wouldn't be because the child process is forked from the PHP process running the check. It seems odd to me that there isn't a function specifically for this task... Although since PIDs may be duplicated over time none of this is 100% reliable for determining whether the specific process is actually the one you are interested in on a busy system. – DaveRandom Jul 11 '12 at 09:50