//WECM100/sleep.c
//decription: basic c for system programming
//machine: WSL on window11
//os: "Ubuntu 22.04.2 LTS"
//language: gcc --version, gcc (Ubuntu 11.4.0-1ubuntu1~22.04) 11.4.0
//> wsl
//> uname -avLinux LAPTOP-2E5CQCBT 6.6.36.3-microsoft-standard-WSL2 #1 SMP //
//PREEMPT_DYNAMIC Sat Jun 29 07:01:04 UTC 2024 x86_64 x86_64 x86_64 GNU/Linux
//> cat /etc/os-release PRETTY_NAME="Ubuntu 22.04.2 LTS"
//$ gcc sleep.c -o sleep
//$ ./sleep
// Sleeping for 10 seconds...
// Slept for 10.00 seconds
//$ ps -a
// PID TTY TIME CMD
// 900 pts/1 00:00:00 bash
// 19714 pts/0 00:00:00 sleep
// 19765 pts/2 00:00:00 ps
#include< stdio.h> // for printf
#include< unistd.h> // for sleep
#include< time.h> // for time
int main(void){ // main that sleeps for 10 seconds.
time_t startTime, endTime;
int sleepDuration = 10;
printf("Sleeping for %d seconds...\n", sleepDuration);
time(&startTime);
sleep(sleepDuration);
time(&endTime);
printf("Slept for %.2f seconds\n", difftime(endTime, startTime));
}
//--------------------------------------------------