Cuberite Forum
Linux popen() alternative - Printable Version

+- Cuberite Forum (https://forum.cuberite.org)
+-- Forum: Cuberite (https://forum.cuberite.org/forum-4.html)
+--- Forum: Development (https://forum.cuberite.org/forum-13.html)
+--- Thread: Linux popen() alternative (/thread-1903.html)

Pages: 1 2 3


Linux popen() alternative - xoft - 05-04-2015

Apparently Linux has a problem spawning other processes from a multithreaded process: http://www.linuxprogrammingblog.com/threads-and-fork-think-twice-before-using-them
I've run into this issue while trying to use Lua's io.popen() from the Gallery plugin ( https://github.com/mc-server/Gallery/blob/master/WebList.lua#L72 ), it works perfectly on my Windows box, but when trying to run this on a RasPi server, the server freaks out - the server seems to be running fine, but the web request never finishes, the browser is stuck at "Reading from ...", and when I attach GDB, there is no sign of any WebAdmin threads at all. There's also no network activity of any kind, even the MC client port seems closed, although the server still looks as if it is running, it responds to console commands.

So now I need the help of a Linux guru. Is there any alternative to popen that I could use? I don't really need the IO redirection, just spawning the process in the background would be nice, but if there was an IO-redirection-capable solution, that would allow us to override Lua's io.popen() seamlessly.


RE: Linux popen() alternative - NiLSPACE - 05-04-2015

Wouldn't os.execute work? In windows you can use os.execute("start <process>") to start a new process without blocking the server.


RE: Linux popen() alternative - xoft - 05-04-2015

os.execute() seems to work well on the RasPi, I'll try back on Windows. From its documentation I was under the impression that it waited for the process to terminate, which is something I don't want.


RE: Linux popen() alternative - xoft - 05-04-2015

Yes, os.execute waits for the child process to terminate. This is pretty useless in the MCS world.


RE: Linux popen() alternative - NiLSPACE - 05-04-2015

Wait.. it blocked the server on Windows, but not on the RasPi? Then what if you add "start" to the to-be-executed string? "start MCSchematicToPng <arguments>"


RE: Linux popen() alternative - worktycho - 05-04-2015

Its more complicated than just a Linux solution, this is an issue that faces all POSIX platforms, due to the nature of the fork/exec model. So whilst there are some easy solutions on linux (clone comes to mind), we want a POSIX solution or else we face breaking other POSIX platforms (OS X and *BSD being most significant).


RE: Linux popen() alternative - worktycho - 05-04-2015

If you don't need the redirection there's posix_spawn on POSIX. It provides the atomicity at the cost of being a more fiddly function. But this will involve custom bindings as there's no way to access it from lua. Its a similar concept to window's CreateProcess.


RE: Linux popen() alternative - xoft - 05-04-2015

STR, it blocked the server on both. I just didn't notice at first, because the child process took only a few seconds. Still, can't afford even that - it would mean a 5-second lag in the game.

worktycho, I was hoping for something that we could wrap in an API. If the alternative allows for IO redirection it would be rather nice to translate that into callbacks, if it supports child termination notification, that would be another nice callback to have. If it doesn't allow either of these, then the API would be as simple as a single function call.


RE: Linux popen() alternative - NiLSPACE - 05-05-2015

According to this: http://askubuntu.com/questions/287350/start-a-new-process-without-blocking-the-terminal

you can put "&" behind the command string on linux to make it execute without blocking the server.


RE: Linux popen() alternative - xoft - 05-05-2015

It is somewhat hackish, but it could work. I'll try tomorrow.