From: http://www.utdallas.edu/~kcooper/teaching/5375/fall08/Tutorial8/tutorial8.htm
(Section "Difference between bash sleep and sleep in C program"):
There is a major difference between how bash sleep command and sleep function of C program works. The sleep function of C programs is a simple function call. When called, it gives up the CPU to the operating system so that it can schedule other processes which need CPU. Then after the specified amount of seconds, the process which called sleep starts working again.
The sleep command bash works differently. When this command is invoked, it creates a child process of itself. It runs for the specified amount of seconds and then exits back to the calling process.
- Why does bash sleep so complicated? (Why does bash sleep is realized through the forking?)
- Why bash sleep can’t be like C sleep?
Because that’s what bash does, it runs programs (which on unixes is done through fork()/exec() ) It so just happens that someone wrote a program,
/bin/sleepwhich just sleeps for the specified amount of seconds.It could, if someone implemented sleep as a built in command/function to bash, but no-one has yet, and likely the benefit is not worth it.