From 0dda5b97e53496fa4d7b7cc9cd0af27247759f29 Mon Sep 17 00:00:00 2001 From: zhangzihao Date: Sat, 23 Jan 2021 09:52:44 +0800 Subject: [PATCH] Use select to not block process and child process whill exit per 30 secs. --- libsearch/index/first-index.cpp | 2 +- libsearch/index/inotify-index.cpp | 66 +++++++++++++++++++++++++------ 2 files changed, 54 insertions(+), 14 deletions(-) diff --git a/libsearch/index/first-index.cpp b/libsearch/index/first-index.cpp index 3d7b70a..587e005 100644 --- a/libsearch/index/first-index.cpp +++ b/libsearch/index/first-index.cpp @@ -112,7 +112,7 @@ void FirstIndex::run(){ pid = fork(); if(pid == 0) { - prctl(PR_SET_PDEATHSIG, SIGKILL); + prctl(PR_SET_PDEATHSIG, SIGTERM); prctl(PR_SET_NAME,"first-index"); QSemaphore sem(5); QMutex mutex1, mutex2, mutex3; diff --git a/libsearch/index/inotify-index.cpp b/libsearch/index/inotify-index.cpp index 71c845f..0c8d5c3 100644 --- a/libsearch/index/inotify-index.cpp +++ b/libsearch/index/inotify-index.cpp @@ -296,6 +296,12 @@ void InotifyIndex::run(){ read: numRead = read(m_fd, buf, BUF_LEN); + if (numRead == -1){ + printf("\033[1;31;40mread event error\033[0m\n"); + fflush(stdout); + assert(false); + } + char * tmp = const_cast(buf); @@ -317,7 +323,7 @@ fork: pid = fork(); if(pid == 0) { - prctl(PR_SET_PDEATHSIG, SIGKILL); + prctl(PR_SET_PDEATHSIG, SIGTERM); prctl(PR_SET_NAME,"inotify-index"); if (numRead == 0) { qDebug() << "read() from inotify fd returned 0!"; @@ -326,10 +332,44 @@ fork: qDebug() << "read"; } eventProcess(buf, numRead); - QTimer* liveTime = new QTimer(); - //restart inotify-index proccess per minute - liveTime->setInterval(60000); - liveTime->start(); + + + fd_set read_fds; + int rc; + timeval* read_timeout = (timeval*)malloc(sizeof(timeval)); + + for(;;) + { + FD_ZERO(&read_fds); + FD_SET(m_fd, &read_fds); + read_timeout->tv_sec = 30; + read_timeout->tv_usec = 0; + rc = select(m_fd + 1, &read_fds, NULL, NULL, read_timeout); + + if ( rc < 0 ) { + // error + qDebug() << "rc < 0"; + assert(false); + } + else if ( rc == 0 ) { + qDebug() << "timeout"; + _exit(0); + }else{ + numRead = read(m_fd, buf, BUF_LEN); + if (numRead == -1){ + printf("\033[1;31;40mread event error\033[0m\n"); + fflush(stdout); + assert(false); + } + this->eventProcess(buf, numRead); + } + } + + +// QTimer* liveTime = new QTimer(); +// //restart inotify-index proccess per minute +// liveTime->setInterval(60000); +// liveTime->start(); //I don't know how to use QTimer, wish someone can fix it! //MouseZhangZh @@ -338,15 +378,15 @@ fork: //// _exit(0); // *b_timeout = 1; // }); - for (;;){ +// for (;;){ // qDebug() << "liveTime->remainingTime():" << liveTime->remainingTime(); - numRead = read(m_fd, buf, BUF_LEN); - this->eventProcess(buf, numRead); - if (liveTime->remainingTime() < 1){ - qDebug() << "liveTime->remainingTime():" << liveTime->remainingTime(); - _exit(0); - } - } +// numRead = read(m_fd, buf, BUF_LEN); +// this->eventProcess(buf, numRead); +// if (liveTime->remainingTime() < 1){ +// qDebug() << "liveTime->remainingTime():" << liveTime->remainingTime(); +// _exit(0); +// } +// } } else if (pid > 0){ memset(buf, 0x00, BUF_LEN);