-
大小: 64KB文件類(lèi)型: .zip金幣: 2下載: 0 次發(fā)布日期: 2021-06-10
- 語(yǔ)言: 其他
- 標(biāo)簽: Qt??守護(hù)進(jìn)程??
資源簡(jiǎn)介
沒(méi)有任何人敢保證自己寫(xiě)的程序沒(méi)有任何BUG,尤其是在商業(yè)項(xiàng)目中,程序量越大,復(fù)雜度越高,出錯(cuò)的概率越大,尤其是現(xiàn)場(chǎng)環(huán)境千差萬(wàn)別,和當(dāng)初本地電腦測(cè)試環(huán)境很可能不一樣,有很多特殊情況沒(méi)有考慮到,如果需要保證程序7*24小時(shí)運(yùn)行,則需要想一些辦法能夠讓程序死了能夠活過(guò)來(lái),在嵌入式linux上,大部分會(huì)采用看門(mén)狗的形式來(lái)處理,程序打開(kāi)看門(mén)狗驅(qū)動(dòng)后,定時(shí)喂狗,一旦超過(guò)規(guī)定的時(shí)間,則硬件軟復(fù)位等。這種方式相對(duì)來(lái)說(shuō)比較可靠,如果需要在普通PC機(jī)上運(yùn)行怎辦呢?本篇文章提供一個(gè)軟件實(shí)現(xiàn)守護(hù)進(jìn)程的辦法,原理就是udp通信,單獨(dú)寫(xiě)個(gè)守護(hù)進(jìn)程程序,專門(mén)負(fù)責(zé)檢測(cè)主程序是否存在,不存在則啟動(dòng)。主程序只需要啟動(dòng)live類(lèi)監(jiān)聽(tīng)端口,收到hello就回復(fù)ok就行。
為了使得兼容任意程序,特意提煉出來(lái)共性,增加了多種設(shè)置。
1:可設(shè)置檢測(cè)的程序名稱。
2:可設(shè)置udp通信端口。
3:可設(shè)置超時(shí)次數(shù)。
4:自動(dòng)記錄已重啟次數(shù)。
5:自動(dòng)記錄最后一次重啟時(shí)間。
6:是否需要重新刷新桌面。
7:可重置當(dāng)前重啟次數(shù)和最后重啟時(shí)間。
8:自動(dòng)隱藏的托盤(pán)運(yùn)行或者后臺(tái)運(yùn)行。
9:提供界面設(shè)置程序名稱已經(jīng)開(kāi)啟和暫停服務(wù)。

代碼片段和文件信息
#include?“applive.h“
#include?“qmutex.h“
#include?“qudpsocket.h“
#include?“qstringlist.h“
#include?“qapplication.h“
#include?“qdatetime.h“
#include?“qdebug.h“
#define?TIMEMS?qPrintable(QTime::currentTime().toString(“HH:mm:ss?zzz“))
QScopedPointer?AppLive::self;
AppLive?*AppLive::Instance()
{
????if?(self.isNull())?{
????????QMutex?mutex;
????????QMutexLocker?locker(&mutex);
????????if?(self.isNull())?{
????????????self.reset(new?AppLive);
????????}
????}
????return?self.data();
}
AppLive::AppLive(Qobject?*parent)?:?Qobject(parent)
{
????udpServer??=?new?QUdpSocket(this);
????QString?name?=?qApp->applicationFilePath();
????QStringList?list?=?name.split(“/“);
????appName?=?list.at(list.count()?-?1).split(“.“).at(0);
}
void?AppLive::readData()
{
????QByteArray?tempData;
????do?{
????????tempData.resize(udpServer->pendingDatagramSize());
????????QHostAddress?sender;
????????quint16?senderPort;
????????udpServer->readDatagram(tempData.data()?tempData.size()?&sender?&senderPort);
????????QString?data?=?QLatin1String(tempData);
????????if?(data?==?“hello“)?{
????????????udpServer->writeDatagram(QString(“%1OK“).arg(appName).toLatin1()?sender?senderPort);
????????}
????}?while?(udpServer->hasPendingDatagrams());
}
bool?AppLive::start(int?port)
{
????bool?ok?=?udpServer->bind(port);
????if?(ok)?{
????????connect(udpServer?SIGNAL(readyRead())?this?SLOT(readData()));
????????qDebug()?<????}
????return?ok;
}
void?AppLive::stop()
{
????udpServer->abort();
????disconnect(udpServer?SIGNAL(readyRead())?this?SLOT(readData()));
}
?屬性????????????大小?????日期????時(shí)間???名稱
-----------?---------??----------?-----??----
?????目錄???????????0??2019-03-02?14:23??live\
?????文件??????????82??2019-03-02?14:18??live\live.pro
?????目錄???????????0??2019-03-02?14:17??live\livedemo\
?????文件????????1666??2018-05-30?14:49??live\livedemo\applive.cpp
?????文件?????????446??2018-05-30?14:57??live\livedemo\applive.h
?????文件?????????201??2019-03-02?14:06??live\livedemo\frmmain.cpp
?????文件?????????284??2019-03-02?14:06??live\livedemo\frmmain.h
?????文件?????????495??2019-03-02?14:05??live\livedemo\frmmain.ui
?????文件?????????570??2019-03-02?14:04??live\livedemo\livedemo.pro
?????文件???????23658??2019-03-02?14:17??live\livedemo\livedemo.pro.user
?????文件?????????880??2019-03-02?14:05??live\livedemo\main.cpp
?????目錄???????????0??2019-03-02?14:17??live\livetool\
?????文件????????2487??2019-03-02?14:10??live\livetool\app.cpp
?????文件?????????849??2019-03-02?13:41??live\livetool\app.h
?????文件????????5584??2019-03-02?14:16??live\livetool\frmmain.cpp
?????文件?????????833??2019-03-02?14:15??live\livetool\frmmain.h
?????文件????????2773??2019-03-02?13:25??live\livetool\frmmain.ui
?????文件?????????595??2019-03-02?13:50??live\livetool\livetool.pro
?????文件???????29845??2019-03-02?14:17??live\livetool\livetool.pro.user
?????文件????????1301??2019-03-02?14:15??live\livetool\main.cpp
?????文件???????67646??2016-06-15?15:26??live\livetool\main.ico
?????文件??????????92??2019-03-02?13:50??live\livetool\main.qrc
?????文件????????2249??2018-08-22?21:39??live\livetool\trayicon.cpp
?????文件????????1826??2018-08-19?19:47??live\livetool\trayicon.h
?????目錄???????????0??2019-03-02?14:12??live\snap\
?????文件???????10924??2019-03-02?14:11??live\snap\QQ截圖20190302141124.png
?????文件???????16677??2019-03-02?14:11??live\snap\QQ截圖20190302141129.png
?????文件???????13291??2019-03-02?14:11??live\snap\QQ截圖20190302141147.png
評(píng)論
共有 條評(píng)論