473,387 Members | 1,700 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

How to stop program?

Hi All,
I have a php program which calls a c++ program using exec. However when
user presses stop button in the browser or closes it this program keeps
on running on the server. How can I stop it according to user's
actions.
Thanks in advance.
Sal

Jul 17 '05 #1
15 3795
shyren wrote:
Hi All,
I have a php program which calls a c++ program using exec. However when
user presses stop button in the browser or closes it this program keeps
on running on the server. How can I stop it according to user's
actions.
Thanks in advance.
Sal


How can you detect the user pressed the stop button or closed the browser?
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Jul 17 '05 #2
I don't know how to detect that user pressed the stop button or closed
the browser. That is one part of the problem.
Sal
Jerry Stuckle a écrit :
shyren wrote:
Hi All,
I have a php program which calls a c++ program using exec. However when user presses stop button in the browser or closes it this program keeps on running on the server. How can I stop it according to user's
actions.
Thanks in advance.
Sal

How can you detect the user pressed the stop button or closed the

browser?

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================


Jul 17 '05 #3
shyren wrote:
I don't know how to detect that user pressed the stop button or closed
the browser. That is one part of the problem.
Sal


That was more of a rhetorical question. The answer is: you can't.
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Jul 17 '05 #4
shyren (sa********@yahoo.com) wrote:
: Hi All,
: I have a php program which calls a c++ program using exec. However when
: user presses stop button in the browser or closes it this program keeps
: on running on the server. How can I stop it according to user's
: actions.
: Thanks in advance.
: Sal

If the c++ program is sending data to the file handle that is going back
to the browser (normally stdout), then the c++ program _might_ receive an
error when it prints to stdout if the browser is stopped, depending on how
the web server works.

If a web server detected that the file handle to the browser was closed
then I suppose some web servers might signal the cgi. The script and its
children could arrange to be interupted by that.

I would experiment with those two things on the server in question to see
if your program can detect either one.

Both cases depend on the browser closing the socket when you hit the stop
button, which is not something you control.

--

This space not for rent.
Jul 17 '05 #5
NC
shyren wrote:

I have a php program which calls a c++ program using exec.
However when user presses stop button in the browser or closes
it this program keeps on running on the server. How can I stop
it according to user's actions.


1. Make sure PHP is not ignoring user's abort commands:

ignore_user_abort(false);

2. Write a function that would use exec() to send a kill
signal to the C++ program. On Unix, you could do
something like this:

function kill_my_cpp_prog() {
// call 'ps -auxww' to get the list of running processes
// find the pid for your C++ program
// call 'kill [pid]' to stop your C++ program
}

Refer to your OS manual for details of ps and kill usage.

3. Register that function to be executed at shutdown:

register_shutdown_function('kill_my_cpp_prog');

So when a user aborts, it will trigger execution of
kill_my_cpp_prog()...

Cheers,
NC

Jul 17 '05 #6
On 20 May 2005 10:07:02 -0700, "NC" <nc@iname.com> wrote:
shyren wrote:

I have a php program which calls a c++ program using exec.
However when user presses stop button in the browser or closes
it this program keeps on running on the server. How can I stop
it according to user's actions.


1. Make sure PHP is not ignoring user's abort commands:

ignore_user_abort(false);

2. Write a function that would use exec() to send a kill
signal to the C++ program. On Unix, you could do
something like this:

function kill_my_cpp_prog() {
// call 'ps -auxww' to get the list of running processes
// find the pid for your C++ program
// call 'kill [pid]' to stop your C++ program
}

Refer to your OS manual for details of ps and kill usage.

3. Register that function to be executed at shutdown:

register_shutdown_function('kill_my_cpp_prog');

So when a user aborts, it will trigger execution of
kill_my_cpp_prog()...


Changing to use proc_open so you can then use proc_terminate might also be
worth thinking about.

There's also posix_kill() for sending signals (but you still have to get the
pid first).

--
Andy Hassall / <an**@andyh.co.uk> / <http://www.andyh.co.uk>
<http://www.andyhsoftware.co.uk/space> Space: disk usage analysis tool
Jul 17 '05 #7
NC
Andy Hassall wrote:

