【发布时间】:2014-01-03 13:59:28
【问题描述】:
我有非常简单的代码,应该在后台运行并在凌晨 1 点关闭计算机:
#include <ctime>
#include <cstdlib>
#include <unistd.h>
int main() {
time_t t;struct tm * now;
daemon(0,0);
while(1){
t = time(0);
now = localtime( & t );
if(now->tm_hour==1){
system("shutdown -P");
break;
}
sleep(10);
}
return 0;
}
代码在没有睡眠(10)的情况下工作,但使用了整个空闲内存,所以我需要睡眠功能来停止循环并每十秒重新检查一次时间,但睡眠功能程序在我运行后立即停止。
【问题讨论】:
-
你怎么知道它停止了?您调用 daemon() 以便它将自己置于后台,并且不会消耗任何明显的资源/CPU。
-
查看daemon()的返回值
-
附带说明,您知道
shutdown已经拥有您正在实施的功能吗? (shutdown -P 01:00)