方法一:使用exec函数实现程序重启
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main()
{
printf("Program is running...\n")
// 休眠5秒钟
sleep(5)
printf("Program will restart now...\n")
// 使用exec函数重启程序
char *args[] = {"./program", NULL}
execvp(args[0], args)
return 0
}
方法二:使用system函数实现程序重启
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main()
{
printf("Program is running...\n")
// 休眠5秒钟
sleep(5)
printf("Program will restart now...\n")
// 使用system函数重启程序
system("./program")
return 0
}
需要注意的是,无论是使用exec函数还是system函数,都需要将程序的可执行文件名作为参数传入。另外,程序在重启之前需要先将需要保存的数据进行保存,否则数据会丢失。
你既然知道system()函数,就用windows的任务计划呀也就是 at 命令。
按你说的意思即:system("at 12:00 net user")
若要每天12:00运行:system("at 12:00 at 12:00:00 /every:M,T,W,Th,F,S,Su net user")