Basics of task scheduling

Thread Starter

Parth786

Joined Jun 19, 2017
642
Embedded system is the type of system that is design to perform one or multiple task's.

These are the some example of tasks , I understand what is task in embedded system. I have been performed this task's with microcontroller.
  • led tasks to turn it on or off
  • LCD task that display a message
  • button task that turn on or off motor
What is task time and task priority?

I think We set a certain time to complete every task and we can decide which one and when task will perform.

Task Time : Every task has specific time
upload_2017-11-19_16-44-5.png

Task priority : Task priority decide which one task will perform first and then next and next ..
upload_2017-11-19_16-50-0.png

My purpose is to know about the task scheduling system. Is it task scheduling ?

Note : Please ignore task time that I have taken in example. It may be different in real example
 
Last edited:

Thread Starter

Parth786

Joined Jun 19, 2017
642
Please help me, Is the explanation given by me for task scheduling is correct ? I don't understand how do we assign task priority ?
 

JohnInTX

Joined Jun 26, 2012
4,787
Well, a simpler answer than reading 1000+ pages is that task priority has to do with how tasks are scheduled. Specifically:

When a task SUSPENDS i.e. is done processing for the present the SCHEDULER takes over.
The scheduler examines the list of current tasks running and has to pick one to run. There will be tasks that are blocked from running because they are waiting on something - a time delay, an input to be TRUE or something like that. The scheduler won't look at those until the next time.
That leaves tasks which are ready to run. There may be one or many and the scheduler has to pick one. That's where the priority comes in. Some things are more important than others. As a simple example consider something mechanical like a conveyer belt that counts widgets as they move along. Several tasks might be running - the motor controller, the counter, maybe some safety detectors - lots of possibilities. The 3 tasks: run the motor, count the widgets and check the safeties, all are running. Each task is scheduled when appropriate - the motor is stepped in intervals, the counter is sampled at some different interval and the safeties are sampled a lot. Each task is queued to run as needed (time to step the motor? queue it. Time to look for product- queue the looker task etc.) Obviously, some of these tasks are more time sensitive than others. Smooth running of the motor means that the steps should come at uniform intervals. That means that motor stepping is more important than detecting the product. But nothing is more important than safety.

The scheduler sorts all of this out. It's easy if only one task is ready at a time - just do that task. When multiple tasks are ready at the same time, that's when the scheduler takes priority into account. What's more time sensitive, counting or uniform stepping? Probably stepping so that task would have a higher priority i.e. if both are ready at the same time, step first then look for widgets to count. Where would you put looking at the safety switches? Safety is important so maybe it gets the highest priority.

There are a lot of variables here and all are dependent on the specific system. Safety systems are important but what are they? Limit switches are important and need high priority - over-temperature switches not so much. It is up to the engineer to know what is important and incorporate that into prioritizing the tasks. When 2 or more tasks are eligible to run at schedule-time, what's important gets run first. That's what prioritization means.

But to give those 1000+ pages their due, it's not as simple as all of that and entire semesters can be devoted to topics such as 'OK, the highest priority tasks execute first but what happens when one or more consume all of the allotted slots or some external event demands that they be serviced all of the time? How does the OS (task scheduler) handle that? Looks at his bookshelf. 1000 pages. Yeah.

Since you're into embedded systems, you'll be happy to hear that you can do a lot with a simple round-robin (look it up) scheduler with interrupts for handling and setting up processing for those faster events. The rest.. well, not so simple but man, that's why we get paid the big bucks.

Good luck!
 

Thread Starter

Parth786

Joined Jun 19, 2017
642
Well, a simpler answer than reading 1000+ pages is that task priority has to do with how tasks are scheduled. Specifically:

But to give those 1000+ pages their due, it's not as simple as all of that and entire semesters can be devoted to topics such as 'OK, the highest priority tasks execute first but what happens when one or more consume all of the allotted slots or some external event demands that they be serviced all of the time? How does the OS (task scheduler) handle that? Looks at his bookshelf. 1000 pages. Yeah.
Thanks @JohnInTX for looking at this thread. You already had been help me a lot in my previous threads.

The example of conveyor belt you took is very understandable. I read it about conveyor belt on internet. This is mechanical device.Therefore I couldn't understand very well. I am taking one example "water bottling plant" In the bottling plant, bottles are moving one by one on single conveyor belt. Suppose water bottling plant system has motor which run conveyor belt. System has sensor which count the bottles and sensor like temperature sensor. When the system gets hot, the system itself may be closed for a while.

Task 1 : run the motor
Task 2 : count the bottles
Task 3 : check the safety of system

First priority : check the safety of system
Second priority : run the motor
Third priority : count the bottles

I think We set a certain time to complete every task and scheduler decide which one and when task will perform. High priority task will run first
I think this will work like this flow chart

upload_2017-11-22_19-44-25.png
Does scheduler work in this way. conveyor belt is good example but I don't know what is connected with the micro control in conveyor belt system

What could be the best example for understanding scheduling in embedded system
 
Last edited:

JohnInTX

Joined Jun 26, 2012
4,787
You have the basic idea. On your diagram, Get Task is the scheduler but Do Task is only one of the 3. Flow charts are good for following a known sequence but the very idea of multi-tasking/task scheduling is not sequential so flow chart probably isn't the best diagram. But, sure. Each task is a box and the scheduler picks one to run. As we discussed above, which task is run is determined by the scheduler according to set of rules and events. Your example is a good one. Imagine that the scheduler is a person (you) and that you have 3 other guys that perform each of those 3 functions, check safety, operate the motor and count the bottles. To get things running you tell each one to get busy.

