473,412 Members | 2,072 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,412 software developers and data experts.

how to interrupt a c program periodically?

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.

14 6719
weaknessforcats
9,208 Expert Mod 8TB
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
sedefy
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
9,208 Expert Mod 8TB
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
sedefy
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
9,208 Expert Mod 8TB
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
sedefy
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
9,208 Expert Mod 8TB
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
sedefy
16
thank you weaknessforcats but a last question can i do that in dos environment
Sep 21 '10 #9
weaknessforcats
9,208 Expert Mod 8TB
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
988 Expert 512MB
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
9,065 Expert Mod 8TB
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
988 Expert 512MB
@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
donbock
2,426 Expert 2GB
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
9,065 Expert Mod 8TB
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

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

Similar topics

26
by: djw | last post by:
Hi, Folks- I have a question regarding the "proper" use of try: finally:... Consider some code like this: d = Device.open() try: d.someMethodThatCanRaiseError(...) if SomeCondition: raise...
3
by: Daragoth | last post by:
Hi, I'm writing a program using Metrowerks CodeWarrior 4.0 that determines the best possible combination of a data set by checking every possible combination. I found there were about 250,000,000...
2
by: Marco | last post by:
Hallo, I have made a microcontroller with keyboard (16 key). I have a function to handle interrupt that comes from keyboard when a key is pressed. I should make a C software (not graphic) with...
6
by: Eranga | last post by:
Hello All, I need to run one program from another within time slots specified by the user. Could some one please tell me how this could be achieved? The program that should be run is created in...
4
by: Eranga | last post by:
I want to add a program to windows which will run at a specified time period which may be changed by the administrator.What I plan to do is start the program at windows startup and then within that...
2
by: Hartmut | last post by:
Hi guys out there..., I am not shure if this is the right forum for my Question. Have a problem using my own Keyboard interrupt handler. The handler works fine as long as i do not press keys...
6
by: excite | last post by:
I want to run a function periodically by using Python, and I found that time.sleep() may be of help, however, I think it may not be safe and flexible. Can somebody give me some suggestions on it?...
6
by: BlueBird | last post by:
Hi, I have a program with a master thread and several slave threads. Whenever an exception occurs, in the master thread or in one of the slave threads, I would like to interrupt all the...
7
by: Anil | last post by:
I have a Javascript program which runs in the browser and has functions work(), and stop(). It listens to commands from the server to work() and can be interrupted by the server to stop(). I am...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.