我正在尝试在C
中运行x
分钟的程序。我需要让child
进程在这段时间内进入休眠状态。任何帮助,将不胜感激。基本上我想了解fork()
和sleep()
是如何工作的。这里是我的代码片段如何在C中运行一个程序x分钟?
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
int i = fork();
printf("fork return value = %d\n", i);
printf("this is the time before sleep");
system("date +%a%b%d-%H:%M:%S");
printf("\n");
if (i==0){
sleep(120);
}
system("ps");
printf("this is the time after sleep");
system("date +%a%b%d-%H:%M:%S");
printf("\n");
}
只是一个仅供参考 - 睡眠()不能保证,只要你问到实际入睡。它可能被信号中断。如果你真的想等一段时间,你应该检查sleep()的返回值。如果在睡眠中剩下时间,秒数会返回,您可以再次请求睡眠时间更长。 – FatalError 2012-01-28 05:38:44