digitalmars.D.learn - std.process: spawnProcess
- Russel Winder (15/15) Sep 07 2018 =46rom what I can see, processes created with std.process: spawnProcess ...
- Andrea Fontana (10/16) Sep 07 2018 void main()
- Russel Winder (15/26) Sep 07 2018 [=E2=80=A6]
- Dr.No (6/12) Sep 07 2018 You also can use WINAPI's job object. It will close the registred
- Russel Winder (14/21) Sep 07 2018 https://stackoverflow.com/questions/6266820/working-example-of-createjob...
- FreeSlave (3/5) Sep 08 2018 On Posix systems you may try using SIGCHLD handler. Google for
- Basile B. (18/24) Sep 07 2018 You can wrap in a Process struct or class and take advantage of
- Russel Winder (15/34) Sep 08 2018 But as with other GC systems, there is no guarantee that the destructor ...
- FreeSlave (6/12) Sep 08 2018 No, detached is not default. By default you should call wait on
- Russel Winder (15/29) Sep 08 2018 Annoying but yes, it looks like having a global registry of processes ha...
- viniarck (40/46) Sep 08 2018 On Python std lib, there's a way to daemonize a process [1]. I
=46rom what I can see, processes created with std.process: spawnProcess are n= ot terminated when the creating process terminates, i.e. it seems Config.detac= hed is the default for these process. Is there a way of all spawned processes being terminated on main terminatio= n? =20 --=20 Russel. =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D Dr Russel Winder t: +44 20 7585 2200 41 Buckmaster Road m: +44 7770 465 077 London SW11 1EN, UK w: www.russel.org.uk
Sep 07 2018
On Friday, 7 September 2018 at 14:36:42 UTC, Russel Winder wrote:From what I can see, processes created with std.process: spawnProcess are not terminated when the creating process terminates, i.e. it seems Config.detached is the default for these process. Is there a way of all spawned processes being terminated on main termination?void main() { ... auto yourpid = spawnProcess(...); scope(exit) kill(yourpid, SIGINT); // Or SIGKILL :) // Or: scope(exit) wait(yourpid); ... } Andrea
Sep 07 2018
On Fri, 2018-09-07 at 15:53 +0000, Andrea Fontana via Digitalmars-d-learn wrote:=20[=E2=80=A6]void main() { =20 ... auto yourpid =3D spawnProcess(...); scope(exit) kill(yourpid, SIGINT); // Or SIGKILL :) // Or: scope(exit) wait(yourpid); ... } =20Nice thought, but the spawn is deep in the heart of the application, so scope(exit) isn't feasible. I am rapidly coming to the conclusion, that I am going to have to use a registry and apply kill as the last act in main. :-( =20 --=20 Russel. =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D Dr Russel Winder t: +44 20 7585 2200 41 Buckmaster Road m: +44 7770 465 077 London SW11 1EN, UK w: www.russel.org.uk
Sep 07 2018
On Friday, 7 September 2018 at 14:36:42 UTC, Russel Winder wrote:From what I can see, processes created with std.process: spawnProcess are not terminated when the creating process terminates, i.e. it seems Config.detached is the default for these process. Is there a way of all spawned processes being terminated on main termination?You also can use WINAPI's job object. It will close the registred process, even if the application exit abruptly. I have, by now, convert to D easily. https://stackoverflow.com/questions/6266820/working-example-of-createjobobject-setinformationjobobject-pinvoke-in-net
Sep 07 2018
On Fri, 2018-09-07 at 16:05 +0000, Dr.No via Digitalmars-d-learn wrote:=20[=E2=80=A6]You also can use WINAPI's job object. It will close the registred=20 process, even if the application exit abruptly. I have, by now,=20 convert to D easily. =20 =20https://stackoverflow.com/questions/6266820/working-example-of-createjobobj= ect-setinformationjobobject-pinvoke-in-net I guess this might work on Windows, but I am on Linux and OSX, so I'll have= to try another route. --=20 Russel. =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D Dr Russel Winder t: +44 20 7585 2200 41 Buckmaster Road m: +44 7770 465 077 London SW11 1EN, UK w: www.russel.org.uk
Sep 07 2018
On Friday, 7 September 2018 at 16:44:09 UTC, Russel Winder wrote:I guess this might work on Windows, but I am on Linux and OSX, so I'll have to try another route.On Posix systems you may try using SIGCHLD handler. Google for exact examples.
Sep 08 2018
On Friday, 7 September 2018 at 14:36:42 UTC, Russel Winder wrote:From what I can see, processes created with std.process: spawnProcess are not terminated when the creating process terminates, i.e. it seems Config.detached is the default for these process. Is there a way of all spawned processes being terminated on main termination?You can wrap in a Process struct or class and take advantage of the destructor to do that. Assuming you write standard GC-ed code the destructor should be called at the end or if you free manually the resources. example API: struct SpawnedProcess { private: Pid pid; public: this(); void execute(); void terminate(); bool running(); ~this(){ if (running) terminate();} } P.S: i just see that in my own Process class i forgot to do that.
Sep 07 2018
On Sat, 2018-09-08 at 02:38 +0000, Basile B. via Digitalmars-d-learn wrote:=20[=E2=80=A6]You can wrap in a Process struct or class and take advantage of=20 the destructor to do that. Assuming you write standard GC-ed code=20 the destructor should be called at the end or if you free=20 manually the resources.But as with other GC systems, there is no guarantee that the destructor wil= l ever be called, or perhaps there is something here that gets round that?example API: =20 struct SpawnedProcess { private: Pid pid; public: this(); void execute(); void terminate(); bool running(); ~this(){ if (running) terminate();} }struct or class? Given it is of necessity a heap located value I'd have use= d class, but is struct better here?=20P.S: i just see that in my own Process class i forgot to do that.--=20 Russel. =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D Dr Russel Winder t: +44 20 7585 2200 41 Buckmaster Road m: +44 7770 465 077 London SW11 1EN, UK w: www.russel.org.uk
Sep 08 2018
On Friday, 7 September 2018 at 14:36:42 UTC, Russel Winder wrote:From what I can see, processes created with std.process: spawnProcess are not terminated when the creating process terminates, i.e. it seems Config.detached is the default for these process.No, detached is not default. By default you should call wait on processes to free OS resources. Process may stay as zombie otherwise and it can be visible in process manager.Is there a way of all spawned processes being terminated on main termination?You probably need to register all child processes. Or spawn them as detached so you won't need to worry about freeing them.
Sep 08 2018
On Sat, 2018-09-08 at 10:24 +0000, FreeSlave via Digitalmars-d-learn wrote:On Friday, 7 September 2018 at 14:36:42 UTC, Russel Winder wrote:Annoying but yes, it looks like having a global registry of processes has t= o be created so they can be killed on main termination. :-( Though I am going to tinker with Basile B's suggestion of using a managing object with a destructor to see if that can avoid the global registry.=20From what I can see, processes created with std.process:=20 spawnProcess are not terminated when the creating process=20 terminates, i.e. it seems Config.detached is the default for=20 these process.=20 No, detached is not default. By default you should call wait on=20 processes to free OS resources. Process may stay as zombie=20 otherwise and it can be visible in process manager.Detached is not a good idea, the spawned processes should not outlive the spawning process. --=20 Russel. =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D Dr Russel Winder t: +44 20 7585 2200 41 Buckmaster Road m: +44 7770 465 077 London SW11 1EN, UK w: www.russel.org.ukIs there a way of all spawned processes being terminated on=20 main termination?=20 You probably need to register all child processes. Or spawn them=20 as detached so you won't need to worry about freeing them.
Sep 08 2018
On Friday, 7 September 2018 at 14:36:42 UTC, Russel Winder wrote:From what I can see, processes created with std.process: spawnProcess are not terminated when the creating process terminates, i.e. it seems Config.detached is the default for these process. Is there a way of all spawned processes being terminated on main termination?On Python std lib, there's a way to daemonize a process [1]. I tried to accomplish something equivalent by spawning a process inside a daemon thread in D: ``` auto daemonizedProc() { import std.process; import std.stdio; auto pid = spawnProcess(["telnet"]); writeln(pid.processID); } void main() { import core.thread; auto t = new Thread(&daemonizedProc); t.isDaemon(true); t.start(); // just to see it running for a few secs.. Thread.sleep(10.seconds); } ``` Based on my tests, whenever the main process exits (which is called "proctest" in the output bellow) the spawned process was terminated as a result as well, I tested on Linux: ``` ~/repos/dlang master* ❯ ps -ax | egrep "telnet|proctest" 14456 pts/13 S+ 0:00 ./proctest 14458 pts/13 S+ 0:00 /usr/bin/telnet 14537 pts/7 S+ 0:00 grep -E telnet|proctest ~/repos/dlang master* ❯ ps -ax | egrep "telnet|proctest" 14456 pts/13 S+ 0:00 ./proctest 14458 pts/13 S+ 0:00 /usr/bin/telnet 14633 pts/7 S+ 0:00 grep -E telnet|proctest ~/repos/dlang master* ❯ ps -ax | egrep "telnet|proctest" 14731 pts/7 S+ 0:00 grep -E telnet|proctest ``` [1] - https://docs.python.org/3/library/multiprocessing.html#multiprocessing.Process
Sep 08 2018