LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 11-16-2012, 09:38 AM   #1
Scarrez
LQ Newbie
 
Registered: Nov 2012
Posts: 7

Rep: Reputation: Disabled
C++ Child Process


Hi,

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...

Code:
#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:
                break;
            case 0:
                cout << "Child Process" << endl;
                execlp("java", "java", "-jar", filetoexec, "nogui", false);
                cerr << "Failed" << endl;
                while(true)
                {
                    cout << "Child Tick..." << getpid() << endl;
                    sleep(4);
                }
                break;
            default:
                cout << "Parent Process" << endl;
                while(true)
                {
                    cout << "Parent Tick..." << getpid() << endl;
                    //system("/list"); //An attempt at running a minecraft console command with no luck
                    sleep(4);
                }
                break;
        }
    }
    return 0;
}
 
Old 11-16-2012, 12:40 PM   #2
millgates
Member
 
Registered: Feb 2009
Location: 192.168.x.x
Distribution: Slackware
Posts: 651

Rep: Reputation: 269Reputation: 269Reputation: 269
Hi,
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.
 
Old 11-16-2012, 02:27 PM   #3
Scarrez
LQ Newbie
 
Registered: Nov 2012
Posts: 7

Original Poster
Rep: Reputation: Disabled
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++?
 
Old 11-18-2012, 02:39 AM   #4
millgates
Member
 
Registered: Feb 2009
Location: 192.168.x.x
Distribution: Slackware
Posts: 651

Rep: Reputation: 269Reputation: 269Reputation: 269
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.
 
Old 11-19-2012, 06:22 AM   #5
Scarrez
LQ Newbie
 
Registered: Nov 2012
Posts: 7

Original Poster
Rep: Reputation: Disabled
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++.

Thanks
Scarrez
 
Old 11-19-2012, 06:55 AM   #6
sundialsvcs
Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 5,268

Rep: Reputation: 1086Reputation: 1086Reputation: 1086Reputation: 1086Reputation: 1086Reputation: 1086Reputation: 1086Reputation: 1086
I'm sure that http://www.perlmonks.org 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.
 
Old 11-19-2012, 07:52 AM   #7
Scarrez
LQ Newbie
 
Registered: Nov 2012
Posts: 7

Original Poster
Rep: Reputation: Disabled
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Sending Signal from Child Process to Parent Process : Not getting desired output thelink123 Linux - General 4 10-26-2012 09:05 PM
Under which circumstances a child process creates another child process using fork? mitsulas Programming 3 12-08-2009 08:16 AM
Difference between Child THREAD and Child PROCESS whho Linux - Newbie 10 08-02-2009 07:24 PM
How to kill a Child and all its subsequent child process in C shayer009 Programming 3 12-04-2007 12:40 AM
Killing a child process from another child marri Programming 6 10-01-2004 07:08 PM


All times are GMT -5. The time now is 04:42 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration