# creating detached processes in PHP

- Thread: https://externals.io/message/13914
- Original archive: https://news-web.php.net/php.internals/13914
- Started by ilya77@gmail.com on 2004-11-21T16:02:51+00:00
- Messages: 11
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #13914 — ilya77@gmail.com — 2004-11-21T16:02:51+00:00

URL: https://externals.io/message/13914#13914

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.

---

## #13917 — ilya77@gmail.com — 2004-11-22T08:47:10+00:00

URL: https://externals.io/message/13914#13917

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.net
> 
> 
> 
> 
> 
> > […]

---

## #13919 — Wez Furlong — 2004-11-22T13:29:18+00:00

URL: https://externals.io/message/13914#13919 · In reply to: #13917

> 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.

---

## #13920 — ilya77@gmail.com — 2004-11-22T13:39:45+00:00

URL: https://externals.io/message/13914#13920 · In reply to: #13919

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...


> 
> > […]
> 
> 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.

---

## #13922 — Christian Schneider — 2004-11-22T13:57:28+00:00

URL: https://externals.io/message/13914#13922 · In reply to: #13919

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.

- Chris

---

## #13929 — Jed Smith — 2004-11-23T02:30:34+00:00

URL: https://externals.io/message/13914#13929 · In reply to: #13917

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 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

---

## #13930 — ilya77@gmail.com — 2004-11-23T06:28:03+00:00

URL: https://externals.io/message/13914#13930 · In reply to: #13929

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:
> > […]
> 
> 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 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

---

## #13931 — Wez Furlong — 2004-11-23T06:46:28+00:00

URL: https://externals.io/message/13914#13931 · In reply to: #13929

> 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.

---

## #13932 — Wez Furlong — 2004-11-23T06:52:41+00:00

URL: https://externals.io/message/13914#13932 · In reply to: #13930

> 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.

---

## #13938 — ilya77@gmail.com — 2004-11-23T19:56:02+00:00

URL: https://externals.io/message/13914#13938 · In reply to: #13932

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;
}


> 
> > […]
> 
> > […]
> 
> 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.

---

## #13939 — Jed Smith — 2004-11-23T21:22:03+00:00

URL: https://externals.io/message/13914#13939 · In reply to: #13931

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
