Results 1 to 17 of 17

Thread: QProcess : child process listening parent output ?

  1. #1
    Join Date
    Feb 2007
    Posts
    158
    Thanks
    25
    Qt products
    Qt4
    Platforms
    Windows

    Default QProcess : child process listening parent output ?

    Hello,

    I know that the QProcess problem has been discussed many times, but there is one thing I can't find on the forum : How the child application can handle the main application's instructons writted on the output channel ?



    My main application creates a QProcess linked to "src_2.exe" and listen its output channel :
    Qt Code:
    1. Veilleur::Veilleur(QWidget *parent) : QObject(parent)
    2. {
    3. myProcess = new QProcess(this);
    4. myProcess->setReadChannelMode(QProcess::MergedChannels);
    5. connect(myProcess, SIGNAL(readyRead()), this, SLOT(SLOT_onReadyReadOut()));
    6.  
    7. myProcess->start("../../src_2/release/src_2.exe");
    8.  
    9. myProcess->write("An instruction to perform...");
    10. }
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. void Veilleur::SLOT_onReadyReadOut()
    2. {
    3. std::cout << "\nReading the child application, on its stdout channel : " << std::endl;
    4. QString message = "";
    5. message.append(myProcess->readAllStandardOutput());
    6. std::cout << "." << message.toStdString() << "." << std::endl;
    7. }
    To copy to clipboard, switch view to plain text mode 



    My "src_2" application is the following :

    man.cpp :
    Qt Code:
    1. #include <QApplication>
    2.  
    3. #include "MailChecker.h"
    4.  
    5. int main(int argc, char *argv[])
    6. {
    7. QApplication app(argc, argv);
    8.  
    9. MailChecker * mChecker = new MailChecker();
    10. mChecker->Check();
    11.  
    12. return app.exec();
    13. }
    To copy to clipboard, switch view to plain text mode 

    MailChecker constructor :
    Qt Code:
    1. MailChecker::MailChecker() : QObject()
    2. {
    3. std::cout << "Message for the parent application : I'm here !" << std::endl;
    4. }
    To copy to clipboard, switch view to plain text mode 



    So I would like the child application doing the same job as the parent application.
    I would like it to listen the output channel of the parent, in order to give it instructions.
    I can't understand how to do it, but I think it would be similar to what i've done for the parent one.

    In the parent one it wasn't too difficult, cause I could create my own child application and recognize it every where : she was the "myProcess".
    But in the new child application I can't access to the parent one, in order to declare a connect like :
    connect(myParentProcess, SIGNAL(readyRead()), this, SLOT(SLOT_onReadyReadOut()));

    And how if I could identify the "myParentProcess", where should I declare the connection ?



    Thanks for your help

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QProcess : child process listening parent output ?

    connect(myParentProcess, SIGNAL(readyRead()), this, SLOT(SLOT_onReadyReadOut()));

    And how if I could identify the "myParentProcess", where should I declare the connection ?
    This might be possible, but if it is, it is probably quite exotic.
    Why don't you just use sockets?
    Or shared memory?
    Or Pipes?
    Any of these would be better and I think quite simpler to acheave.

  3. #3
    Join Date
    Feb 2007
    Posts
    158
    Thanks
    25
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QProcess : child process listening parent output ?

    I don't know how to use socket with QProcess, and in reality I have created a secnd application in order to hold the QTcpSocket connection cause she takes lots of memory .

    My main application is a Mail Checker that creates a connection to the server each 5 minutes.
    I made a second application that is called each time I want to check the server, with a QTcpSocket.
    So I would like no to use Socket to speak with the second application

    What do you named share memory ? A file for example ?
    This was my first idea, but I thought QTcpSocket where made in order to communicate (send and receive) with other process.

    And how could I use pipes ?

    Thanks for your interest

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QProcess : child process listening parent output ?

    The parent process can write to the child process' stdin by using QIODevice::write(). Maybe that's enough for your needs?

  5. #5
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QProcess : child process listening parent output ?

    but I thought QTcpSocket where made in order to communicate (send and receive) with other process.
    Correct
    And you have two processes that need to comunicate - so isn't it a job for a socket?
    QTcpSocket connection cause she takes lots of memory
    What makes you think that?

  6. #6
    Join Date
    Feb 2007
    Posts
    158
    Thanks
    25
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QProcess : child process listening parent output ?

    Yes, I use the write() function.
    But, if I understand well, my child application must loop listening the stdin ?
    I can't use signal/slots ?

    QTcpSockets : I don't know if it is wanted, but when I connect my sockets to a server it takes 1MO and don'tfree it when I disonnect/close/delete the socket.

  7. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QProcess : child process listening parent output ?

    Quote Originally Posted by Nyphel View Post
    But, if I understand well, my child application must loop listening the stdin ?
    The application must just read the input if it wants to. There are no special requirements for that to work.
    I can't use signal/slots ?
    No, signal-slot connections are not meant for interprocess communication. You can use other IPC mechanisms of course - named or anonymous pipes, message queues, shared memory.

    QTcpSockets : I don't know if it is wanted, but when I connect my sockets to a server it takes 1MO and don'tfree it when I disonnect/close/delete the socket.
    A socket should take no more than about 100kB to work including the buffer (which resides in kernel space, so it is not counted into resources used by the app). Maybe you have a memory leak somewhere?

  8. #8
    Join Date
    Feb 2007
    Posts
    158
    Thanks
    25
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QProcess : child process listening parent output ?

    Qt Code:
    1. QTcpSocket *socket_r;
    2. socket_r = new QTcpSocket();
    3.  
    4. //socket_r->connectToHost("marathon", 143);
    5.  
    6. //socket_r->disconnectFromHost();
    7. //socket_r->close();
    8.  
    9. socket_r->deleteLater();
    To copy to clipboard, switch view to plain text mode 
    Here : all the memory has been freed


    Qt Code:
    1. QTcpSocket *socket_r;
    2. socket_r = new QTcpSocket();
    3.  
    4. socket_r->connectToHost("marathon", 143); // Takes 1 000 ko
    5.  
    6. socket_r->disconnectFromHost();
    7. socket_r->close();
    8.  
    9. socket_r->deleteLater();
    To copy to clipboard, switch view to plain text mode 
    Here : 1 000 KO memory has not been freed

  9. #9
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QProcess : child process listening parent output ?

    But how how are you mesuring the memory used by the socket object?
    Try this:
    Qt Code:
    1. QTcpSocket *socket_r;
    2. socket_r = new QTcpSocket();
    3. socket_r->connectToHost("marathon", 143); // Takes 1 000 ko
    4. qDebug()<<"socket size:"<<sizeof(*socket_r);
    5. socket_r->disconnectFromHost();
    6. socket_r->close();
    7. socket_r->deleteLater();
    To copy to clipboard, switch view to plain text mode 

    And what is KO?
    You mean KB. (KiloByte)

  10. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QProcess : child process listening parent output ?

    "sizeof" won't work. You'll only measure the socket object size and not the memory it allocates on the heap (including its private component if it has one).

    A better indicator for me would be to instantiate 100 sockets, let them be for a while and then see the memory consumption. Remember that connecting to a host which you pass by a hostname involves also the DNS resolving phase (which uses memory as well). So if your processes resides on the same machine, it would be wiser to pass "127.0.0.1" here instead.

  11. #11
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QProcess : child process listening parent output ?

    not the memory it allocates on the heap
    True - didn"t tink it through.

  12. #12
    Join Date
    Feb 2007
    Posts
    158
    Thanks
    25
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QProcess : child process listening parent output ?

    I mesure it with the Windows task manager.
    The server and the client are on different machines.

    If I launch multiple connections, it doesn't take more memory (tested with 3 sequentials connections/deconnections). So, I don't think it's a memory leak, instead the 3 connections used the same memory adress... I should try to laucnh the 3 at the same time.



    PS : KO for "Kilo Octet"

  13. #13
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QProcess : child process listening parent output ?

    Quote Originally Posted by Nyphel View Post
    The server and the client are on different machines.
    Ok, now I'm a bit lost... You want to launch an application on a remote machine using QProcess and listen to its output or are we dealing with two completely separate problems here?

  14. #14
    Join Date
    Feb 2007
    Posts
    158
    Thanks
    25
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QProcess : child process listening parent output ?

    Huhu

    My main appplication runs on the client machine.
    It want's to question the server about new incoming mails.

    In order to do that, i have made two small application.
    The first, the main, is a GUI based one : trayicon, menus, pop-ups, timers, etc.
    The second, the child, is able to communicate with the server.

    The main one launch the secnd when must know the number of incoming mails.
    The child one answer the main one with the output channel.

    I would like the main one to give orders to the child one.
    Example : re-check for mails, give me the mails senders, etc.

  15. #15
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QProcess : child process listening parent output ?

    So does the "main" one work on the same machine as the "child"?

    If so, then I don't see why you can't pass 127.0.0.1 to the socket...

  16. #16
    Join Date
    Feb 2007
    Posts
    158
    Thanks
    25
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QProcess : child process listening parent output ?

    Yes, the two are on the client machine.
    I don't want to use Sockets to communicate with the child application, I wanted a signal/slots base communication

  17. #17
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QProcess : child process listening parent output ?

    You can't have signals/slots - they don't work accross process boundaries! You can either use an stdin/stdout communication (through QProcess) or some kind of proper IPC mechanism like a shared memory, a pipe, message queue, socket or something simmilar.

Similar Threads

  1. initialize child widgets within parent?
    By ucomesdag in forum Newbie
    Replies: 6
    Last Post: 6th June 2006, 08:11
  2. Move child widget along with the parent widget
    By sreedhar in forum Qt Programming
    Replies: 2
    Last Post: 15th May 2006, 12:00
  3. Infinite loop - resize parent from child
    By bitChanger in forum Qt Programming
    Replies: 3
    Last Post: 5th May 2006, 13:21
  4. Referencing Parent Widget from Child
    By taylor34 in forum Qt Programming
    Replies: 8
    Last Post: 11th April 2006, 15:13
  5. QProcess / system call not working under linux. Why?
    By johnny_sparx in forum Qt Programming
    Replies: 12
    Last Post: 11th March 2006, 00:32

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.