1. Make sure PHP is not ignoring user's abort commands:

2. Write a function that would use exec() to send a kill
signal to the C++ program.

3. Register that function to be executed at shutdown:
Changing to use proc_open so you can then use proc_terminate might
also be worth thinking about.


Definitely.
There's also posix_kill() for sending signals (but you still
have to get the pid first).


Also, the POSIX extension is often disabled for security purposes...

Cheers,
NC

Jul 17 '05 #8
According to given suggestions I wrote a program for testing. To see
whether I capture the user's abort I write to the file in tmp directory
with writing permissions set. But still I am not able to detect it. i
am new to php and more help would be highly appreciated. Thanks..
__________________________________

<?php
ignore_user_abort(false);
function kill_my_cpp_prog()
{
$file=fopen("tmp/num","w");
fputs($file,"number2");
fclose($file);
echo "in";
}
#for($i=0;$i<10000000;$i++)
# echo $i;
exec("sleep 5");
register_shutdown_function('kill_my_cpp_prog');
?>

Jul 17 '05 #9
shyren,

You should most likely move the register_shutdown_function to right
after the function kill_my_cpp_prog as so:

<?php
ignore_user_about(false);
function kill_my_cpp_prog() {
...
}
register_shutdown_function('kill_my_cpp_prog');
....

Because the way you have it now, it will not register the shutdown
function until the script completes.

Mike
Jul 17 '05 #10
Mike,
Thanks for your response...

I moved the register_shutdown_function just after the kill_***
function. Still it doesn't work. Another observation is that if user
doesn't press stop button than the script completes its execution and
it writes "in" on the browser while there is nothing written in file
(even the file is not created). There is no problem with permissions
however.
Shyren

Jul 17 '05 #11
NC
shyren wrote:

According to given suggestions I wrote a program for testing.
To see whether I capture the user's abort I write to the file
in tmp directory with writing permissions set. But still I am
not able to detect it.

$file=fopen("tmp/num","w");


Are you sure you didn't mean:

$file=fopen("/tmp/num","w");

"tmp/num" refers to file "num" located in directory "tmp", which
is a subdirectory of the current working directory. "/tmp/num"
refers to file "num" located in directory "/tmp", which is the
default temporary directory on a Unix system...

Cheers,
NC

Jul 17 '05 #12
Hi,
There is another observation if I write to /tmp I can write in the
function kill_*** but to the directory tmp (I created this directory
with write permissions) I can't. While in other part of the script I
can write to both /tmp and tmp directory.
I am planning to write to a unique file the information that user has
pressed stop. And read this information in c program to stop it.
Will write soon...
thanks a lot for help.
shyren

Jul 17 '05 #13
It seems that I am not able to capture the event when user presses the
stop button. Here is my php script which calls a program test (in c,
code is given below). test program checks the file $pass_id every
second and if "exit" is written in the file it exits.
Whether or not the stop button is pressed file $pass_id is created
which shows that there is no detection of "stop" button being pressed
by the user.

Shyren
__________________________________
<?php
ignore_user_abort(false);
$hostid = hexdec(shell_exec("hostid"));
$td = gettimeofday();
$pass_id=$td["sec"].$td["usec"]. getmypid(). $hostid;
$pass_id="/tmp/my".$pass_id;
function kill_my_cpp_prog()
{
global $pass_id;
$file=fopen($pass_id,"w");
$num="exit";
fwrite($file,$num);
fclose($file);
echo "in";
}
register_shutdown_function('kill_my_cpp_prog');
$com="./test ".$pass_id;
exec($com,$out,$in);

?>
___________________________________
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main(int argc,const char** argv)
{
cout<<argv[1];
fstream fl1("/tmp/nun",ios::out);
for(int i=0;i<20;i++)
{
fstream fl(argv[1],ios::in);
char tmp[5];
fl.getline(tmp,5);
cout<<" "<<tmp;
if(string(tmp)=="exit")
i=10;
fl.close();
fl1<<tmp<<"in"<<i<<" ";
sleep(1);
}
fl1.close();
return 0;

}

