You are not logged in.

Dear visitor, welcome to KDE-Forum.org. If this is your first visit here, please read the Help. It explains in detail how this page works. To use all features of this page, you should consider registering. Please use the registration form, to register here or read more information about the registration process. If you are already registered, please login here.

karye

Unregistered

1

Wednesday, August 18th 2004, 6:13am

How do I start a process thread in Linux?

Hi!

I'm a bit lost on threads.
I'd like to start an app in Linux, release it, let it run in the background, then reconnect to it and read its output.
Is it possible?
The main app would work as a frontend to the Linux app.

Greatly appreciate an answer!

djanubis

Beginner

Posts: 29

Location: Moulins France

Occupation: Software ingeneering

  • Send private message

2

Wednesday, August 18th 2004, 6:44am

RE: How do I start a process thread in Linux?

Quoted

I'm a bit lost on threads.
I'd like to start an app in Linux, release it, let it run in the background, then reconnect to it and read its output.
Is it possible?
The main app would work as a frontend to the Linux app.


As we're in a Qt Forum, let's start with what Qt provide us:

The simplest way to achieve what you want could be QProcess class. This class works for every supported operating system and uses whatever underlying system needs. Its main advantage is its portability.

For pure threading, you should consider Qt threading classes wich provide system independant threading abstrction.

Linux internally manages kernel threading in two flavours: old style Linux Threads and newer NPTL (Native Posix Threads Library). You can find some HOWTO in kernel documentation.
You can also use pthreads library if you need user space only threads.

Note: each kind of threads has its own programming style. Qt classes offer an abstract way for dealing with processes and threads.
Never patch not working code. Rewrite it !
Never patch badly designed classes. Recreate them cleanly.
(Excerpts from Computing Bible)

Home of the Lab project

karye

Unregistered

3

Wednesday, August 18th 2004, 10:32am

RE: How do I start a process thread in Linux?

Thanx!

Some example would be great, if possible.

The QT would act as frontend to the linux app, the process would go like this:
1. Start QT app
2. Launch Linux app from within QT app
3. Close QT app
4. Later ... restart QT app, connect to Linux app and check progress and output

djanubis

Beginner

Posts: 29

Location: Moulins France

Occupation: Software ingeneering

  • Send private message

4

Wednesday, August 18th 2004, 10:49am

RE: How do I start a process thread in Linux?

A good start is examining sample code in QProcess class documentation and the process sample in the examples directory from your Qt distribution.

The main problem when quitting ui and reconnecting later is finding processid for app you want to control.
You might save the processid somewhere and check if such an id is active, if it is the good executable and connect to it.

With linux, you can find many informations on processes in /proc filesystem.
Never patch not working code. Rewrite it !
Never patch badly designed classes. Recreate them cleanly.
(Excerpts from Computing Bible)

Home of the Lab project

wysota

Trainee

Posts: 65

Location: Warsaw, POLAND

  • Send private message

5

Wednesday, August 18th 2004, 1:48pm

RE: How do I start a process thread in Linux?

Doesn't the external process get killed if the parent process (Qt app) ends?

And how to reconnect to stdin/stdout of an already running process? It isn't that easy if this "linux app" doesn't support its own IPC handling.
Live and let live - use the search engine.
"Use the docs Luke, use the docs!"

This post has been edited 1 times, last edit by "wysota" (Aug 18th 2004, 1:50pm)


djanubis

Beginner

Posts: 29

Location: Moulins France

Occupation: Software ingeneering

  • Send private message

6

Wednesday, August 18th 2004, 2:33pm

RE: How do I start a process thread in Linux?

Quoted

Originally posted by wysota
Doesn't the external process get killed if the parent process (Qt app) ends?

