I'm writting an application which will be doing something with memory of other process.
These actions may take some time, so I decided to place them in another thread.
Here's the simplified class of this thread:
public:
int pid;
void run();
};
Thread : public QThread{
public:
int pid;
void run();
};
To copy to clipboard, switch view to plain text mode
and run() function:
void Thread::run(){
ptrace(PTRACE_ATTACH, pid);
sleep(60);
ptrace(PTRACE_DETACH, pid);
}
void Thread::run(){
ptrace(PTRACE_ATTACH, pid);
sleep(60);
ptrace(PTRACE_DETACH, pid);
}
To copy to clipboard, switch view to plain text mode
Of course i put those sleep for example only, in my program there are another functions, but it doesn't matter.
User can start or stop this thread by clicking on buttons - Start, and Cancel.
I have no problems with starting this thread, here's the simplified code:
connect(ui->pushButton, SIGNAL(clicked()), this, SLOT(startThread()));
(...)
void MainWindow::startThread(){
thread.pid=getPid();
thread.start();
}
connect(ui->pushButton, SIGNAL(clicked()), this, SLOT(startThread()));
(...)
void MainWindow::startThread(){
thread.pid=getPid();
thread.start();
}
To copy to clipboard, switch view to plain text mode
But problem appears, when user clicks on Cancel button.
Before doing anything with memory of other process it's needed to attach this process.
As you see, I'm doing it at the beggining of run() function.
But when process is attached it's impossible to use it - it must be detached first.
That's why i added the last line to run():
ptrace(PTRACE_DETACH, pid);
ptrace(PTRACE_DETACH, pid);
To copy to clipboard, switch view to plain text mode
But when user clicks Cancel process also need to be detached, so I overloaded terminate() function:
void Thread::terminate(){
ptrace(PTRACE_DETACH, pid);
}
void Thread::terminate(){
ptrace(PTRACE_DETACH, pid);
QThread::terminate();
}
To copy to clipboard, switch view to plain text mode
connect(ui->pushButton_2, SIGNAL(clicked()), this, SLOT(stopThread()));
(...)
void MainWindow::stopThread(){
thread.terminate();
}
connect(ui->pushButton_2, SIGNAL(clicked()), this, SLOT(stopThread()));
(...)
void MainWindow::stopThread(){
thread.terminate();
}
To copy to clipboard, switch view to plain text mode
I think that should work fine, but it doesn't.
When user doesn't click Cancel, 60 seconds passess and process is detaching.
But when user click Cancel detaching fails.
How is it possible?
The same function, called in exactly the same way, and first works but second doesn't.
Thanks in advance and sorry for bad english!
Bookmarks