e-PG Pathshala

Subject: Science

Paper: Embedded System Module: RTOS: Multiple tasks and Processes Module No: CS/ES/23

Quadrant 1 – e-text

In this module introduction to RTOS will be given. An RTOS becomes essential when there is processing and servicing of multiple tasks and multiple devices with real time constraints.Hence in this module, the ideas of Processes and Tasks will be discussed. The multiple tasks, processes and control block will also be discussed.

1.OS and RTOS

Real-Time (RTOS) is the operating system that provides facilities for satisfying the real-time requirements. It also allocates resources by considering real time constraints and with the help of real time algorithms it allocates resources but in General operating system (OS) allocates resources based on the criteria like fairness. It also allocates the process without regards to time which can easily cause processes to miss their deadlines.

1.1 Task and process

A process is a single execution of a program. If the same process runs at different times, it is nothing but two different processes are created. Every process has its own state and each state include registers and memory. In many OSs each process is kept in a separate address space by the memory management unit. Threads are processes, which will share same address space, while a task is composed of several processes or threads that have their own address space.

1.2 Release time and deadline.

Depending on the application, processes will have timing requirements. A policy which defines how processes are selected for promotion from the ready state to the running state. It must define the timing requirements that it uses whether the schedule is valid or not. The timing requirements are mainly divided into release time and deadline.

Aperiodic and periodic :

The period of a process is the time between successive executions. Periodic processes have regular arrival times and hard deadlines. Aperiodic process have irregular arrival times and either soft or hard deadlines. An aperiodic process is initiated by an event such as external data arriving or data computed by another process.

Process Rate : It is the inverse of its period.

1.2.1 Release time - It is the time at which the process becomes ready to execute. For aperiodic,the process is initiated by an event. The release time for aperiodic is measured from that event onwards.

In periodic process the release time will be determined in two ways. For simpler system, the process will be ready at the start of the period. In sophisticated systems , the release time will be set at the arrival of data.

1.2.2 Deadline- It is the time at which the process must finish. For aperiodic process the deadline is measured from the release time. In case of periodic process it may occur at some time other than the end of the period.

Figure 1 shows the example for release time and deadline.

Figure 1. Release times and deadline

Dead line frequency will occur in two ways i.e. periodic and aperiodic.

There are three types of deadlines as follows:

● Hard: failure to meet deadline causes system failure. ● Soft: failure to meet deadline causes degraded response. ● Firm: late response is useless but some late responses can be tolerated. 2. Timing requirements on processes:

In a multirate system each process executes at its own distinct rate. The most common case for periodic processes is for the initiation interval to be equal to the period. However, the overlapped execution of different instances of processes allows the initiation interval to be less than the period. A sequence of processes with high initiation rate is shown in figure 2. The various execution instances of program P1 have been subscripted to distinguish their initiation time. In this example, there are four instances of the processes given to four different processors with initiation interval is equal to one fourth of the period. The fifth instance of the process is assigned back to CPU 1.

There will be a possibility of process initiation rate less than the period even in case of single-CPU systems. If the process execution time is significantly less than the period, the multiple copies of a program can be initiated in slightly offset times.

Figure 2. Sequence of process with high initiation rate

2.1 Process State and Scheduling

The process will be in any one state. The states are ready, executing and waiting states.

• Ready- A process goes into the ready state when it receives its required data and when it enters a new period.

• Waiting-A process goes into the waiting state when it needs data that it has not yet received or when it has finished all its work for the current period.

• Executing-A process can go into the executing state only when it has all its data, that is ready to run.

The process state is shown in figure 3.

Figure 3. Process state diagram

Each process will have its own process control block (PCB). The PCB consists of a data structure having the information using which the OS controls the process state. The PCB is stored in the memory area of the kernel. It consists of the information about the process state. The main components of PCB are

• Process ID

• Process priority

• Parent process

• Child process and address to the PCB of the next process which will run.

3. Task

A task is like a process or thread in an OS. In RTOS, task term is used for the process. It consists of executable program (codes), state of which is controlled by OS. Each task will have the task control block (TCB). The TCB will have data structure information using which the OS controls the task state. The task will be any one of these states - idle, ready, running, and blocked or finished.

The states are as follows:

•Idle state

• Ready State

•Running state

•Blocked (waiting) state

•Delayed for a preset period

•Idle state- task has been created and memory is allotted to its structure,if it is not ready, then task is not scheduled by kernel.

Figure 4 : Task State

Ready State - The created task is ready and is schedulable by the kernel but not running at present as another higher priority task is scheduled to run and gets the system resources at this instance.

Run state-Executing the codes and getting the system resources at this instance. It will run till it needs some IPC (input) or for an event or till it gets preempted by another higher priority task than this one.

Blocked state- Execution of task codes suspends after saving the needed parameters into its context. It needs some IPC (input) or it needs to wait for an event or wait for higher priority

task to block to enable running after blocking.

Deleted Task─ The created task has memory de-allotted to its structure. It frees the memory. Task has to be re-created.

The contents of a task control block are shown in figure 5.

Figure 5: Task control block

The TCB consists of a Task ID which is a number between 0 and 255. In Tasks Context, PC is the Program Counter which maintains the address of the next instruction to be executed. Stack pointer, CPU and the Processor status register are also available in the tasks context. The priority for a task is given using the Task priority in TCB which is between 0 and 255 and is represented by bytes. Parent Task and child task are also maintained using Task priority.

The Allocated program memory addresses the blocks in physical memory and in secondary (virtual) memory for the tasks-codes,

• Allocated task-specific data address blocks.

• Allocated task-heap (data generated during the program run) addresses

• Allocated addresses of CPU register-save area as a task context represents by CPU registers, which include the program counter and stack pointer. Allocated task-stack addresses for the functions called during running of the process.

• Allocated addresses of CPU register-save area as a task context represents by CPU registers, which include the program counter and stack pointer.

•Allocated addresses of CPU register save area as a task context. Task is independent, it takes control of the CPU when scheduled by a scheduler at an OS. The scheduler controls

and runs the tasks.

Task Characteristics for a single CPU in a system:

Task is an independent one and it cannot call another task. Each task is identified by a TCB. Each task has an ID just as each function has a name. The ID, task ID is a byte if it is between 0 and 255. ID is also an index of the task. Each task may have a priority parameter. If the priority is between 0 and 255, it is represented by a byte. Multitasking operations are done by context switching between the various tasks and it is performed by OS Scheduler. Each task must be either a reentrant routine or it must have a way to solve the shared data problem. A function is reentrant if it can be invoked while the process is already executing.

For a function to be rentrant the following should hold:

● All the arguments pass the values and none of the arguments is a pointer. ● Whenever a function call is initiated and when an operation is not atomic that function should not operate on any variable which is declared outside the function or which an interrupt service routine uses or which is a global variable. ● A variable should be passed by reference as an argument into the function and that function should not call any other function which is not itself reentrant.

4. Summary

In this module we discussed about the Task and Process. The Multi task and processes are explained in detail. Both the Process control block and Task control block are also explained.

5. References

1. Wayne Wolf, “ as components: Principles of embedded computing systems design”; Second edition, published by Morgan Kaufmann series(2008).

2. Embedded Systems Architecture, Programming and Design - Raj Kamal, second edition, Tata McGraw-Hill Education 2008.