controlling program execution
I am writing small debugging program for multithread apps. My idea is to run the target, that is being debuged for for example 100 nanosecs, then pause and ex开发者_开发技巧amine its memory. However this ( just pseudocode )
nanosleep(100); //sleep debuger for 100 nanosec and let a program run
kill(target_app_pid, SIGSTOP); //stop target app
wouldn't work, because process-switch could happen right after nanosleep and target would run longer as required. Is there any way in to give a process "defined" time slice and then suspend it? Only solution that I can imagine is add system calls and fix the scheduler to achieve what I require but this assumes great effort and many bugs. Perhaps setting "real-time" priority for debuger process can help? Would it be clean solution?
Assume too, that I can insrument source code of target app. Is it possible to set up some kind of timer and sleep the whole process after some (very small and precise) period of time?
Do you really think you can nanosleep
for 100 nanoseconds? What's the HZ
value in your kernel? The closest you get to this order of sleep is udelay()
, but that's in the kernel where you can disable preemption, not in user-land. Check out linux/include/delay.h
and init/calibrate.c
in the Linux kernel source.
Since linux is at best a "soft real time" OS (especially working in user space), it will be difficult to get the type of precision you are after.
If you are working on an embedded processor, the nanosleep
and microsleep
calls are often implemented as a spin loop. During boot, the processor is characterized to "calibrate" this spin loop. Still, however, getting this type of precision requires a very detailed understanding of the processor architecture and what is being executed (at the instruction level).
Newer versions of the linux kernel do have some high-speed timer implementations, but you are still looking at microsecond resolution.
If you are running on a desktop system, I have no idea why you need this level of precision.
精彩评论