Why delay is not available in gcc? As per my understanding delay function halt the program while sleep function make program to sleep and perform context switching. If I want to halt my program what is the available option in gcc?
-
You want to prevent context switching? As in to bring down the machine? You simply need to `sleep` on a non-multitasking OS. Linux won't do. – Potatoswatter Dec 06 '12 at 06:39
-
possible duplicate of [implement time delay in c](http://stackoverflow.com/questions/3930363/implement-time-delay-in-c) (At least, as much as I can decipher the question) – Brian Roach Dec 06 '12 at 06:42
-
@Potatoswatter I want system to halt ...Its my program requirement – Mohaqiq Dec 06 '12 at 06:48
-
@Brian Roach the thread you pointed also using sleep and time functions which are making context switching... – Mohaqiq Dec 06 '12 at 06:50
-
1You might want to say why or what kind of program would have such a requirement. Typically that would be the definition of a very hard crash. – Potatoswatter Dec 06 '12 at 06:52
-
Program requirement I mentioned in comment of below Answer. Anyhow I also want to know the answer for WHY as well. – Mohaqiq Dec 06 '12 at 07:01
-
use algorithm like: `now=time(); later=now+60; while(time() <= later) asm("nop");` – anishsane Dec 06 '12 at 07:25
-
If you want to "halt" the program, you can let one thread sleep() and another thread burn away CPU in a loop, waiting for the sleeper thread to wake up. Why you would ever want to do such a weird thing, I have no idea. There is never a guarantee that a context switch won't happen at any place in your program. Linux is not a RTOS. – Lundin Dec 06 '12 at 09:05
2 Answers
Because delay
is, as far as I know, not in the C99 or C2011 standard.
The C standard is quite poor on APIs (it mostly knows about FILE*
, some memory operations like malloc
, a few string operations, and setjmp
...; it does not know about directories or delays).
Context switching does not make sense in pure standard C. (it only makes sense on multi-tasked operating systems à la Unix).
A delay function should be provided by the (non-standard-C) programming environment. On Linux, it is a superset of Posix which indeed have sleep and many other functions. You could use sched_yield(2) syscall to "force" a context switch.
You should read some book like Advanced Linux Programming. Perhaps to stop the system you want to use the sync(2) and reboot(2) syscalls, but this is probably a bad idea; you probably want to run, e.g. with system(3)
, the shutdown
or halt
command.
If you just want to stop your particular program (and let the rest of the system run other processes as usual), simply call exit(3) which is in the C standard.
PS. If you write a kernel device driver you should state that explicitly in the question and you really should learn a lot more about Linux. Inside some driver you might call some panic function when in deep trouble.

- 223,805
- 18
- 296
- 547
-
He wants to *prevent* a context switch. `sched_yield` has the same effect as `sleep` in that regard. "Solving" his problem involves leaving Linux entirely, or running the program in the kernel. – Potatoswatter Dec 06 '12 at 06:51
-
Agreed, and I did wrote that `sched_yield` will force a context switch. Actually, I have the strange feeling that the original poster *Mustafa* does not understand Linux, and will have a lot of trouble if he does not learn more.... – Basile Starynkevitch Dec 06 '12 at 06:53
-
And even if `delay` was in the C Standard, you would find it in the libc and not in `gcc`. – ouah Dec 06 '12 at 06:53
-
I am writing a device driver. During two read or write operations system should halt for few second.... – Mohaqiq Dec 06 '12 at 07:00
-
1A device driver runs inside the kernel. It is not a C program (it often is a kernel module). Technically the entire kernel is a freestanding implementation and does not follow the usual C standard. But before writing any driver, you should learn a big lot about Linux (read several books, be fluent with application level system programming....). – Basile Starynkevitch Dec 06 '12 at 07:01
-
1Basile - excellent response to a regrettable question. Sincere kudos. – Brandt Solovij Dec 06 '12 at 07:02
If you do sleep()
then OS promptly switch context. Doesn't this behaviour satisfy you? In other words, do you want to hold CPU and do nothing for a given period? Then do busy_waiting; for(; ; ;) break_if_time_elapses
. Even then, you cannot escape time-slice based context switching by OS. So it's meaningful for very short time, less than OS's time slice.

- 1,223
- 10
- 17
-
thanks cwyang this could be an option but how to impose some specified amount of time for example halt for 3 sec. – Mohaqiq Dec 06 '12 at 06:58
-
Inside the kernel you cannot stop for 3 seconds. If you did that, you could physically **break the hardware** (e.g. because some fans would not be active). – Basile Starynkevitch Dec 06 '12 at 07:06
-
@Basile Starynkevitch thanks for your help. I think I need to learn alot :( – Mohaqiq Dec 06 '12 at 07:08
-
No you need to learn a very big lot. That means months of learning. Don't play inside the kernel till you are not fluent with application level system programming. – Basile Starynkevitch Dec 06 '12 at 07:09