0

I need to write something similar to compiletest. Compiletest works by invoking the rustc process and capturing stdout/stderr. My project does not have a binary to run yet, so I need another way to create a subprocess.

I can't use a channel nor stdout because it's not controlled by my code.

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
kdy
  • 370
  • 3
  • 14

1 Answers1

7

How can I promote a thread to child process

You cannot. This is simply something that operating systems don't do; it's not specific to Rust. You cannot start an arbitrary thread and then move it ("promote it") to another process later; threads and processes are very different concepts.

My project does not have a binary to run yet

I suggest making one.


If you wish to simply start another process, you can use std::process::Command for that. This would be the most recommended option.

POSIX does have the fork call, which duplicates the entire memory of the process and the current thread, so theoretically you could use this to "promote" the current thread to a new process.

Using this in a multithreaded context can be very complicated and basically will always be recommended against by default.

Support for fork on Windows is... limited, at best. If you only care about POSIX systems, you can use fork from the libc crate.

See also:


capturing stdout/stderr

There are hidden, unstable methods for changing the current threads stdout / stderr. Rust's testing framework uses these methods. If you'd be interested in making these stable, you could perhaps offer to help on the tracking issue.

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
  • Then.. how does node child process work? Does it just spawn other instance of `node` executable with different parameter? – kdy Jan 27 '18 at 03:45
  • Oh.. that make sense.. And I guess that's why only scripting languages has such multi process apis. – kdy Jan 27 '18 at 03:46
  • I just want to capture stderr of test, and diff it with expected output. I don't care if it runs in separate process, but stderr is per process so I thought I have to somehow create a process for each test. – kdy Jan 27 '18 at 04:38
  • [`fork`](https://docs.rs/libc/0.2.36/libc/fn.fork.html) "promotes" a copy of the current thread to a new process. How does that not fit the question? (Although I wouldn't recommend using `fork` in multi-thread context.) – Stefan Jan 27 '18 at 12:57
  • @Stefan you have a good point, I just didn't think `fork` was relevant to OPs actual situation as it sounded like they wanted to move *another* thread to a process (plus it is platform-dependent). I might as well add it for completeness. – Shepmaster Jan 27 '18 at 15:23