You are on page 1of 40
Real-Time Operati Sytem (VOR) based mbedded System Design jerstand the basics of an operatin : ig system, Be fetesctencleveesotanoperaingsnun aan | ify the types of operating systems | ‘earn the internals of Real-Time Operating System and the fundamentals of RTOS: pased embedded firmware design aset shout the different realtime kernels and the features that make a kesmel Real-Time ‘Discuss tasks, processes and threads in the {earn about the structure of a process, life cycle and process management Learn the concept of multithreading, thread standards and thread scheduling Understand the difference between multiprocessing and multitasking Learn about the different types of multitasking (Co-operative, Non-preemptive ribe the FCFS/FIFO, ‘Learn about the Shortest operating system context the different states of a process, Prt ocess Preemptive and riority based task/process scheduling (SRT), Round Robin and priority based | ion (IPC) mechanisms used by tasks/ perate each other ina multitasking, environment — ‘ess Communication f shared memory techniq| iques (Messat LCFS/LIFO, SJF and pI Remaining Time f message passing techni nisation in a multitasking environ different issues related t0 the accessing of@ In the previous chapter, we discussed about the Super loop based ta i ‘model for fim execution, The super loop executes the tasks sequentially in the order in which the tasks are listed within Toop. Here every task is repeated at regular intervals and the task execution is non-real time, AS the nunby of task increases, the time intervals at which a task gets serviced also increases. If some of the tasks inyoly waiting for external events or /O device usage, the task execution time also gets pushed off in accortane with the ‘wait’ time consumed by the task. The priority in which a task is to be executed is fixed andis determined by the task placement within the loop, in a super loop based execution. This type of fimise execution is suited for embedded devices where response time for a task is not time critical, Typical exams are electronic toys and video gaming devices. Here any response delay is acceptable and it will not ereateamy operational issues or potential hazards. Whereas certain applications demand time critical response total events and any delay in the response may become catastrophic. Flight Control systems, Air bag controlatd Anti-lock Brake System (ABS) systems for vehicles, Nuclear monitoring devices, etc. are typical example of applications/devices demanding time critical task response. How the increasing need for time critical response for tasks/evente ; asec ical oS onse for tasks/events is addressed in embedded applica! ; : ihe on anced basis. A normal compen Aailabe wo se : , working, Ing. § to the ng. and storage memory. Then collection of imary functions g ([Eecesmanaaeren] interface (API) Kernel Services 1/0 system management Device driver interface Fig. 10.1 The Operating System Architecture 1.1 The Kernel is the core of the operating system and is responsible for managing the system resources and the nication among the hardware and other system services, Kernel acts as the abstraction layer between urces and user applications. Kernel contains a set of system libraries and services. For a general the kernel contains different services for handling the following ent Process management deals with managing the processes/tasks. Process management + the process, loading the process's code into the memory space, setting up and managing ing up the memory space fo he roca ystem resources, schedulin; ‘and managing the execution 0! We Process, a : SS Communication and synchronisation, process termination! Control Block (PCB), Inter Proces Do a v &, We will look into the description of process ‘and process management in a later section of this snemory, refers to the VolaUIe TSE R ry Management The term primary neated wit, cach PIOEESS are st e loaded and variables and shared data el it (MMU) of the kernel is responsible for ‘of which part of the memory area is ‘De-allocating memory space 0” anced P , oe ident. For example, the kerne}, 8 Operon and they are not the sq ‘or routing the /O requests coming rent Kernel De ee Ina well-structured OS, the d 10 the appropriate VO device are provided through a set of Application p not allowed and the aceess 1 them ATT sof all the UO devices of the nel. The kern fing the kernel. Some kernels, dynamically time of buil ‘ : . 1d when, ag device is installed (c.g. Windows NT kernel keeps they plug “n" ley USB device i tached to the system). The service “Device Manager’ (Ng mew ‘n ible for handling all 1/O device related o In the operating system context mulaprocesis Sa ity to multiple processes simultaneously. Systems which are ag le Of perfor | multiproce as multiprocessor systems. Multiprocessor yes | possess multiple CPUs and can execute multiple processes simultaneous ‘The ability of the operating system to have multiple programs in meme Which are ready for execution, is referred as multiprogramming. In a uniprocessor system, iti not posi fo execute multiple processes simultaneously. However, it is possible for a uniprocessor system to ati some degree of pseudo parallelism in the execution of multiple processes by switching the execution aa different processes. The ability of an operating system to hold multiple processes in memory and sis the processor (CPU) from executing one process to another process is known as multitasking. Multis creates the illusion of multiple tasks executing in parallel. Multitasking involves the switching of CPU ie executing one task to another. In an earlier section “The Structure of a Process’ of this chapter, We ka that a Process is identical to the physical processor in the sense it has own register set which mim CPU registers, stack and Program Counter (PC). Hence, a ‘process’ is considered as a ‘Virtual prove awaiting its turn to have its properties switched into the physical processor. In a multitasking enviroa®® when task/process switching happens, the virtual processor (task/process) gets its properties converted that of the physical processor. The switching of the virtual processor to physical processor is controlled Scheduler ofthe OS kemel. Whenever a CPU switching happens, the current cc atest of exec 'B IS, current context of execution 3g, are known The process of retrievin, itchi 2 ig the saved context detai ig Is switching, is known as ‘Context retr letails for a process, which is coi ean, | etrieval. Manus 1! # Process, which is going to be executed saving’ and “Context retrieval, Multitasking involves “Context itching (Fig. 10.10 Juggling The skilful object mani i manipulation game i . Juggler uses a number of objects (balls, as 3 ee eae nes ‘s them up i based Embedded. s only one ball » throwing and catching cea oe i 1B Creates Per hand, wo hands © simultaneously. ne ee eis Wor. Execution switches o Process I ‘ater or System Call) “Context Switching” 3, Reload Context for Process 2 ffom PCB 2. Perform other OS operation related to 1, Save Curent contest into PCBE 3 pater other OS operations relate © ' ' ' \ i 1 i 1 ' i 1 I 1 1 i 1 2 \ = 1 1 {Delay in execution of | Proce happens | Gusto Content Switching’ Delay in execution of Process t > Fig. (M1 Context switching Types of Multitasking earlier, multitasking involves the switching of execution among multiple tasks, Depending he switching act is implemented, multitasking can be classified into different PES ‘The following bes the various types of multitasking existing in the Operating System's comiess 1 Co-operative Multitasking multitasking is the most primiti ‘when the currently executing tasl can hold the CPU as much time as! feach other for getting the CPU time for exer uting task is non-cooperative, the other tasks may hi ve form of multitasking in which a taskfprocess gets a chance 0 I/process voluntarily relinguishes the CPU. Inthis ‘method, any evants, Since this type of implementation involves the merey vtion, itis known as co-operative multitasking. Ifthe ave to wait for a long time to get the CPU. ptive Multitaskini : titaskii ee nacre task/process gets a chance 10 execute, When and how acne ets is dependent on the implementation of the preemptive sca then same indice nultitasking, the currently running task/process is eae hess ‘The preemption of task may be based on time slots oF ask/process Pri ive Multitasking a i the proce s/task, which is © tein mbes ee reemptive mulcitasine feceny ree and non-P! , the currently © TOCESS/tagy. they ‘The co-operative erative batt wating foran W/O, ofa shared resouyeg Seay ae ‘Blocked i ching the curently executing ask elinguigys Feling a efor whereas in nonepreem™ptve MEY A 16 occur. &% ‘TASK SCHEDULING - ititasking involves the execution switehin 10.5 [As we already discussed 1 ome mechanism in place to sha tet ere 9 s + the different tasks. Tne id to decide which process/task is to be eyacn x ‘1 among the differe! Lae a ge which task/prOCES sto be exec tt given point of ne a as task/process scheduling. Task scheduling fgae"™ int of time is kn fas = Baers ing policies forms the guidelines for dees! : itasking. Scheduling p , in Pn ree wed when, The scheduling policies are implementeting whi s a service. The kernel service/app) i itis run by the Kernel as a service. iceland Hea hm. is known as Scheduler’. The process scheduling decisongg ‘which implements the scheduling algorithm, take place when a process switches its state to 1. “Ready” state from ‘Running’ state 2 Blocked/Wait’ state from ‘Running’ state 3. “Ready” state from ‘Blocked/Wait’ state 4. ‘Completed’ state ‘ ; A process switches to ‘Ready” state from the ‘Running’ state when it is preempted. Hence, the type Scheduling in scenario 1 is pre-emptive. When a high priority process in the “Blocked/Wait’ state complies 4%: UO and switches to the “Ready” state, the scheduler picks it for execution if the scheduling policy wed Prony based preemptive, This is indicated by scenario 3. In preemptive/non-preemptive multitasking te Process relinquishes the CPU when it enters the “Blocked/Wait’ state or the ‘Completed’ state and switching GB: CPU happens at this stage. Scheduling under scenario 2 can be either preemptive or non-preempive, Scheduling under scenario 4 can be preemptive, non-preemptive or co-operative ‘The selection of a scheduling criterion/algorithm should consider the following factors: aaa fe re algorithm should always make the CPU utilisation high. CPU utilisation’s Percentage of the CPU is being utilised, Throughput: This gives an indication of the number of ae fora good scheduler should always be Hees Processes executed per unit of time. The throught “Ready” queue waiting to get the CPU! heduling algorithm, contains 10 eee Ses in the system contains rOCesses, Which "The Ready queue is em are ready fc £ a. PLY When there i, gon oXecution and wait 2 kee »: Contains the Ee of processes, which A 4s no process ready eye for CPU to get their es through all these queues during its ann’ {Of an HO device. ae matic representation (Fi, hg its journe eae (Fig. 10.12) illustrates ie eaten to ‘Completed’ stage. The ition of a process through the vari ob Queue cc Process? Process Ready Queve Jeg Nove prsmpted aces Move VO Compl recs Ready eve ea oP te a Toes? | Process? Process Device Quewe Fig. 10.12 Illustration of process transition through various queues scheduling algorithm used, the scheduling can be classified into the following categories. lon-preemptive Scheduling j scheduling is employed in systems, which implemen non-preemptive multitasking model. — type, the currently executing task/process jg allowed to run until it terminates or en ng for an I/O or system resource. The various types of non-preemptive scheduling ad w completion time 10, 5,7 millise spt and Fete sling Ene and Turn Around Time (Assan CPU is represented as <«— 10 the CPU is readily available at the time of arrival of P1, P1 starts executing without be “Read” queue. Hence the waiting time for P1 is zero. The waiting time for all processes are; ‘ms (PI starts executing first) ‘ms (P3 starts executing after completing P1 and P2) (Waiting time for all processes) / No, of Processes (Waiting time for (P1+P2+P3)) / 3 (0+10+15)/3 = 25/3 = 8.33 milliseconds 10ms (Time spent in Ready Queue + Execution Time) ‘urn Around Time for all processes) / No. of Processes (Turn Around Time for (P’ 1+P2+P3)) / 3 = (10+15+22)/3 = 47/3 = 15.66 milliseconds P) is ime and Turn Around Ti, B ‘csuming there is no VO wo; (TAT) for 4 irene together in the order Pa ee Ot ach process £P2, PE, py ne Process and the Average w: Hofrexecution of the processes by the CPU i ) for the above ‘presented 2 is readily available at the ti : Dacca the waiting time for Po ger atot 2, P2 stan executing without any waiting in ms re Stas creer Ze. The waiting ime forall processes is given a5 ms (P1 starts executi B65 stars execu after completing P2) ast ing after completing P2 (Waiting time for all processes) / No. of P ee (Waiting time for PHP14P3)/30 (0+5+15)/3 = 20/3 6.66 milliseconds nd 1) f i ; ea eye ims (Tine sentin Realy Gaon ee wad Time (TAT) for P3 =22 ms (-Do-) fom Around Time = (Turn Around Time for all processes) / No. of Processes = (Turn Around Time for (P2+P1+P3)) /3 (5+154+22)/3 = 42/3 14 milliseconds “Average waiting time and Turn Around Time (TAT) depends on the order in which the processes "Ready’ queue, regardless there estimated completion time. fie above two examples itis clear thatthe Average waitin time and Turn Around Time improve if P ‘with shortest execution completion time is scheduled fist. x drawback of FCFS algorithm is that it favours monopoly of process. & Process which does in any VO operation, continues its execution until it finishes is task. If the process contains any sation, the CPU is relinquished by the process. In general, FCFS favours CPU bound processes and processes may have to wait until the completion of CP ‘pound process, if the currently executing 4 CPU bound process. This leads to poor device utilisation. The average waiting time is not lor FCFS scheduling algorithm. st-Come-First Served (LCFS)/L CPU time to the processes bas First Served (LCFS) schedulin; js serviced first. LCI Which they are entered in the ‘Ready’ quel I corn First Out (LIFO) Where eB 2) ee aL Caleuine at : centered “Re jeduling PI. ithe upand 2. PS ove ater § ee time and Tum Around pets 1 el + n re the an a the processes contain only CPU opera a i mo 0 wating te ee and the scheduling sequence wit p ote sare i Ready ave rocess entered the “Ready” the ti becomes the Fast Pi 1 ee as given below. May me PA ee i 16 23 28 10 pees 6—+— —> pes iting ti the processes is given as ws “om sr Se completing. But P4 arrived ater saa PE eo et te waiting ime = Execution start time ~ Arrival Time 16 ms (P3 starts executing after completing Pl and Ph 2 3 ms (P2 starts executing after completing P1, 3 1g time forall processes) / No. of Processes = (Waiting time for (P1+P4+P3+P2)) / 4 (0+5+ 16+ 23)/4 = 44/4 = 11 milliseconds Tum Around Time (TAT) for P! Tum Around Time (TAT) for P4 Waiting Time for P: (Time spent in Ready Queue + Execution Time) (Time spent in Ready Queue + Execution Time = (Exeale Start Time — Arrival Time) + Estimated Execution Tine=)) -5)+6=5+6) Fam Around Time (TAT) for P3 = 23 ms_ (Time spent in Ready Queue + Execution Time) Zum Around Time (TAT) for P2=28 ms (Time spent in Ready Queue + Execution Time) Average Turn Around Time = (Tum Around Time for all Processes) / No. of Processes (Tum Around Time for (P1-+P4+P34P2)) / 4 (10+11+23428)/4 = 72/4 18 milliseconds Possesses the same drawback as that of FCFS algorii rst (SIF) Scheduling 10.5.1.3 Shortest Job Fi re iid oe aa ate peu “Sorts the ‘Ready’ queue’ each time a process er the proces ‘ cae a Ss wt shores! (cas estimated conc, Me" le Waiting for UO or syatem sccoue) MB Tun time is scheduled fj HS followed by tne es time, In SIF, the process with the shortest ‘ess, and so on, ext shortest Proc ‘and Tum Around Time (A tiace ‘Ready’ queue based on the ‘estimated competion time Ue SHOES estimated are scheduled for execution tt the next least one ge presented a second, fz SehE | p2 yo ae : : ara n 1 ee > ; sed execution time of P2 is the least (5 ms) fot ccc ) followed by P3.(7 ms) and PI (10 ms). Oms (P2 starts executing first) ms (P3 starts executing after completing P2) 2 ms (P1 starts executin, estes eas se npn Pant =S) 2 = (Waiting time for (P2+P3+P1)) /3 — (045412)/3 = 1773 5.66 milliseconds Time (TAT) for P2=5ms (Time spent i . ee Dorr ws ms ta pent in Ready Queue + Execution Time) Time (TAT) for Pl =22ims _—(-Do-) Jum Around Time = (Turn Around Time for all processes) / No. of Processes ; = (Turn Around Time for (P2+P3+P1)) /3 on 10 = (5+12422)23 = 39/3 = 13 milliseconds Around Time (TAT) is the sum of average waiting time and average execution time. age Execution time = (Execution time for all processes)/No, of processes (Execution time for (P1+P2+P3))/3 (10+54+7)/3 = 22/3 = 7.33 Around Time = Average Waiting time + Average Execution time 5.66 + 7.33 = 13 milliseconds a : 4 : ample, it is clear that the average waiting time and turn around time is much improved with the : : for the same processes when compared to the FCFS algorithm. ed 2 iting ime and Turn Around Time (TAT) for cach and the Average ‘ for the above example if a new process Pé wit sses contain er 2 ms of execution of P2. Assume all he ProcesS®S © waiting me forall the processes are given as ting tir =0 ms (P2 starts executing first) : ae a a res (P4 starts executing after completing P2. But P4 arrived after 2.ms of of P2, Hence its waiting time = Exec' 7 ms (P3 starts executing after completing P2 and P4) 14 ms (PI starts executing after completing P2, P4 and P3) ion start time ~ Arrival Time =5~ (Waiting time for all processes) / No. of Processes (Waiting time for (P2+P4+P3+P1)) / 4 =(04+3+7+ 14/4 = 24/4 milliseconds Sms (Time spent in Ready Queue + Execution Time) Time ~ Arrival Time) + Estimated Execution Time +2) Around Time (TAT) for P3=14ms _ (Time spent in Ready Queue + Execution Time) ound Time (TAT) for Pl =24 ms (Time spent in Ready Queue + Execution Time) Turn Around Time = (Turn Around Time for all Processes) / No. of Processes (Turn Around Time for (P2+P4+P3+P1)) / 4 = (545+14424)/4 = 48/4 = 12 milliseconds time ie a Bs bis of feed is minimal in SJF scheduling and s 'g like FCFS. The major drawback of SIF ecution Senin time is high may not get a chance to ¢ ss can be indicated threw, d as a priority based scheauit tious ing Where with process IDs P|, P2, P3 with estimated complet | highest priority, 3—lowest priority) respectively enters th fand Turn Around Time (TAT) for each process and the Average waiting time and Tum Around there is no 0 waiting for the processes) in priority based scheduling algorithm, mauler sorts the ‘Ready’ queue based on the priority and schedules the process with the highest pl with priority number 0) first and the next high priority process (P3 with priority number 2) as adso on, The order in which the processes are scheduled for execution is represented as PL P3 P2 0 10 7 2 +——_ 10 ——»«—_7—__ >< 55> ne for all the processes are given as P1=0 ms (PI starts executing first) P3 = 10 ms (P3 starts executing after completing P1) or P2 = 17 ms (P2 starts executing after completing P1 and P3) time = (Waiting time for all processes) / No. of Processes (Waiting time for (P1+P3+P2))/ 3 = (0410417)/3 = 273 milliseconds Queue + Execution (Time spent in Ready (-Do-) Pee piers nt Zier = 8° 2:16.33 millis 8s and the Aye ‘xT) for each process ani ee Calculate the waiting time and ‘Turn Around ns ry pisces P4 with estimated complain tig the Leeat fabove example if sfexecution of PI. Assume all the processes gyi % Spe ‘5 mso ee Tum Around Time f . after . : iy i ee sa involved ses (Pl, P2 and P3) available in the ‘Ready @ operation and n0- three proce: yn this example Pl with priory eg } of Example 1. Now process py isis same as that of Ex ) d Rei renters the ‘Ready" queue rene me of execu ops ‘execution completion time 6 ms and pt tive and process P1 does not contain any VO operations py Sy Se cieduting, Pl trinates and nov the scheduler again sori fis execution, After 10 ms of scheduling, te jority 1) is higher than that of py ah jority for P4 (priority u : for process with Be roca ion of) as per the “Ready” queue available atine 2 ‘which was supposed to be run arrival of the process P4 wigh ing, P4 is pi -xecuting. Due to the arrival 0 a ‘of execution scheduling, P4 is picked up ee Fee eae “ia the “Ready” queue is resorted in the order P1, P4, Po. The sequence now changes as per the following diagram diagram for th Pl Ps P3 P2 0 10 16 23 28 +10 >< 6 « _1_ 545 -> ‘The waiting time for all the processes are given as Waiting time for P1 = 0 ms (P1 starts executing first) Waiting time for P4 = 5 ms (P4 starts executing after completing P1, But P4 arrived after 5 ms of executions PL. Hence its waiting time = Execution start time — Arrival Time = 10-5=5) Waiting time for P3 = 16 ms (P3 starts executing after completing P| and P4) Waiting time for P2 = 23 ms (P2 starts executing after completing P1, P4 and P3) Average waiting time Waiting time for all Processes) / No. of Processes = (Waiting time for (P1+P4+P3+P2)) / 4 (045+ 164234 = 44/4 I milliseconds Tum Around Time (TAT) for P1 = 10 ms Tum Around Time (TAT) for P4 = 11 ms ae Spent in Ready Queue + Execution Time) Ime s] i i pes a a ag oa Tim nearness cen i Average Turn Around Time = (Tur ae ns ni cae en i eee om P4+P3+PI))/4 IS Priority started its iling by dynamically Priory ate acy aveve fora rity of processes which are i known as ‘Aging’ ‘Preemptive sched ul ation (waiting in preemptive Scheduling juling is employed in systems, wi seis purely dependent on the scheduling al . gorithm, “Ready’ TI : COO nenn eee cee mine & Running” process into the Ready pe processes requesting for it is known as ‘Preemption’ na os “Ready” queue by the in different peponctics ‘The two important approaches adopted in aa scheduling can be tion and priority-based preemption. The various types of preeaiveechedling aepeed Sevres scheduling are explained below. 'ypes of preemptive scheduling adopted 52d ee SIF pciedullne Shenae, Remaining Time (SRT) Ipeerpeenotive SIF scheduling algorithm sors the ‘Ready’ queue only after completing he execution of feemat processor when the process enters “Wait state, whereas the preemptive SIF scheduling algorithm file Ready’ queue when a new process enters the “Ready” queue and checks whether the exeution Golitenew process is shorter than the remaining of the total estimated time for the currently executing fae Iithe execution time of the new process is less, the curently executing provess is preempted and my process is scheduled for execution, Thus preemptive SIF scheduling always compares the execution dleion ime (It is same as the remaining time for the new process) of anew process entered the "Ready" wih the remaining time for completion of the currently executing process and schedules the process remaining time for execution, Preemptive SIF scheduling is also known as Shortest Remaining scheduling. : : : Tet us solve Example 2 given under the Non-preemptive SJF scheduling for preempue SIF . The problem statement and solution is explained in the following example. a Eee a a aoe 3 with estimated compl : th estimated complet an swith process IDs P1, P2, steady queue together. A new process P4 WHT ry ms Assume all the processes contain OMY cel there are only ehree processes (P ler picks up the process with the shore” vith remaining time 5 ms) for s . under Re acenite SIF scheduling. vith cotmated execution COMPICNCT ms trouction 12 embedded. ptives algorithi i Pr nares of srt etion an for completion of the newly cme ee rection P4 continues its execution 2 fi coution. After 2 ms of sy ih hed % ¢ P4, The remaining time for s sched ot criring its eX! 2 Ready’ ae gue based om the remaining ime fore ining time for P2 (3.8), which Hay nthe “Ready” que, P2s ny es 1 process y" queue other Proven time 2 ms, the ‘Ready’ queue ig time fo ¢ " xecution 4 with execution ul ‘ gisates Die At ee eaginal git was P2, P3. PI “The execution sequence now ¢ a the order P2, PA, P2, P3, Pl. At the , the following diagram pore] Pe P3 PI 24 a4 7 14 7—_»><+——__10 > <2p the beginning it was PI, P3, P2. ‘The execution sequence now changes &s Per the foll rime (TAT) for P4 = 6 ms © ¢Time spent in Ready Queue + stimated tion Time = (5 - 5) + 6= : a an for Soar sims (Time spent in Ready Queue + Execution Time) = ime spent in Ready Queue + Execution Time) Tie (TAT) for Pas 28 Tine forall the processes) / No. of Processes = (Turn Around Time for (P2 + P4 + P3 + P1))/4 (16 +6 + 23 + 28)/4 = 73/4 = 18.25 milliseconds "Priority based preemptive scheduling gives Real-Time attention to high priority tasks, ‘based preemptive scheduling is adopted in systems which demands ‘Real-Time’ behaviour. RTOSs make use of the preemptive priority based scheduling algorithm for process scheduli priority based scheduling also possesses the same drawback of non-preemptive priority . This can be eliminated by the ‘Aging’ technique. Refer the section Non-preemp ‘scheduling for more details on ‘Starvation’ and ‘Aging 3 THREADS, PROCESSES AND SCHEDULING: __ PUTTING THEM ALTOGETHER r we discussed about threads, processes and process/thread ‘Now Tetus have alook athow these entities are addressed d implementation. Let’s examine the following pieces

You might also like