Re: creating detached processes in PHP

From: Date: Tue, 23 Nov 2004 06:28:03 +0000
Subject: Re: creating detached processes in PHP
References: 1 2 3 4  Groups: php.internals 
Request: Send a blank email to internals+get-13930@lists.php.net to get a copy of this message
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... On Mon, 22 Nov 2004 18:30:34 -0800, Jed Smith <jed@jed.bz> wrote: > 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 > > -- > > > PHP Internals - PHP Runtime Development Mailing List > To unsubscribe, visit: http://www.php.net/unsub.php > >

« previous php.internals (#13930) next »