By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,986 Members | 1,905 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,986 IT Pros & Developers. It's quick & easy.

how to interrupt a c program periodically?

P: 16
i write an ANSI C program that run many tasks
i need to run a scheduler that interrupt the running process each 1second for example and run a certain program and after finishing it return back to continue the previously interrupted scheduler.
i use only ANSI c,not c++,for a project constraint,and compiler c++ builder 6 ,under windows 7 environment
Sep 18 '10 #1

✓ answered by Banfa

Sorry to join late. I did something like this, ported an RTOS scheduler on Windows for testing purposes for an RTOS several years ago (well a decade).

The easiest way to think of embedded processor using an RTOS to provide threads is as a single process. The embedded environment and the single process with multiple threads both all any thread access to any piece of valid memory, which is quite important.

What I did emulating on windows was start a thread to be the timer interrupt and run the scheduler and then start a windows thread for each thread of the RTOS. The trick I used was to start all the threads except the timer thread suspended and then the scheduler, rather than actually perform context switches just resumed the Windows thread of the RTOS thread that was supposed to be running.

The simulation is not perfect, but then how perfectly can you run a simulation for a 50MHz micro processor on a 300MHz PC without actually writing a emulator for the micro processor and running native code.

But it worked well enough for test because it allowed the true logic of the RTOS scheduler to control the running thread in the Windows simulation.

Share this Question
Share on Google+
14 Replies


weaknessforcats
Expert Mod 5K+
P: 9,197
I believe you can use the Windows scheduler for this. Check the Platform SDK for the system calls that allow you to manage your processes.

I don't believe you need to write the scheduler yourself unless this is some part of a homework assignment.
Sep 18 '10 #2

P: 16
the scheduler is a part of my project ,a description again
my algorithm is to write a main function that is have an infinite cycle to do nothing but will be interrupted every 1 minute for example to run a set of process.
and my quistion is how to interrupt my main function periodically and when finish return back to it under windows environment,after this simulation on windows the interrupt routines for an embedded processor will be used in a really application.
Sep 18 '10 #3

weaknessforcats
Expert Mod 5K+
P: 9,197
Your "prcesses" spawned in main() are probably threads. I don't believe you can have actual processes since C has no access to OS scheduler. You do that through system calls.

So in main() you start a thread and then start a timer. When the timer reaches on second you raise an event to stop the thread.

The thread, of course, has to be in an interrupt loop looking for that pause event. When it sees it, it drops into an inner loop looking for the resume event. When it sees that it drops back into the outer interupt loop looking for the pause event again.

If you have a loop in main() you can call a series of functions that start timers followed by a thread start.

Kind of like a server structure.

Presumably, main could read a disc file that contains the names of the processes to execute plus any run-time parameters, etc.
Sep 18 '10 #4

P: 16
my project is
1-embeded rel time operating system
2-the proplem description is to simulate the schedular on windows enviroment
3-the task of the schedular is to interrupt (using timer interrrupt of the embeded microprocessor) to run a certain process/function/threads whatever each certain period of time
4-because i cant simulate that on a PC or in windows environment i want to have a temperory c code to interrupt my schedular which in c code each 1 second for eg. to run a another c function.
so my question again?
"i need only a C# code function to save the current position of a running c# function P1 and pause it to run another process P2 each certain period and return back to the last position in P1"
Note:plz i dont need anthing depend on windows,thanx alot
Sep 19 '10 #5

weaknessforcats
Expert Mod 5K+
P: 9,197
You won't be able to interrupt your C code.

Your C code will run to completion.

That's because the C code is on a thread and a thread is not interruptible at the C level unless it has some sort of internal loop where it checks each cycle of the loop to see if it should continue for another cycle.

Also, C code is not set up to run a function at a time unless the function is its own thread. That puts you back to the previous paragraph.

You will need to write a preemptive thread manager at the processor level to do what you want to do. This code is beyond my level of competance.
Sep 19 '10 #6

P: 16
thank you too much,i appreciate your effort with me
but it is a hard real time OS and i will use processor interrupt level at end,but i need just to simulate it first on windows environment.so is thier any other solution that go in sleep for a period and wake to stop the runnin process and run another and then return back
Sep 19 '10 #7