Yes, except if we use hacks to reparent the process. But this is not very clean nor a snap :(

Quoted

And how to reconnect to stdin/stdout of an already running process? It isn't that easy if this "linux app" doesn't support its own IPC handling.

That's the main issue for most classic utilities. One could cheat and poke into /proc, but we would need different methods for 2.0, 2.2, 2.4, 2.6 kernels. Not so clean too.

Well, maybe the gui launcher should not quit, but hide, and show when trying a new launch. Sounds easier to implement and does not lead to a gas factory.
Never patch not working code. Rewrite it !
Never patch badly designed classes. Recreate them cleanly.
(Excerpts from Computing Bible)

Home of the Lab project

karye

Unregistered

7

Wednesday, August 18th 2004, 3:11pm

RE: How do I start a process thread in Linux?

I'v been experimenting with the Qthread example.

Not so successfully though, I get:
QThread object destroyed while thread is still running.
subclass: Fatal IO error: client killed
KCrash: Application 'subclass' crashing...

gemerge.h:
#ifndef GEMERGE_H
#define GEMERGE_H

#include "MyDialog2b.h"
#include <qthread.h>

class Gemerge : public QThread
{

public:
virtual void run();

};

#endif

gemerge.cpp:
#include "gemerge.h"
#include <qthread.h>

void Gemerge::run()
{
for( int count = 0; count < 20; count++ ) {
sleep( 1 );
qDebug( "Ping!" );
}
}

mydialog2b.cpp:
...
void MyDialog2b::fileOpen()
{
Gemerge a;

a.start();
}
...

karye

Unregistered

8

Wednesday, August 18th 2004, 3:47pm

RE: How do I start a process thread in Linux?

It's working now!
Forgot to declare the thread in mydialog2b.h.

Can I get a signal when a thread has reach its end?
Will the thread die at termination?

djanubis

Beginner

Posts: 29

Location: Moulins France

Occupation: Software ingeneering

  • Send private message

9

Wednesday, August 18th 2004, 5:15pm

RE: How do I start a process thread in Linux?

You could have a llop testing the thread for finished() and send a signal on finished() returning TRUE.
Never patch not working code. Rewrite it !
Never patch badly designed classes. Recreate them cleanly.
(Excerpts from Computing Bible)

Home of the Lab project

karye

Unregistered

10

Thursday, August 19th 2004, 7:38am

RE: How do I start a process thread in Linux?

Won't a loop freeze the qt app?

djanubis

Beginner

Posts: 29

Location: Moulins France

Occupation: Software ingeneering

  • Send private message

11

Thursday, August 19th 2004, 8:18am

RE: How do I start a process thread in Linux?

No if your loop calls qApp->processEvents().

But by loop, I though of some timer calling a checking slot on timeout. Timers are very handy for subprocess monitoring.
Never patch not working code. Rewrite it !
Never patch badly designed classes. Recreate them cleanly.
(Excerpts from Computing Bible)

Home of the Lab project

wysota

Trainee

Posts: 65

Location: Warsaw, POLAND

  • Send private message

12

Thursday, August 19th 2004, 9:23am

RE: How do I start a process thread in Linux?

If it really is a child process, you should get a SIGCHLD posix signal when the child process ends (not to be confused with Qt signals of course).

There is a way to do the thing with quitting the application and then reconnecting - you need another app, that will be a controller of theese child processes. You don't fork theese from your "monitor" app, but from the controller one.... It has to run during the life of all children and it has to provide a way to communicate with theese processes (like a Unix domain socket, messages or shared memory), then you can send commands to such controller to spawn a child, kill it, monitor it and so on.
Live and let live - use the search engine.
"Use the docs Luke, use the docs!"

djanubis

Beginner

Posts: 29

Location: Moulins France

Occupation: Software ingeneering

  • Send private message

13

Thursday, August 19th 2004, 10:00am

RE: How do I start a process thread in Linux?

some kind of daemon like. Sounds good :)
Never patch not working code. Rewrite it !
Never patch badly designed classes. Recreate them cleanly.
(Excerpts from Computing Bible)

Home of the Lab project

karye

Unregistered

14

Saturday, August 21st 2004, 9:50am

RE: How do I start a process thread in Linux?

A small example using Qtimer would be very handy :-)
Experimenting with QCustomEvent and postEvent with no success.

karye

Unregistered

15

Saturday, August 21st 2004, 9:50am

RE: How do I start a process thread in Linux?

Oh lala!
Sounds complicated...

karye

Beginner

Posts: 9

Location: Stockholm

  • Send private message

16

Saturday, August 21st 2004, 9:53am

RE: How do I start a process thread in Linux?

Oh lala!
Sounds complicated...


Quoted

Originally posted by wysota
If it really is a child process, you should get a SIGCHLD posix signal when the child process ends (not to be confused with Qt signals of course).

There is a way to do the thing with quitting the application and then reconnecting - you need another app, that will be a controller of theese child processes. You don't fork theese from your "monitor" app, but from the controller one.... It has to run during the life of all children and it has to provide a way to communicate with theese processes (like a Unix domain socket, messages or shared memory), then you can send commands to such controller to spawn a child, kill it, monitor it and so on.

wysota

Trainee

Posts: 65

Location: Warsaw, POLAND

  • Send private message

17

Saturday, August 21st 2004, 10:09am

RE: How do I start a process thread in Linux?

Neeee... it's easy :P

You have to write an application that will wait for commands and then execute them. You can use sockets with datagrms for the "command line". Commands you have to implement are: spawn child, kill child, send data to child, get data from child. Based on these commands you can implement more complicated actions.

The object is to have a daemon that is active during the life of the monitored process. This way you can always "connect" to it and send it commands. I think this is the only "proper" way to achieve such result, because using proc is not the best way (too much system dependant).

Edit:

Oh... I just noticed that it is the KDE forum :P You can also use the DCOP interface and forget the controller app.
Live and let live - use the search engine.
"Use the docs Luke, use the docs!"

This post has been edited 1 times, last edit by "wysota" (Aug 21st 2004, 10:11am)