Jul 17 '05 #14
shyren <sa********@yahoo.com> wrote:
It seems that I am not able to capture the event when user presses the
stop button.


Regartless if you actually manage to catch this, you should always make
sure your system call has proper limits (eg max memory/cpu(time) usage set).

How to do this depends on your OS...

Jul 17 '05 #15
This works finally...
the ignore_user_abort() was set to false before. Manual is also
misguiding. Also ob_flush() is necessary sometimes after flush() as
said in manual too...
"back" is to run the program in backgroun. It is mikehup available at
http://www.mikekohn.net/mikehup.php.
Thanks
_Shyren_____________________________________
<?php
set_time_limit(0);
ignore_user_abort(TRUE);
#register_shutdown_function('kill_my_cpp_prog');
$hostid = hexdec(shell_exec("hostid"));
$td = gettimeofday();
$pass_id=$td["sec"].$td["usec"]. getmypid(). $hostid;
$pass_id="/tmp/my".$pass_id;
#$pass_id="/tmp/num";

function kill_my_cpp_prog()
{
global $pass_id;
$file=fopen($pass_id,"w");
$num="exit";
fwrite($file,$num);
fclose($file);
echo "in";
exit();
}

$com="./back ./test ".$pass_id." &";
#$com="./test ".$pass_id." &";
echo $com;
flush();
ob_flush();
exec($com,$out,$in);
echo $out[0];
sleep(1);
while(1)
{
echo ".";
flush();
ob_flush();
if(connection_status() && connection_aborted())
{
kill_my_cpp_prog();
}
if(file_exists("/tmp/nun"))
{$file=fopen("/tmp/nun","r");
$num=fgets($file,7);
echo $num;
fclose($file);
if($num=="finish")
exit();
}
sleep(1);

$i--;
}

?>
______________________________________

#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main(int argc,const char** argv)
{
cout<<argv[1];
bool check=0;
ofstream fl1("/tmp/nun",ios::out);
for(int i=0;i<30;i++)
{
fstream fl(argv[1],ios::in);
char tmp[5];
fl.getline(tmp,5);
cout<<" "<<tmp;
if(string(tmp)=="exit")
{
i=30;
check=1;
}
fl.close();

sleep(1);
}
if(!check)
{
char *str="finish";
fl1.write(str,7);
}
fl1.close();
return 0;

}

Jul 17 '05 #16

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Harald Armin Massa | last post by:
I need to do some synchronisations like in a cron.job import time from threading import Thread class updater(Thread): def run(self): while True: do_updates() time.sleep(600)
8
by: Eric Osman | last post by:
My javascript program has reason to want to stop. For example, function A has 5 lines, the second of which calls function B, which has 5 lines, the first of which calls function C. But...
2
by: Mike Bennett | last post by:
Does the .NET framework (using VB.NET) support the ability to programmatically STOP a device prior to its removal from the system? I have need to move data from a computer on one network to a...
3
by: prakashsha | last post by:
HI, I've an asp.net page which has a button. On click of the button the program queries the database and gets the result and displays the same.Now assume that my program takes 2 minutes to get the...
14
by: electrician | last post by:
While running a program that exceeds the array limits it issues an alert. I want to then stop the program after filling in the output boxes with blanks. How do you stop the program? I have...
5
by: darthghandi | last post by:
I've created a class to listen to all interfaces and do a BeginAccept(). Once it gets a connection, it passes the connected socket off and stores it in a List. Next, it continues to listen for...
1
by: =?Utf-8?B?S2Vubnk=?= | last post by:
I have one bat file that contains a command to startup Java Program. Then, I would like to create a cluster job to call the bat file. In case of one computer is down, another computer can also call...
8
by: goron | last post by:
Hello, I am writing a computationally heavy program which I know will need to run for many hours. Therefore, I need to make it possible to stop the program at any time while it's running and save...
0
by: =?Utf-8?B?am1hZ2FyYW0=?= | last post by:
My program needs to do X when someone 'starts using' their Windows user account, and it should do Y when they 'stop using' their Windows user account. By 'starts using' I mean they log on, unlock...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.