Task scheduling

I am attempting to take a project using an embedded controller and its own proprietary operating system and port it to linux. One of the features of this operating system is that every task gets a set reschedule interval, and the operating system automatically schedules the task to run every time this interval expires. Here's an example:

main () { rsi(25); /* set reschedule interval to 25 time ticks (1/4 second)

*/

while(1) { /* forever loop */

/* ... do task stuff here ... */

pause(); /* put task to sleep until the next reschedule interval

*/ } }

What this will do is run everything inside the forever loop every 250 milliseconds.

What's the best way to do this under linux?

Reply to
Mark Sokos
Loading thread data ...

Not quiete what the OP wants. Actually you need to do

while(1) { gettimeofday(&tv,NULL); /* do task stuff */ gettimeofday(&tv2,NULL); usleep(25000-(tv2.tv_usec - tv1.tv_usec)); }

Otherwise the loop is not exectuted _every_ 25ms.

--
42Bastian
Do not email to bastian42@yahoo.com, it's a spam-only account :-)
Use @epost.de instead !
Reply to
42Bastian Schick

What this OS does seems to be "cooperative multitasking" instead of "preemptive multitasking" that is done in Linux.

Preemptive multitasking can interrupts each task at any time when an event (e.g. an interrupt or inter-task communication) occurs or a time slice expires. Now it does a rescheduling and reassigns priorities and "running" flags. So another task can be made the running one. The advantage is that at any given time (hopefully: see priority inversion) the most important task is running. The downside is that you need to do precautions if resources are used by multiple tasks. Usually you need to use semaphores to prevent conflicts.

In cooperative multitasking systems the tasks can't be interrupted unless they agree (e.g. call "pause()").

Thus moving your tasks to preemptive mode will be quite a lot of work, especially if the code of the tasks is not yours, finding out the critical resources.

But I think it's easy to leave everything as it is and do a cooperative multitasking on top of Linux (if the performance (i.e. maximum latency of this way is sufficient in that application).

Either

(1) you use just a single Linux task for all your tasks and call the tasks as functions that (kind of) return when they call "pause()"

or

(2) (perhaps even easier) using a linux task for each of your tasks, you use a linux semaphore for each task (linux semaphores are groups of semaphores anyway) that is requested in the "pause()" function and thus might block the running task. Before blocking, in the "pause()" function the semaphore of the next task to be run will be decremented (or is it incremented ?) (linux semaphores are "counting"). So that this task is going to be executed (out of it's own "pause()" call. In the "pause()" code you can do whatever scheduling you like to decide which semaphore to update and thus make the most appropriate of your tasks do the next "cycle".

-Michael

Reply to
Michael Schnell

ElectronDepot website is not affiliated with any of the manufacturers or service providers discussed here. All logos and trade names are the property of their respective owners.