简体   繁体   中英

running C exec in php background

I have searched and found some suggestions to my question but almost all of them are executing php files, so i don't know if that has something to do with it not working for me.

My goal is for my webpage to load completely without finishing my script that takes x amount of time, but it wont do it with this line of code. Is there something im missing? i have seen this answer in many places and it seems to work for them.

<?php

exec("sudo ./EscalonVel 50 2 100 10 20 &> /dev/null &");
echo "Hello";
?>

If you has to show on the page data calculated on the C script, then "someone" has to wait until it's finished to show it out. If the data comes directly from the command executed (output/stdout of the execution), then you cannot background the command with & : the output might come after request is dispatched, and process backgrounding disconnects it's output from actual execution. So you have 2 options:

  1. Show the page "template" completely and prepare it to accept the contents at the very end of your HTML (suboptimal but possible)
  2. Do a 2 request page: the first shows the template (full page) and the second (AJAX) fills it up with data from command execution. Depending on how you write it, the AJAX can do several request until the command is terminated, which is preferable if the script runs for more than 20 seconds. Then you'll need some kind of process checking and some backend to save (by command) and view (by AJAX request) the data, as a file or database.

Hope it helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM