From 501e252bf71d5de424a1cc34b16956cfdcc26417 Mon Sep 17 00:00:00 2001 From: zhangzihao Date: Thu, 21 Jan 2021 21:05:53 +0800 Subject: [PATCH] Adjust the logic between parent and child processes, named pipes to be added. --- libsearch/index/first-index.cpp | 32 +++++----------------- libsearch/index/inotify-index.cpp | 44 ++++++++++++++++++++++++------- libsearch/index/inotify-index.h | 2 +- libsearch/index/traverse_bfs.h | 1 + src/main.cpp | 2 +- 5 files changed, 43 insertions(+), 38 deletions(-) diff --git a/libsearch/index/first-index.cpp b/libsearch/index/first-index.cpp index 9ba13c9..120cffe 100644 --- a/libsearch/index/first-index.cpp +++ b/libsearch/index/first-index.cpp @@ -2,24 +2,6 @@ #include "first-index.h" #include -void handler(int){ - qDebug() << "Recieved SIGTERM!"; - GlobalSettings::getInstance()->setValue(INDEX_DATABASE_STATE, "2"); - GlobalSettings::getInstance()->setValue(CONTENT_INDEX_DATABASE_STATE, "2"); - GlobalSettings::getInstance()->setValue(INDEX_GENERATOR_NORMAL_EXIT, "2"); - GlobalSettings::getInstance()->setValue(INOTIFY_NORMAL_EXIT, "2"); - - - qDebug() << "indexDataBaseStatus: " << GlobalSettings::getInstance()->getValue(INDEX_DATABASE_STATE).toString(); - qDebug() << "contentIndexDataBaseStatus: " << GlobalSettings::getInstance()->getValue(CONTENT_INDEX_DATABASE_STATE).toString(); - -// InotifyIndex::getInstance("/home")->~InotifyIndex(); - qDebug() << "~IndexGenerator() end!" << endl; - - //wait linux kill this thread forcedly -// while (true); -} - #define NEW_QUEUE(a) a = new QQueue(); qDebug("---------------------------%s %s %s new at %d..",__FILE__,__FUNCTION__,#a,__LINE__); //#define DELETE_QUEUE(a ) @@ -94,11 +76,11 @@ void FirstIndex::run(){ else{ //if the parameter is false, index won't be rebuild //if it is true, index will be rebuild - this->p_indexGenerator = IndexGenerator::getInstance(true,this); + p_indexGenerator = IndexGenerator::getInstance(true,this); } } else{ - this->p_indexGenerator = IndexGenerator::getInstance(false,this); + p_indexGenerator = IndexGenerator::getInstance(false,this); } // this->q_content_index->enqueue(QString("/home/zhangzihao/Desktop/qwerty/四库全书.txt")); @@ -110,6 +92,7 @@ void FirstIndex::run(){ pid = fork(); if(pid == 0) { + prctl(PR_SET_PDEATHSIG, SIGKILL); prctl(PR_SET_NAME,"first-index"); FileUtils::_index_status = CREATING_INDEX; QSemaphore sem(5); @@ -166,9 +149,9 @@ void FirstIndex::run(){ if (this->q_content_index) delete this->q_content_index; this->q_content_index = nullptr; - if (this->p_indexGenerator) - delete this->p_indexGenerator; - this->p_indexGenerator = nullptr; + if (p_indexGenerator) + delete p_indexGenerator; + p_indexGenerator = nullptr; QThreadPool::globalInstance()->releaseThread(); QThreadPool::globalInstance()->waitForDone(); @@ -183,9 +166,6 @@ void FirstIndex::run(){ } FileUtils::_index_status = FINISH_CREATING_INDEX; - qDebug() << "sigset start!"; - sigset( SIGTERM, handler); - qDebug() << "sigset end!"; //quit() is shit!!! // return; diff --git a/libsearch/index/inotify-index.cpp b/libsearch/index/inotify-index.cpp index e87b75d..6ff5c22 100644 --- a/libsearch/index/inotify-index.cpp +++ b/libsearch/index/inotify-index.cpp @@ -1,5 +1,23 @@ #include "inotify-index.h" +void handler(int){ + qDebug() << "Recieved SIGTERM!"; + GlobalSettings::getInstance()->setValue(INDEX_DATABASE_STATE, "2"); + GlobalSettings::getInstance()->setValue(CONTENT_INDEX_DATABASE_STATE, "2"); + GlobalSettings::getInstance()->setValue(INDEX_GENERATOR_NORMAL_EXIT, "2"); + GlobalSettings::getInstance()->setValue(INOTIFY_NORMAL_EXIT, "2"); + + + qDebug() << "indexDataBaseStatus: " << GlobalSettings::getInstance()->getValue(INDEX_DATABASE_STATE).toString(); + qDebug() << "contentIndexDataBaseStatus: " << GlobalSettings::getInstance()->getValue(CONTENT_INDEX_DATABASE_STATE).toString(); + _exit(0); + +// InotifyIndex::getInstance("/home")->~InotifyIndex(); + + //wait linux kill this thread forcedly +// while (true); +} + InotifyIndex::InotifyIndex(const QString& path) : Traverse_BFS(path) { /*-------------ukuisearchdbus Test start-----------------*/ @@ -111,7 +129,7 @@ void InotifyIndex::eventProcess(const char* buf, ssize_t tmp){ if (event->mask & IN_CREATE){ if (event->mask & IN_ISDIR){ AddWatch(currentPath[event->wd] + '/' + event->name); - this->setPath(currentPath[event->wd] + '/' + event->name); + setPath(currentPath[event->wd] + '/' + event->name); Traverse(); } @@ -166,7 +184,7 @@ void InotifyIndex::eventProcess(const char* buf, ssize_t tmp){ RemoveWatch(currentPath[event->wd] + '/' + event->name); IndexGenerator::getInstance()->deleteAllIndex(new QStringList(currentPath[event->wd] + '/' + event->name)); AddWatch(currentPath[event->wd] + '/' + event->name); - this->setPath(currentPath[event->wd] + '/' + event->name); + setPath(currentPath[event->wd] + '/' + event->name); Traverse(); indexQueue->enqueue(QVector() << QString(event->name) << QString(currentPath[event->wd] + '/' + event->name) << QString((event->mask & IN_ISDIR) ? "1" : "0")); @@ -266,6 +284,10 @@ next: */ void InotifyIndex::run(){ + qDebug() << "sigset start!"; + sigset( SIGTERM, handler); + qDebug() << "sigset end!"; + char buf[BUF_LEN] __attribute__((aligned(8))); ssize_t numRead; @@ -278,6 +300,7 @@ void InotifyIndex::run(){ pid = fork(); if(pid == 0) { + prctl(PR_SET_PDEATHSIG, SIGKILL); prctl(PR_SET_NAME,"inotify-index"); if (numRead == 0) { qDebug() << "read() from inotify fd returned 0!"; @@ -286,19 +309,20 @@ void InotifyIndex::run(){ qDebug() << "read"; } eventProcess(buf, numRead); - QTimer* liveTime = new QTimer(this); - bool b_timeout = false; - liveTime->setInterval(30000); - connect(liveTime, &QTimer::timeout, this, [&](){ -// _exit(0); - b_timeout = true; - }); + QTimer* liveTime = new QTimer(); + //restart inotify-index proccess per minute + liveTime->setInterval(60000); liveTime->start(); + +// connect(liveTime, &QTimer::timeout, [ = ](){ +//// _exit(0); +// *b_timeout = 1; +// }); for (;;){ numRead = read(m_fd, buf, BUF_LEN); liveTime->stop(); this->eventProcess(buf, numRead); - if (b_timeout){ + if (liveTime->remainingTime() < 1){ _exit(0); } liveTime->start(); diff --git a/libsearch/index/inotify-index.h b/libsearch/index/inotify-index.h index e535b11..56ef147 100644 --- a/libsearch/index/inotify-index.h +++ b/libsearch/index/inotify-index.h @@ -12,7 +12,7 @@ #include "file-utils.h" #include "first-index.h" -#define BUF_LEN 1024 +#define BUF_LEN 1024000 class InotifyIndex; static InotifyIndex* global_instance_of_index = nullptr; class InotifyIndex : public QThread, public Traverse_BFS diff --git a/libsearch/index/traverse_bfs.h b/libsearch/index/traverse_bfs.h index ed5e3c9..635eee6 100644 --- a/libsearch/index/traverse_bfs.h +++ b/libsearch/index/traverse_bfs.h @@ -10,6 +10,7 @@ class Traverse_BFS { public: void Traverse(); + virtual ~Traverse_BFS() = default; virtual void DoSomething(const QFileInfo&) = 0; void setPath(const QString&); protected: diff --git a/src/main.cpp b/src/main.cpp index 7e56892..f088c9b 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -186,7 +186,7 @@ int main(int argc, char *argv[]) // FirstIndex* fi = new FirstIndex("/home/zhangzihao/Desktop/qwerty"); FirstIndex fi("/home"); fi.start(); - fi.wait(); +// fi.wait(); // fi->wait(); // fi->exit(); // delete fi;