[phobos] std.process deprecations

Lars Tandle Kyllingstad via phobos phobos at puremagic.com
Thu Aug 7 11:03:25 PDT 2014


On 07/08/14 16:05, Andrei Alexandrescu wrote:
> On 8/7/14, 1:21 AM, Lars Tandle Kyllingstad via phobos wrote:
>> I think we made a good case for why those functions ought to be
>> deprecated on Windows in that discussion, and while I stand by my
>> arguments, I don't really have anything more to add to re-convince you.
>>
>> Removing functionality is a drastic step to take, and I won't try to
>> deprecate them unless there is a clear consensus that it is the right
>> thing to do.
>
> I am convinced. I do understand exec* is inefficient on Windows.
> However, I remember I dealt with an application in which removal of said
> functionality would force me to essentially paste the source code from
> std.process into my program. That doesn't seem like progress.

Inefficiency was never used as an argument.  The problem is that exec* 
does different things on Windows and POSIX.  In other words, a D program 
which uses std.process.exec* will have different observable behaviour on 
different platforms.  This is what bothers me.

   - On POSIX systems, exec* overwrites the current process
     with a new one.

   - On Windows, exec* spawns a new, independent process and
     exits the current one.

As an example of how this causes different observable behaviour, let us 
say that program A uses exec* to run program B.  The user starts A from 
the command line.  The POSIX user will then be returned to the command 
line when program B has completed, and the exit code he receives is that 
of program B.  The Windows user will be returned to the command line 
immediately after the exec* statement has run, and program B will run 
independently in the background.  Which exit code the user receives is 
anyone's guess, but it is certainly not from B.


> Is there a chance to offer the functionality on Windows in a reasonable
> way?

Yes or no, depending on how your question is interpreted.

No, it is not possible to make exec* overwrite the current process on 
Windows.  New processes are always created in isolation.  Not even 
Microsoft managed to implement exec* properly in their C runtime; their 
functions fake it with spawn-and-exit, as described above.

But yes, you can make a program that does the same thing yours does now, 
even if exec* is deprecated on Windows.  The user-side code will be more 
verbose, but the difference in behaviour will be explicit.

Program using today's std.process.execv():

     execv("prog", ["arg1", "arg2" ]);

Program that behaves in the same manner, after exec* deprecation on Windows:

     auto cmd = [ "prog", "arg1", "arg2" ];
     version (Posix) execv(cmd[0], cmd[1 .. $]);
     else { spawnProcess(cmd); _exit(0); }

Somewhat improved program that appears to behave in almost the same 
manner on all platforms (assuming there are no other threads running!):

     auto cmd = [ "prog", "arg1", "arg2" ];
     version (Posix) execv(cmd[0], cmd[1 .. $]);
     else _exit(wait(spawnProcess(cmd)));

Lars


More information about the phobos mailing list