Skip to content
Advertisement

Process management code behaves different on Linux and Windows – Why?

(Context) I’m developing a cross-platform (Windows and Linux) application for distributing files among computers, based on BitTorrent Sync. I’ve made it in C# already, and am now porting to C++ as an exercise.

BTSync can be started in API mode, and for such, one must start the ‘btsync‘ executable passing the name and location of a config file as arguments.

At this point, my greatest problem is getting my application to deal with the executable. I’ve come to found Boost.Process when searching for a cross-platform process management library, and decided to give it a try. It seems that v0.5 is it’s latest working release, as some evidence suggests, and it can be infered there’s a number of people using it.

I implemented the library as follows (relevant code only):

File: test.hpp

JavaScript

File: Test.cpp

JavaScript


(Problem) On Windows, the above code will start btsync and wait (block) until the process is terminated (either by using Task Manager or by the API’s shutdown method), just like desired.
But on Linux, it finishes execution immediately after starting the process, as if wait_for_exit() isn’t there at all, though the btsync process isn’t terminated.

In an attempt to see if that has something to do with the btsync executable itself, I replaced it by this simple program:

File: Fake-Btsync.cpp

JavaScript


When used with this program, instead of the original btsync downloaded from the official website, my application works as desired. It will block for 20 seconds and then exit.

Question: What is the reason for the described behavior? The only thing I can think of is that btsync restarts itself on Linux. But how to confirm that? Or what else could it be?

Update: All I needed to do was to know about what forking is and how it works, as pointed in sehe’s answer (thanks!).


Question 2: If I use the System Monitor to send an End command to the child process ‘Fake-Btsync‘ while my main application is blocked, wait_for_exit() will throw an exception saying:

waitpid(2) failed: No child processes

Which is a different behavior than on Windows, where it simply says “ok” and terminates with status 0.

Update 2: sehe’s answer is great, but didn’t quite address Question 2 in a way I could actually understand. I’ll write a new question about that and post the link here.

Advertisement

Answer

The problem is your assumption about btsync. Let’s start it:

JavaScript

So, that’s the whole story right there: BitTorrent Sync forked to background. Nothing more. Nothing less. If you want to, btsync --help tells you to pass --nodaemon.

Testing Process Termination

Let’s pass --nodaemon run btsync using the test program. In a separate subshell, let’s kill the child btsync process after 5 seconds:

JavaScript

No problem!

A Better Fake Btsync

This should still be portable and be (much) better behaved when killed/terminated/interrupted:

JavaScript

You can test whether it is well-behaved

JavaScript

The same test can be run with “official” btsync and “fake” btsync. Output on my linux box:

JavaScript
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement