我正在嘗試在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