weaknessforcats
Expert Mod 5K+
P: 9,197
Unless you are at the processor level and can switch OS threads, the executing thread/process can't be stopped unless the executing thread/process is checking perioically to see if it should coninue.

On Windows this is a Ring 0 operation. This is far beyond the power of C.

For a Windows simulator, you write a series of functions (your threads) that contain a loop testing the return of WaitForSingleObject. Maybe wait for 5 seconds then continue.

In main() you call SetEvent using the thread id of the target thread. That thread will pick up the event and pause. In main() you can now start another thread.

If there is a loop in main(), you can start/stop these threads at whatever timing interval os built into the loop.

This covered in detail in the book Windows via C/C++ by Jeffrey Richter.
Sep 20 '10 #8

P: 16
thank you weaknessforcats but a last question can i do that in dos environment
Sep 21 '10 #9

weaknessforcats
Expert Mod 5K+
P: 9,197
Yes you can.

There is nothing here that requires a GUI.

If you can use Visual Studio.NET, you just create an empty Win32 project.

You would #include windows.h and then just write a nromal main().
Sep 21 '10 #10

Oralloy
Expert 100+
P: 983
This may sound a little lame, but there are several complete examples of trivial real-time operating systems available on the 'net.

No, you don't have to start with Linux, but there are a lot of others available. If you start with the Wikipedia RTOS page, you'll get a good start.

Luck!
Sep 21 '10 #11

Banfa
Expert Mod 5K+
P: 8,916
Sorry to join late. I did something like this, ported an RTOS scheduler on Windows for testing purposes for an RTOS several years ago (well a decade).

The easiest way to think of embedded processor using an RTOS to provide threads is as a single process. The embedded environment and the single process with multiple threads both all any thread access to any piece of valid memory, which is quite important.

What I did emulating on windows was start a thread to be the timer interrupt and run the scheduler and then start a windows thread for each thread of the RTOS. The trick I used was to start all the threads except the timer thread suspended and then the scheduler, rather than actually perform context switches just resumed the Windows thread of the RTOS thread that was supposed to be running.

The simulation is not perfect, but then how perfectly can you run a simulation for a 50MHz micro processor on a 300MHz PC without actually writing a emulator for the micro processor and running native code.

But it worked well enough for test because it allowed the true logic of the RTOS scheduler to control the running thread in the Windows simulation.
Sep 22 '10 #12

Oralloy
Expert 100+
P: 983
@banfa,

I'll betray my ignorance here and ask if the scheduler thread could suspend the individual running threads.

I assume that your scheduler thread ran at a higher priority than the worker threads, so that it would actually recieve the timer tic asynchronously.

Cheers!
Sep 22 '10 #13

Expert 100+
P: 2,396
One way to model a preemptive scheduler on DOS or Windows would be to design the thread functions so that they expect to be called repeatedly, with each invocation accomplishing only a little bit of work. The functions would have to maintain an internal state so that they could pick up where they left off. The scheduler then repeatedly calls the thread functions in an order derived from their simulated priorities. Preemption occurs by not calling a function the next time around.

You could model an RTOS this way, but it would be more of a throwaway model -- this thread architecture probably wouldn't port well to the real RTOS.

I concede that this is in actuality a round-robin scheduler being used to model a preemptive scheduler. In this scheme you trade off a simpler scheduler for more complicated threads.
Sep 22 '10 #14

Banfa
Expert Mod 5K+
P: 8,916
Oralloy: I assume that your scheduler thread ran at a higher priority than the worker threads, so that it would actually recieve the timer tic asynchronously.
Nothing so sophisticated I am afraid.

The scheduler thread did run at a higher priority to ensure if got the run time it needed but it was not receiving a timer tick. As I recall it just had a dirty great big sleep in the loop and effectively generated the tick itself with added extra inaccuracy.

Again back then accuracy wasn't much of a problem because Windows could manage a 50ms time slice at best where as the embedded platform used a 10ms time slice so there was no chance of Windows ever accurately mimicking this.
Sep 22 '10 #15

Post your reply

Sign in to post your reply or Sign up for a free account.