Jump to page: 1 2
Thread overview
std.process: spawnProcess
Sep 07, 2018
Russel Winder
Sep 07, 2018
Andrea Fontana
Sep 07, 2018
Russel Winder
Sep 07, 2018
Dr.No
Sep 07, 2018
Russel Winder
Sep 08, 2018
FreeSlave
Sep 08, 2018
Basile B.
Sep 08, 2018
Russel Winder
Sep 08, 2018
FreeSlave
Sep 08, 2018
Russel Winder
Sep 08, 2018
viniarck
September 07, 2018
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?

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



September 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
September 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, only a link to a C# example how do that but I believe you can convert to D easily.

https://stackoverflow.com/questions/6266820/working-example-of-createjobobject-setinformationjobobject-pinvoke-in-net
September 07, 2018
On Fri, 2018-09-07 at 15:53 +0000, Andrea Fontana via Digitalmars-d-learn wrote:
> 
[…]
> void main()
> {
> 
> ...
> auto yourpid = spawnProcess(...);
> scope(exit) kill(yourpid, SIGINT); // Or SIGKILL :)
> // Or: scope(exit) wait(yourpid);
> ...
> }
> 

Nice 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. :-(

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



September 07, 2018
On Fri, 2018-09-07 at 16:05 +0000, Dr.No via Digitalmars-d-learn wrote:
> 
[…]
> You also can use WINAPI's job object. It will close the registred process, even if the application exit abruptly. I have, by now, only a link to a C# example how do that but I believe you can convert to D easily.
> 
> 
https://stackoverflow.com/questions/6266820/working-example-of-createjobobject-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.

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



September 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.
September 08, 2018
On Sat, 2018-09-08 at 02:38 +0000, Basile B. via Digitalmars-d-learn wrote:
> 
[…]
> 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.

But as with other GC systems, there is no guarantee that the destructor will ever be called, or perhaps there is something here that gets round that?

> example API:
> 
> 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 used class, but is struct better here?

> P.S: i just see that in my own Process class i forgot to do that.
-- 
Russel.
===========================================
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



September 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.
September 08, 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.
September 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:
> > 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.

Annoying but yes, it looks like having a global registry of processes has to 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.

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

Detached is not a good idea, the spawned processes should not outlive the spawning process.
-- 
Russel.
===========================================
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



« First   ‹ Prev
1 2