As things progress, the bottle counting guy comes to your desk and says '1'. He returns.
The motor guy is happy, his motor is running.
The safety guy comes up to you if there is a problem but so far so good.
You call the motor guy over and tell him to go faster.
The count guy runs up and says '2', he counted another one.
The safety guy comes up and warns of a problem, you dispatch a fix
And so on. So far, each task has been serviced as required.

Now suppose that the count guy shows up to shout '3' but the safety guy shows up at the same time. Both demand attention but you have decided that safety is more important. You handle that one first and tell the counting guy to wait. He does and when the safety issue is resolved you turn to the count guy and service him. That is what task prioritization does.

That's a coarse description of how it works but gives you the idea. You (the scheduler) are the boss (the executive) and the others perform tasks as you see fit. When there are timing conflicts, you resolve them by doing the most important things first, then the less important ones. Note that there are no delays i.e. if you want the counter guy to pause, you don't hold him at your desk and ignore the others. Instead you tell him to stop what he is doing and set a timer on your desk. Then while he is waiting around (his task is SUSPENDED) you resume handling the remaining two. When time is up, you yell at the count guy to resume coming to your desk and shouting out the next number (you changed his state from SUSPENDED to RUN). What this means is that the kind of delay loops you are used to don't appear in a real-time system. To make a task wait, the scheduler just sets a timer and doesn't schedule that task until the time runs out.

As I said earlier, there's lots more to it but put yourself at that desk and imagine the kinds of things you might have to deal with to keep things running. Sure safety gets priority but what if that guy is always at your desk? If you always drop everything and listen to him, you won't be paying attention to the others. That may be a problem that you want to think about. 'Hey safety, shut up for just a bit while I see what these other two want'. That sort of thing.

That's what goes on, the scheduler shuffles things around to keep things running. He is the executive and that's also why many multi-tasking systems call that function their 'real-time executive'.

Hope that moves you along.
 

Thread Starter

Parth786

Joined Jun 19, 2017
642
Now I understand what is multitasking / scheduling. Multitasking solves the problem by scheduling which task may be the one running at any given time, and when another waiting task gets a turn. Scheduling is process decide which task that should execute at this moment. It determines what process to run next based on a variety of factors, like priority, time etc.

Task has three states:
  • Ready : Task is ready to executed on processor but is not currently executing
  • Running : Task is executing on processor
  • Waiting : Task is currently waiting to execute
The example of conveyor belt was little bit difficult to understand because of mechanical interfacing. I am taking another example. Automated monitoring system for bus that will works in the following way

Task 1 : Design a system that can open or close the bus door

Task 2 : Design system that will show the next stop and can count how many peoples are in bus. if total number of peoples in a bus exceed the passenger carrying capacity, then an alarm is generated by the system.

Task 3 : Design system that will take care of safety issues like if fire start in bus, The system will automatically generate alarm.

Task priority for system
  • First priority : Task 3
  • Second priority : Task 2
  • Third priority : Task 1
Now I understand what is the priority and scheduling. I want to understand whole process with Pseudo-code because I do not think that I can write a program at this level. Pseudo-code can help me to understand original program process. I do not understand how to start with Pseudo-code.

Note : I do not know that the example taken by me is right or wrong. I thought this can be simple example of task scheduling but I am not that it is right example .
 
Last edited:

JohnInTX

Joined Jun 26, 2012
4,787
Yeah, that's going to be a pretty big topic to cover in a forum environment...

At your level using the 8051 you'll probably want to use a Finite State Machine for the underlying structure. You'll also need to understand timer interrupts, the C 'switch' statement and use state diagrams instead of flow charts to design your tasks.

Here are some good links to get you going:
@Robin Mitchell has a nice introduction to state machines here on AAC. He uses the 'switch' statement to create simple state machines. You should study it until you understand what is going on and can code one yourself.
https://www.allaboutcircuits.com/technical-articles/finite-state-machines-microcontrollers/

Here is a nice writeup describing a simple multi-tasking system in C.
https://www.ics.uci.edu/~givargis/pubs/C50.pdf
http://www.cs.ucr.edu/~vahid/rios/

If you google 'simple scheduler in C', you'll get lots of hits with coding examples.

Sorry there is not a simpler answer but once you get past simple, single task, step-by-step processing a lot more things come into use. You'll have to get proficient with all of those things before you can move on to designing such a system. None of it is particularly difficult, there is just a lot of it. The GOOD news is that once you get the basics done, you'll find that its much easier to create bigger systems that have many things going on at once.

Enjoy the journey!
 

Thread Starter

Parth786

Joined Jun 19, 2017
642
Yeah, that's going to be a pretty big topic to cover in a forum environment...

At your level using the 8051 you'll probably want to use a Finite State Machine for the underlying structure. You'll also need to understand timer interrupts, the C 'switch' statement and use state diagrams instead of flow charts to design your tasks.
I understand interrupt programming. I have been written c program for interrupt. I also understand switch case statement in c programming. I will read about state machine and state diagram

I am also reading some links on task scheduling. while reading I am confused. Is there any difference between Priority-based scheduling and Time -based scheduling ? I understand, In Priority-based scheduling , task execute on the basis of priority but what about Time -based scheduling ? I have read it task execute on the basis of time. But I don't understand exact meaning? Is round robin scheduling different from these?
 
Top