4. Consider the following program: I int a-2 2 void foo (int b) ( 258 Lee &Seshia, Introduction to Embedded Systems 9. MEMORY ARCHITECTURES printf ("%d", b) ; main (void) foo (a)i a-1; s int main (void) Is it true or false that the value of a passed to foo will always be 2? Explain Assume that this is the entire program, that this program is stored in persistent memory, and that the program is executed on a bare-iron microcontroller each time a reset button is pushed. enable models of concurent compositions of sach sequential peograms We then progress to discuss threads, processes, and message passing, which are three styles of composition 11.1 Imperative Programs called an impertive language. C is an imperative langae Eample I1.1: In this chapler, we illustale several kcy pis ng the example C program shown in Figure 11.2 This program implements a commonly wsed design pattern called the ebserver pattern (Gamma ctall. 1994) In this paten, an update procedure changes the value of a variuble x Observers (which are other programs or other parts of the program) will be mo ified whenever x is changed by calling a callback peocedure. For example, the value of x might be displayed by an observer on a screem. Whenever the value changes the observer needs to be notified so that it can update the display on the screen. The following main procedure uses the procedures defined in Figure 11.2 saddtistener (print pdate ( 294 Lee & Seshia action to Embedded Syste . MULTITASKING This test program regisiers the print procedure as a calhack twice, then per- fonms an update (settingx1 then regislers the print procedure again, and finally performs another update (setting x2 The print procedure simply prints the current value, so the output when exec- lol program is 1 1 2 2 2. ACprogram specifies a sequence of steps where each step changes the state of the memory in the machine. In C, the state of the memory in the machine is represented by the values of variables Eample I1.2: In the peogram Figure 11.2 the saie of the memoey of the machine includes the value of variable x (which is a plobal variable) and a list of elements pointed to by the variable head (amother global variablc) The list iself is represented as a linked list, where each element in the list coetains a function pointer referring to a procedure to be called when x chanpes During execution of the C program, the state of the momory of the machine will need to inclade also the state of the stack, which inclales any local variables Using estended state machines, we can model the esecution of certain simple C programs assuming the proprams have a fixed and bounded number of varables The variables of the C will represent positions in the program, and the transitions will npcst eecution of the proeram program will be the variables of the state machine. The states of the state machine Example 113 Figure 11.3 shows a model of the update procedure in Figure 11.2. The machine tramsitions from the initial Idie state when the update proce- dure is called. The call is signaled by the input arg being peosent its valar will be the int argument to the update procedure. Whem this trasition is taken, neex (on the stack) will be assigned the value of the angument In addision, x (a global ariable) will be updated 11.1 Imperative Program:s A programming language that expresses a computation as a sequence of operations is called an imperative language. C is an imperative language Example 11.1: In this chapter, we illustrate several key points using the example C program shown in Figure 11.2. This program implements a commonly used design pattern called the observer pattern (Gamma et al., 1994). In this pattern, an update procedure changes the value of a variable x. Observers (which are other programs or other parts of the program) will be notified whenever x is changed by calling a callback procedure. For example, the value of x might be displayed by an observer on a screen. Whenever the value changes, the observer needs to be notified so that it can update the display on the screen. The following main procedure uses the procedures defined in Figure 11.2: I int main (void) addListener (&print); addListener (&print) update (1); addListener (&print); update (2); return 0