← 返回首页
Linux高级程序设计(二十九)
发表时间:2021-11-17 22:36:35
进程挂起

sleep函数用来挂起当前进程。

函数名: sleep

#include <unistd.h> 
unsigned sleep(unsigned seconds);
//此外还有
int usleep (useconds_t usec);//n微秒

注意:sleep这个函数是可以被中断的,也就是说当进程在睡眠的过程中,如果被中断,那么当中断结束回来再执行该进程的时候,该进程会从sleep函数的下一条语句执行;这样的话就不会睡眠time秒了;

实例:

#include <stdio.h>  
#include <sys/types.h>  
#include <unistd.h>

int main(int argc, char const *argv[]){
    while(1){
        printf("hello,world!\n");
        sleep(2);
    }
    return 0;
}

测试运行:

[root@iz2zefozq9h39txdb8s7npz shelldemo]# ./a.out
hello,world!
hello,world!
hello,world!