-   Programming (
-   -   C++ Child Process (

Scarrez 11-16-2012 09:38 AM

C++ Child Process

I'm trying to make a game server wrapper file for minecraft in C++ (Has to be Linux), doesn't have to be C++ so if someone has an alternative better language to do this in I will use it.

The main thing I want to do is write it start the server and then monitor with an IsAlive() function of sorts.
I also want to be able to commit console commands to the server from the C++ application.

I have managed to get it to launch the server using "execlp" but it pauses upon running that function. because I'm using the Fork the parent process continues to run which is ok, if someone knows of a way you can send commands the the child process which will actually run in the minecraft server this will almost be perfect.

This is what I have so far...


#include <cstdlib>
#include <iostream>
#include <stdio.h>
#include <string>
#include <sys/types.h>

using namespace std;

int main(int argc, char** argv)
        char* filetoexec = "/Servers/minecraft_server.jar";
        cout << "Hello World: " << filetoexec << endl;

        pid_t pid;
        pid = fork();
        cout << pid << endl;

        switch (pid)
            case -1:
            case 0:
                cout << "Child Process" << endl;
                execlp("java", "java", "-jar", filetoexec, "nogui", false);
                cerr << "Failed" << endl;
                    cout << "Child Tick..." << getpid() << endl;
                cout << "Parent Process" << endl;
                    cout << "Parent Tick..." << getpid() << endl;
                    //system("/list"); //An attempt at running a minecraft console command with no luck
    return 0;

millgates 11-16-2012 12:40 PM

may I ask what is the purpose of this wrapper of yours?

To achieve what you described, you may want to use some sort of pipe. I also think a shell script (or a language like perl) might be better suited for this.

Scarrez 11-16-2012 02:27 PM

I'm more doing this as a learning task since I enjoy playing with code and want to write a management program for my minecraft server, I know there are many out there that do this but I enjoy learning so that's the main reason.

Can I ask why Perl over C++?

millgates 11-18-2012 02:39 AM

I have allways found C(++) to be a bit awkward when dealing with external commands. I allways try to prefer library calls to running external programs. Whenever I am tempted to use something like system("...") in C++, I consider writing a shell script instead. Of course, it is perfectly possible in C++, so if you just want to learn, go ahead. But I believe a few lines in bash or something like that would be a much simpler solution. It of course depends on what you want your program to do. If you want to do any advanced text processing/parsing, or maybe a GUI, then for example Perl could be a good choice, being faster to write than C++, but having a lot of modules for practically anything.

Scarrez 11-19-2012 06:22 AM

I would like to say thank you for recommeding Perl, I have looked into it and it can do everything I'm after with far more ease than C++.
I have started writing this in Perl now and I'm already further than I was in C++.


sundialsvcs 11-19-2012 06:55 AM

I'm sure that would be delighted to hear that. ;)

But it's also true enough. Scripting languages (of which Perl is not the only one, although it is one of the best) are the power-tools, and they are especially so because of the large number of tested packages of software which are available for them. So, you don't have to reinvent the wheel no matter what wheel it might be. "The language itself" is not the real focus: "everything else," is.

Scarrez 11-19-2012 07:52 AM

Agreed, I was looking into SQL in C++ as noticed it wasn't as simple as originally hope but with Perl i managed the run a query within 1 minute of starting my search on Google :)
Another thing I like is the similarity to PHP which is a language I'm very familiar too.

All times are GMT -5. The time now is 09:25 PM.