当该应用程序中加速文件的最后一个修改时间发生更改时,如何重新启动应用程序
how to reboot the application when there is a change in last modification time of an accesed file in that application
我是线程编程的新手。我正在尝试创建一个应用程序,该应用程序不断检查某些文件的最后一个修改时间并在此时间更改时退出程序。
请在下面找到我的代码:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <iostream>
#include <cstdlib>
#include <pthread.h>
#include <cerrno>
#include <unistd.h>
using namespace std;
#define NUM_THREADS 2
void *getFileCreationTime(void *path) {
const char *pt;
pt=(const char *)path;
struct stat attr;
stat("/home/utthunga/shmrp.cpp", &attr);
while(1){
char *timestamp= ctime(&attr.st_mtime);
if(timestamp)
{
cout<<"Last modified time: %s"<< ctime(&attr.st_mtime)<<endl;
cout<<"No changes has been made to the file"<<endl;
sleep(4);
}
else
{
cout<<"Last modified time: %s"<< ctime(&attr.st_mtime)<<endl;
cout<<"Time stamp has been changed"<<endl;
exit(0);
}
}
pthread_exit(NULL);
}
int main()
{
pthread_t threads[NUM_THREADS];
int i;
int rc;
for( i = 0; i < NUM_THREADS-1; i++ )
rc = pthread_create(&threads[i], NULL, getFileCreationTime, (void *)i);
pthread_exit(NULL);
return 0;
}
任何人都可以告诉我,我必须实施什么更改才能连续检查该文件的最后一个修改时间并在此时间更改时退出应用程序?
第一次检索文件的修改时间后,您需要保存它,因此您可以将其与之后的后续值进行比较。
尝试更多这样的东西:
void* getFileCreationTime(void *) {
const char *path = "/home/utthunga/shmrp.cpp";
struct stat attr;
if (stat(path, &attr) < 0) {
cout << "stat error" << endl;
exit(0);
}
time_t mtime = attr.st_mtime;
cout << "Last modified time: " << ctime(&mtime) << endl;
while(1) {
sleep(4);
if (stat(path, &attr) < 0) {
cout << "stat error" << endl;
exit(0);
}
if (attr.st_mtime != mtime) {
cout << "Time stamp has been changed" << endl;
exit(0);
} else {
cout << "No changes have been made to the file" << endl;
}
}
pthread_exit(NULL);
}
相关文章:
- WINAPI 注册应用程序重新启动时不清除打开的套接字
- 如何在 MFC 应用程序中启动 ZeroMQ 线程?
- 从最基本的Qt应用程序重新链接对象 - 链接器错误
- 当使用“空dllmain”的dll加载链接到DLL时,应用程序无法启动(0xc0000142)
- 无法加载dll libglorycolx2010.dll.该应用程序未能启动,因为其并排配置不正确.Hresult:0x
- 此应用程序无法启动,因为它找不到或加载Qt平台插件"windows"
- 您如何从已经运行的GUI应用程序中启动使用GUI的可执行文件
- Eclipse PTP挂起OpenMPI应用程序的启动调试会话
- 系统会冻结如果我使用运行的守护程序重新启动或关闭,该守护程序可以使用Fanotify控制对文件的访问
- OpenGL 应用程序在启动时立即崩溃
- 保存“编辑控件”用户输入并在应用程序重新启动C++Visual Studio时进行恢复的有效方法
- 使用 tcp 命令在 c++ 应用程序中启动函数
- 当将应用程序作为启动的守护程序运行时,C++ 库中的信号 10 (SIGBUS)
- 在Windows 7和8下,其他应用程序无法启动openfiles.exe
- 将QCheckBox的状态保存在文件中,并在程序重新启动时加载状态
- 应用程序无法启动,因为缺少平台插件“窗口”
- Win7 / Eclipse / OpenCV / MinGW:应用程序每次启动时都会崩溃
- 此应用程序无法启动,因为它找不到或加载Qt平台插件"xcb"
- 如何在 Windows 服务应用程序中启动系统时运行代码
- Qt Windows 部署:应用程序无法启动