PHP has system()
and shell_execute(), but how about spawn() ?
Currently we can only create an asynchronous process only with
proc_open()
or popen()
, and then I have to track the pipe(s).
It would be great to "exec and let go" the process.
PHP has system()
and shell_execute(), but how about spawn() ?
Currently we can only create an asynchronous process only with
proc_open()
or popen()
, and then I have to track the pipe(s).
It would be great to "exec and let go" the process.
Unfortunately no.
On win32 for instance, fork() doesn't exist at all.
There is a need for a cross-platform unified interface.
P.S. please always "reply to all"
If I am following you right, wouldn't a fork() followed by an exec
(one of the many forms) do what you're needing?
Jeremy Johnstone
http://www.jeremyjohnstone.com
jsjohnst@php.netPHP has
system()
and shell_execute(), but how about spawn() ?Currently we can only create an asynchronous process only with
proc_open()
orpopen()
, and then I have to track the pipe(s).It would be great to "exec and let go" the process.
Unfortunately no.
On win32 for instance, fork() doesn't exist at all.There is a need for a cross-platform unified interface.
I suspect that such a thing is a myth in the case of PHP; we already
have cruft following us from the SAPI on fork() that potentially
screws up detached processes on platforms that have it.
Will "app &" work do what you want on unix and "start app" do what you
want on win32?
--Wez.
I've tried it all, actually, on Win32.
"start app" will start another console window for console
applications, and wait until it closes. for windowed applications it
would wait for the application to finish.
same with "start /b app".
I would write an implementation myself, but I'm not familiar with the
zend api and php source base yet. some day I will...
Unfortunately no.
On win32 for instance, fork() doesn't exist at all.There is a need for a cross-platform unified interface.
I suspect that such a thing is a myth in the case of PHP; we already
have cruft following us from the SAPI on fork() that potentially
screws up detached processes on platforms that have it.Will "app &" work do what you want on unix and "start app" do what you
want on win32?--Wez.
Wez Furlong wrote:
Will "app &" work do what you want on unix and "start app" do what you
want on win32?
On unix you probably want to do something like "nohup app </dev/null
/dev/null 2&1 &" to completely detach.
Ilya77@Gmail.Com wrote:
Unfortunately no.
On win32 for instance, fork() doesn't exist at all.
Sure it does, it's just a different model.
<snip href="http://msdn.microsoft.com/"> One of the largest areas of difference is in the process model. UNIX has fork; Win32 does not. Depending on the use of fork and the code base, Win32 has two APIs that can be used: CreateProcess and CreateThread. A UNIX application that forks multiple copies of itself can be reworked in Win32 to have either multiple processes or a single process with multiple threads. If multiple processes are used, there are multiple methods of IPC that can be used to communicate between the processes (and perhaps to update the code and data of the new process to be like the parent, if the functionality that fork provides is needed). For more on IPC, see Interprocess Commuications. </snip>fork() doesn't work exactly as planned, i.e.:
pid2 = fork();
if(pid2 == 0) {
/* hi, i'm the child */
That won't exactly work because Windows requires an exact image to load
from, and it will treat it as if you had just started it (EIP = ENTRY
POINT). However, some uses of fork() revolve around something like this:
pid2 = fork();
if(pid2 == 0) {
execve("/bin/part2", ...)
In this case, great news! It'll work. Use CreateProcess().
The assumption that Win32 doesn't have fork() isn't entirely correct.
It doesn't have POSIX fork(), but it can do some uses of fork().
CreateThread() is something else to look into, but if you use fork() for
threading, you're taking the long road.
If I am following you right, wouldn't a fork() followed by an exec
(one of the many forms) do what you're needing?
If it will, CreateProcess() will work, like I said. Look into
http://pecl.php.net/package/ffi as well. You can't pass writable
structures (0.4, WEZ!? :P) but you're bound to be able to whip something
together if you're willing to play dirty.
Otherwise, just use exec()
. :D
--
_
()__ Jed Smith, Code Ninja
| / | RFBs: [email for info]
| _ \ +1 (541) 606-4145
/ |/ jed@jed.bz (Signed mail preferred: PGP 0x703F9124)
|_/ http://personal.jed.bz/keys/jedsmith.asc
It seems that this discussion is at all obsolete.
Cygwin implements fork() by copying the process address space to a new
process, and ActivePerl implements fork() using threads. But we are
not talking about fork() at all. There is no need for fork() to exist
as an API within PHP to implement this SIMPLE, BASIC operation.
The bottom line, is that in PHP there is no simple and consistent
cross-platform way to create a detached child process.
I'm currently implementing a job processing system using PHP 5.0.x,
and on my way I've already found a couple of bugs in the engine and
fixed one of them.
Being a system programmer, this shouldn't be too much of an obstacle
for me, but at this moment I'm thinking of the application programmers
which don't have an api to create detach processes.
That's it...
Ilya77@Gmail.Com wrote:
Unfortunately no.
On win32 for instance, fork() doesn't exist at all.Sure it does, it's just a different model.
<snip href="http://msdn.microsoft.com/"> One of the largest areas of difference is in the process model. UNIX has fork; Win32 does not. Depending on the use of fork and the code base, Win32 has two APIs that can be used: CreateProcess and CreateThread. A UNIX application that forks multiple copies of itself can be reworked in Win32 to have either multiple processes or a single process with multiple threads. If multiple processes are used, there are multiple methods of IPC that can be used to communicate between the processes (and perhaps to update the code and data of the new process to be like the parent, if the functionality that fork provides is needed). For more on IPC, see Interprocess Commuications. </snip>fork() doesn't work exactly as planned, i.e.:
pid2 = fork(); if(pid2 == 0) { /* hi, i'm the child */
That won't exactly work because Windows requires an exact image to load
from, and it will treat it as if you had just started it (EIP = ENTRY
POINT). However, some uses of fork() revolve around something like this:pid2 = fork(); if(pid2 == 0) { execve("/bin/part2", ...)
In this case, great news! It'll work. Use CreateProcess().
The assumption that Win32 doesn't have fork() isn't entirely correct.
It doesn't have POSIX fork(), but it can do some uses of fork().
CreateThread() is something else to look into, but if you use fork() for
threading, you're taking the long road.If I am following you right, wouldn't a fork() followed by an exec
(one of the many forms) do what you're needing?If it will, CreateProcess() will work, like I said. Look into
http://pecl.php.net/package/ffi as well. You can't pass writable
structures (0.4, WEZ!? :P) but you're bound to be able to whip something
together if you're willing to play dirty.Otherwise, just use
exec()
. :D--
_
()__ Jed Smith, Code Ninja
| / | RFBs: [email for info]
| _ \ +1 (541) 606-4145
/ |/ jed@jed.bz (Signed mail preferred: PGP 0x703F9124)
|_/ http://personal.jed.bz/keys/jedsmith.asc
The bottom line, is that in PHP there is no simple and consistent
cross-platform way to create a detached child process.
Being a system programmer, this shouldn't be too much of an obstacle
for me, but at this moment I'm thinking of the application programmers
which don't have an api to create detach processes.
Define what you mean by detached and provide an example (in C) of how
to achieve it on all (well, most will do) unices without assuming the
presence of utility apps like nohup.
Likewise for win32.
--Wez.
win32: copy the code from proc_open()
, just leave the pipes stuff out.
By detached I mean - start and leave running.
for most unixes:
pid = fork();
if (pid == 0)
{
close(0);
close(1);
close(2);
// mask out `SIGHUP` signal - unfortunately I don't remember the
calls for unixes
exec(...);
}
else if (pid == -1)
{
// fail
return -1;
}
else
{
return pid;
}
The bottom line, is that in PHP there is no simple and consistent
cross-platform way to create a detached child process.Being a system programmer, this shouldn't be too much of an obstacle
for me, but at this moment I'm thinking of the application programmers
which don't have an api to create detach processes.Define what you mean by detached and provide an example (in C) of how
to achieve it on all (well, most will do) unices without assuming the
presence of utility apps like nohup.Likewise for win32.
--Wez.
Sure it does, it's just a different model.
<snip href="http://msdn.microsoft.com/">
...
That's not fork() ;)
If it will, CreateProcess() will work, like I said. Look into
What do you think proc_open()
does? :)
http://pecl.php.net/package/ffi as well. You can't pass writable
structures (0.4, WEZ!? :P)
it's on my very long TODO list.
--Wez.
Wez Furlong wrote:
it's on my very long TODO list.
--Wez.
No rush. If I was 50% less lazy I might cvs co it and look at it for a
few minutes, and say "aw hell, I'll just wait for Wez and Ilia," but I'm
too lazy to do even that, so ... I'll just wait for Wez and Ilia. =)
Jed
--
_
()__ Jed Smith, Code Ninja
| / | RFBs: [email for info]
| _ \ +1 (541) 606-4145
/ |/ jed@jed.bz (Signed mail preferred: PGP 0x703F9124)
|_/ http://personal.jed.bz/keys/jedsmith.asc