Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8212575
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T10:48:13+00:00 2026-06-07T10:48:13+00:00

I have very primitive web front-end for my C++ application. Client (web browser) is

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T10:48:15+00:00Added an answer on June 7, 2026 at 10:48 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have searched online but all the answers I have found were very primitive.
Hi I'm building a primitive browser in c which is to do a very
I've written a resolver so I can have a very primitive DI framework. In
Very primitive question but I am stuck (I guess being newbie). I have a
First of all I have to admit that these are very basic and primitive
I have an application that contains a main Activity, which has a very simple
i have very serious problem if any tech expert can help...thank you in advance..
I have very simple piece of code. The goal is when i input four-digit
I have very very big html page/data. I need to fetch data under h1
I have very strange problem while I am submitting a practice problem on codechef.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.