minimal POSIX compliant sleep

Related tags

Miscellaneous sleep
Overview

Minimal POSIX compliant sleep.

Results

Only 3 syscalls per execution

$ strace ./sleep 1
execve("./sleep", ["./sleep", "1"], 0x7ffc998d09a8 /* 40 vars */) = 0
nanosleep({tv_sec=1, tv_nsec=0}, NULL)  = 0
exit(0)                                 = ?
+++ exited with 0 +++

Compared to an empty C program compiled with gcc

$ strace ./a.out
execve("./a.out", ["./a.out"], 0x7ffe2f0aa150 /* 40 vars */) = 0
brk(NULL)                               = 0x561007006000
arch_prctl(0x3001 /* ARCH_??? */, 0x7ffeacd2dff0) = -1 EINVAL (Invalid argument)
access("/etc/ld.so.preload", R_OK)      = -1 ENOENT (No such file or directory)
openat(AT_FDCWD, "/etc/ld.so.cache", O_RDONLY|O_CLOEXEC) = 3
fstat(3, {st_mode=S_IFREG|0644, st_size=116514, ...}) = 0
mmap(NULL, 116514, PROT_READ, MAP_PRIVATE, 3, 0) = 0x7f74729a9000
close(3)                                = 0
openat(AT_FDCWD, "/usr/lib/libc.so.6", O_RDONLY|O_CLOEXEC) = 3
read(3, "\177ELF\2\1\1\3\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0\220\202\2\0\0\0\0\0"..., 832) = 832
pread64(3, "\6\0\0\0\4\0\0\[email protected]\0\0\0\0\0\0\[email protected]\0\0\0\0\0\0\[email protected]\0\0\0\0\0\0\0"..., 784, 64) = 784
pread64(3, "\4\0\0\0\20\0\0\0\5\0\0\0GNU\0\2\0\0\300\4\0\0\0\3\0\0\0\0\0\0\0", 32, 848) = 32
pread64(3, "\4\0\0\0\24\0\0\0\3\0\0\0GNU\0\207\360\21\247\344\314?\306\nT\320\323\335i\16t"..., 68, 880) = 68
fstat(3, {st_mode=S_IFREG|0755, st_size=2159552, ...}) = 0
mmap(NULL, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f74729a7000
pread64(3, "\6\0\0\0\4\0\0\[email protected]\0\0\0\0\0\0\[email protected]\0\0\0\0\0\0\[email protected]\0\0\0\0\0\0\0"..., 784, 64) = 784
mmap(NULL, 1868448, PROT_READ, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x7f74727de000
mmap(0x7f7472804000, 1363968, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x26000) = 0x7f7472804000
mmap(0x7f7472951000, 311296, PROT_READ, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x173000) = 0x7f7472951000
mmap(0x7f747299d000, 24576, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x1be000) = 0x7f747299d000
mmap(0x7f74729a3000, 12960, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0) = 0x7f74729a3000
close(3)                                = 0
mmap(NULL, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f74727dc000
arch_prctl(ARCH_SET_FS, 0x7f74729a8540) = 0
mprotect(0x7f747299d000, 12288, PROT_READ) = 0
mprotect(0x5610050fc000, 4096, PROT_READ) = 0
mprotect(0x7f74729f2000, 4096, PROT_READ) = 0
munmap(0x7f74729a9000, 116514)          = 0
exit_group(0)                           = ?
+++ exited with 0 +++

Small binary

$ du -b ./sleep /bin/sleep ./a.out
864     ./sleep
39048   /bin/sleep             # default arch sleep binary
14184   ./a.out                # empty C program compiled with gcc

The above measurement is the apparent size, as modern file systems allocate memory in blocks of 4096 bytes, further reducing the size won't actually change the disk usage reported below.

$ du -B1 ./sleep /bin/sleep ./a.out
4096    ./sleep
40960   /bin/sleep             # default arch sleep binary
16384   ./a.out                # empty C program compiled with gcc

Compilation

Compilation dependencies:

  • gcc
  • make
make sleep

Usage

./sleep SECONDS

where SECONDS is a non negative number (e.g. 15, 12.003)

You might also like...
Jimp-native is a fast C++ re-implementation of Jimp with zero system dependencies and minimal overhead!

Jimp native Make your sever-side Jimp code run 10x faster! Jimp-native is a fast C++ re-implementation of Jimp with zero system dependencies and minim

Tails is a fast, minimal Forth-like interpreter core in C++ with no assembly

Tails is a minimal, fast Forth-like interpreter core. It uses no assembly code, only C++, but an elegant tail-recursion technique inspired by Wasm3 makes it nearly as efficient as hand-written assembly. I created it as a one-day hack to celebrate May Forth 2021 … then kept going because it's fun.

mstatus is a super minimal entirely signal based statusbar for DWM.

mstatus is a uber-minimalistic signal based statusbar for DWM. All components of the statusbar are modules that must either be written by the user, or written by someone else and copied by the user.

WARD is  a minimal, performant infinite whiteboard app for wacom tablets
WARD is a minimal, performant infinite whiteboard app for wacom tablets

WARD: WARD 'Aint Really for Drawing No-nonsense infinite whiteboard, in the suckless tradition. This lacks: multiple types of brushes. layers. save/lo

A Minimal Web Browser with Built-in Adblocker in Less Than 100 Lines of Code
A Minimal Web Browser with Built-in Adblocker in Less Than 100 Lines of Code

A Minimal QtWebEngine Web Browser with Adblocker How Does It Work This is a minimal network filter implementation using QWebEngineUrlRequestIntercepto

Minimal-ish implementation of Unreal Online Subsystem + ImGui UI

Minimal-ish implementation of Unreal Online Subsystem + ImGui UI

Miryoku is an ergonomic, minimal, orthogonal, and universal keyboard layout.
Miryoku is an ergonomic, minimal, orthogonal, and universal keyboard layout.

Miryoku is an ergonomic, minimal, orthogonal, and universal keyboard layout. This is the Miryoku implementation for QMK.

Extremely minimal raw Wayland greeter for greetd.
Extremely minimal raw Wayland greeter for greetd.

greetd-mini-wl-greeter An extremely simple raw Wayland greeter for greetd, inspired by lightdm-mini-greeter. Usage Follow the same steps as for e.g. g

Minimal setup required to develop firmware for STM32 using ceedling
Minimal setup required to develop firmware for STM32 using ceedling

STM32 & Ceedling Base Project I created this project as a starting point for anyone looking to create production quality firmware for an embedded syst

Owner
null
Just loop forever, with sleep for specified seconds

loopever Just loop forever, with sleep for specified time Build & Install $ mkdkir build $ cd build $ cmake .. $ make $ make install Run $ loopever 0

Tomohito Nakayama 1 Oct 24, 2021
Save battery power and put your ESP32 to (deep) sleep - but what happens when it wakes up?

#229 ESP32 Deep Sleep Save battery power and put your ESP32 to (deep) sleep - but what happens when it wakes up? Direct link to video: https://youtu.b

Ralph Bacon 5 Nov 15, 2022
Simulate Linux Completely Fair Scheduler (CFS) using POSIX Threads

Linux CFS Simulator Simulate Linux Completely Fair Scheduler (CFS) using POSIX Threads. Build and Run $ make $ ./cfs-sim Note: The process status tabl

null 19 May 4, 2022
This program try to recreate bash --posix comportement in certain limite

minishell : petitcoquillage This program try to recreate bash --posix comportement in certain limite Execution : To execute this Program you have to b

null 63 Dec 9, 2022
An efficient, small mobile key-value storage framework developed by WeChat. Works on Android, iOS, macOS, Windows, and POSIX.

中文版本请参看这里 MMKV is an efficient, small, easy-to-use mobile key-value storage framework used in the WeChat application. It's currently available on Andr

Tencent 15.4k Jan 8, 2023
Implementation of POSIX mqueue using System V messages

mqueue-mac Implementation of POSIX mqueue using System V messages. 23-January-2022 James P. Parziale Description I got frustrated not being able to te

Jim-P 0 Jul 3, 2022
A minimal viable programming language on top of liblgpp

This project aims to implement a minimal viable programming language on top of liblgpp. setup The project requires a C++17 compiler, CMake and liblgpp

Andreas Nilsson 73 Jun 28, 2021
A Minimal, Header only Modern c++ library for terminal goodies 💄✨

rang Colors for your Terminal. Windows Demo Example usage #include "rang.hpp" using namespace std; using namespace rang; int main() { cout << "P

Abhinav Gauniyal 1.3k Dec 30, 2022
Minimal tool for measuring cost of mode switch

CPU mode switch statistics The mode-switch-stat tool measures the cost of CPU mode switch, the round trip between user and kernel mode. At present, th

Steven Cheng 12 Feb 22, 2022
A very minimal & simple text editor written in C with only Standard C Library.

Texterm Text Editor A very minimal & simple text editor written in C with only Standard Library. Syntax highlighting supported for C JavaScript Python

Biraj 40 Dec 8, 2022