APUE course source code
guowenxue
2 days ago 68826376ee5f47783c644c6604f4411ec747cd7e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
/*********************************************************************************
 *      Copyright:  (C) 2025 LingYun IoT System Studio
 *                  All rights reserved.
 *
 *       Filename:  sleep.c
 *    Description:  This file is sleep example code.
 *
 *        Version:  1.0.0(10/15/2025)
 *         Author:  Guo Wenxue <guowenxue@gmail.com>
 *      ChangeLog:  1, Release initial version on "10/15/2025 05:16:02 PM"
 *
 ********************************************************************************/
 
#include <stdio.h>
#include <time.h>
#include <unistd.h>
 
int main(void)
{
    struct timespec req;
 
    printf("Sleep 2 seconds using sleep()\n");
    sleep(2);
 
    printf("Sleep 0.5 seconds using nanosleep()\n");
    req.tv_sec = 0;
    req.tv_nsec = 500000000;  /* 0.5 秒 */
    nanosleep(&req, NULL);
 
    printf("Sleep 0.1 seconds using clock_nanosleep()\n");
    req.tv_sec = 0;
    req.tv_nsec = 100000000;  /* 0.1 秒 */
    clock_nanosleep(CLOCK_MONOTONIC, 0, &req, NULL);
 
    return 0;
}