www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - spawnProcess: Exit parent process without terminating child process

reply timvol <timvol unknownmailaddress.com> writes:
Hi guys,

I want execute a process. I know, I can execute a process using 
"spawnProcess" or "executeShell". But I want exit the parent.

My code for testing purposes is the following:

int main(string[] asArgs_p)
{
     if ( (asArgs_p.length >= 2) && asArgs_p[1].isDir() )
     {
         while(1) {}
     }
     else
     {
         import std.process;
         spawnProcess([asArgs_p[0], "test"]);
     }
     return 0;
}

So, starting the application without any parameter, it calls 
"spawnProcess" with an parameter. Now, I want that the parent 
process (the process started without parameter) terminates, while 
the created process remains running.

At the moment, the parent process creates the child and remains 
open (because of the while(1)-loop).

Any ideas how I can exit the parent and keep the child process 
running?
Aug 25
next sibling parent FreeSlave <freeslave93 gmail.com> writes:
On Friday, 25 August 2017 at 19:55:09 UTC, timvol wrote:
 Hi guys,

 I want execute a process. I know, I can execute a process using 
 "spawnProcess" or "executeShell". But I want exit the parent.

 My code for testing purposes is the following:

 int main(string[] asArgs_p)
 {
     if ( (asArgs_p.length >= 2) && asArgs_p[1].isDir() )
     {
         while(1) {}
     }
     else
     {
         import std.process;
         spawnProcess([asArgs_p[0], "test"]);
     }
     return 0;
 }

 So, starting the application without any parameter, it calls 
 "spawnProcess" with an parameter. Now, I want that the parent 
 process (the process started without parameter) terminates, 
 while the created process remains running.

 At the moment, the parent process creates the child and remains 
 open (because of the while(1)-loop).

 Any ideas how I can exit the parent and keep the child process 
 running?
Running process in detached state will be available in the future versions of Phobos. This functionality has been already merged in master https://github.com/dlang/phobos/pull/5483 Until then you may consider to use a third-party library https://github.com/FreeSlave/detached
Aug 26
prev sibling parent FreeSlave <freeslave93 gmail.com> writes:
On Friday, 25 August 2017 at 19:55:09 UTC, timvol wrote:
 Hi guys,

 I want execute a process. I know, I can execute a process using 
 "spawnProcess" or "executeShell". But I want exit the parent.

 My code for testing purposes is the following:

 int main(string[] asArgs_p)
 {
     if ( (asArgs_p.length >= 2) && asArgs_p[1].isDir() )
     {
         while(1) {}
     }
     else
     {
         import std.process;
         spawnProcess([asArgs_p[0], "test"]);
     }
     return 0;
 }

 So, starting the application without any parameter, it calls 
 "spawnProcess" with an parameter. Now, I want that the parent 
 process (the process started without parameter) terminates, 
 while the created process remains running.

 At the moment, the parent process creates the child and remains 
 open (because of the while(1)-loop).

 Any ideas how I can exit the parent and keep the child process 
 running?
Note that in your particular case the spawnProcess as used now should work too. It should run in parallel with its parent. Parent will exit and child process will remain. I'm not sure why the parent does not exit in your case. When starting this program without parameters it will not run into while(1) branch at all.
Aug 26