Real Time Operating Systems
Dr. Anupriya Koneru,
Associate professor,
Department of IT
LBRCE
Reference : Douglas Wilhelm Harder, Jeff Zarnett, Vajih Montaghami and Allyson Giannikouris – A Practical Introduction to real time systems for undergraduate engineering
UNIT-II
Static Memory Allocation
1. temporary data is that which must be reacted to, but once the action is performed, the data is no longer required,and
2. persistent data is that information that is being collected by the system for long-acquisition or subsequent data transfer to another system.
Static Memory Allocation
Static Memory Allocation
1. In some cases, the compiler can make decisions about where to allocate memory. It may be either at an absolute address or at a relative address, but the need for such memory must be discernable from the code at compile time, and this is termed static memory allocation. The absolute addressing includes global and static local variables, while relative addressing is used for the local variables of functions.
2. In others, the requirement for memory cannot be determined at compile time. For example, when you open a new document in a word processor, this requires memory; however, the compiler cannot be aware of that. Consequently, this requires memory allocation at run time, or dynamic memory allocation.
The requirements of a function
The operation of a function requires, at a minimum: locations to store:
1. arguments,
2. local variables, and
3. a return value.
These must be passed to the function, and as a function may be called recursively, each function call requires a different location in memory.
In addition, as a function may be called from multiple locations, the processor must know where to return to when the function call returns; that is, you must store
4. the program counter as it was immediately prior to the function call
The requirements of a function
The requirements of a function
The requirements of a function
5. a stack pointer to the current top of the stack.
6. a frame pointer that separates arguments from local variables.
The requirements of a function
7. the old stack pointer, and
8. the old frame pointer.
9. the previous values of any registers used.
The requirements of a function
The requirements of a function
1. be assigned to a variable and copied to that location,
2. become the argument of another function call or operation, or
3. be ignored.
The requirements of a function
The requirements of a function
The Cortex-M3 design
The Cortex-M3 design
The Cortex-M3 design
The Cortex-M3 design
The Cortex-M3 design
The Cortex-M3 design
Barel Shifter
Set jump and long jump
Set jump and long jump
It returns ZERO when returns first time.
Set jump and long jump
2. longjmp(jmp_buf resume_here, int i) is then called. This says go back to place where to resume from defined by ‘resume_here’.
This makes it look like you’re returning from original ‘setjmp()’ but return value of integer ‘i’ to let code tell when you actually got back here via ‘longjmp()’.
3. After longjmp()’ed, contents of ‘jmp_buf’ variable ‘resume_here’ destroys.
Set jump and long jump
Set jump and long jump
Summary of static memory allocation
Dynamic Memory Allocation �
int a[5];
int i,n;
scanf(“%d”, &n);
For(i=0; i<n; i++)
{
scanf(“%d”, &a[i]);
}
Overflow
or
Underflow
Dynamic Memory Allocation �
Code Segment: Compiled program with executive instructions are kept in code segment. It is read only. In order to avoid over writing of stack and heap, code segment is kept below stack and heap.
Data Segment: Global variables and static variables are kept in data segment. It is not read only.
Stack: A stack is usually pre-allocated memory. The stack is a LIFO data structure. Each new variable is pushed onto the stack. Once variable goes out of scope, memory is freed. Once a stack variable is freed, that region of memory becomes available for other variables. The stack grows and shrinks as functions push and pop local variables. It stores local data, return addresses, arguments passed to functions and current status of memory.
Heap: Memory is allocated during program execution. Memory is allocated using new operator and deallocating memory using delete operator.
��Abstract dynamic memory allocation��
Memory Allocation :
Abstract dynamic memory allocation
Memory Deallocation:
Deallocation of memory using delete Keyword
Heap Memory allocation is slower than a stack.
In heap there is no particular order in which you can allocate memory as in stack
Dynamic memory allocation
Abstract dynamic memory allocator
Abstract dynamic memory allocator
void *allocate_clear_memory( size_t n );
void *reallocate_memory( void *p_memory, size_t n );
Abstract dynamic memory allocator
Static | allocated at compile or design time | deterministic | allocation and deallocation are performed during the initialization and termination of processes |
Dynamic | allocated at run time | stochastic | allocation and deallocation occurs during the execution of the process |
Abstract dynamic memory allocator
1. require manual deallocation by the developer, or
2. the system may perform automatic deallocation.
Manual allocation management:
Manual allocation management:�
1. pointers that store addresses of memory that have not yet been initialized are referred to as wild pointers,
2. pointers that store addresses of memory that has been freed are referred to as dangling pointers,
3. the same memory being freed multiple times, and
4. memory that is allocated but not appropriately deallocated when it is no longer needed; that is, a memory leak.
Manual allocation management:�
a. Wild pointers –
Example:
int main()
{
int *p;
*p = 10;
return 0;
}
Manual allocation management:�
single_list_t *p_list = (single_list_t *) malloc( sizeof( single_list_t ) ); single_list_push_front( p_list, 42 );
Manual allocation management:�
int main()
{
Int var =10;
int *p;
*p = &var;
return 0;
}
single_list_t *p_list = (single_list_t *) malloc( sizeof( single_list_t ) );
single_list_init( p_list );
single_list_push_front( p_list, 42 );
Manual allocation management:�
#define SINGLE_LIST( list ) single_list_t list; \
single_list_init( &list )
#define SINGLE_LIST_P( p_list ) \
single_list_t *p_list = (single_list_t *) malloc( sizeof( single_list_t ) ); \
single_list_init( p_list )
Now our code looks like:
SINGLE_LIST( list );
single_list_push_front( &list, 42 );
or
SINGLE_LIST( p_list );
single_list_push_front( &p_list, 42 );
Manual allocation management:
Dangling pointer
free( ptr );
ptr = NULL;
free( ptr );
#ifdef DEVELOPMENT
ptr = NULL;
#endif
Manual allocation management:
Dangling pointer
1. still flag that memory as allocated, so no issues occur,
2. cause the program to crash, or
3. have reallocated that memory to the same task, but it is now being used for a different purpose.
Manual allocation management:
Freeing the same location more than once :
1. the allocator will cause the program to stop execution,
2. the memory may have since been allocated again, in which case, you would free memory that was not meant to be freed, or
3. heap corruption—the heap is in an inconsistent state and operations that mange it will be unpredictable.
Manual allocation management:
Memory leaks:
Manual allocation management:
Memory leaks:
1. in an embedded system where memory is more limited as compared to what one would expect from a desktop or laptop system,
2. in an embedded system that is meant to execute for an extended period of time (even years),
3. when memory may be shared by multiple processes and where the termination of one of these processes does not necessarily cause the memory to be collected, and
4. in a device driver.
Dynamic memory allocation
Automatic allocation management:
Automatic allocation management has two aspects:
Automatic initialization:
Type *p_entry = (Type *) malloc( sizeof( Type ) );
type_init( p_entry, … );
Dynamic memory allocation
Automatic initialization:
Garbage collection:
We will describe
Dynamic memory allocation
Garbage collection:
Garbage collection algorithms:
There are two mechanisms for dealing with garbage collection:
Automatic allocation management
Automatic allocation management
Automatic allocation management
Dynamic memory allocation
Reference Counting:
1. the unary dereference operator *,
2. the assignment operator =,
3. the auto increment and decrement operators ++ and --, are overloaded.
1. cycles cannot be detected (what if head and ptr are set to NULL),
2. it requires O(n) additional memory where n is the number of pointers or references, and
3. it is not real-time, as the reference count of multiple objects may have be decremented even if none of them are eligible for garbage collection.
Tracing algorithm
Garbage collection in C
1. installing and including the library with #include "gc.h";
2. initializing the garbage collection with a call to GC_INIT();
3. replacing all calls to malloc(…) with calls to
4. replacing all calls to realloc(…) with calls to GC_REALLOC(…); and
5. remove all calls to free(…).
You can access the size of the heap with GC_get_heap_size().
Issues with Garbage collection
Allocation strategies
Reference from: https://www.javatpoint.com/memory-management-operating-system
Allocation strategies
Contiguous memory management schemes:
1. Single contiguous memory management schemes:
Allocation strategies
Advantages :
Disadvantages
Allocation strategies
2. Multiple Partitioning:
Allocation strategies
1. fixed block sizes,
2. variable block sizes,
3. a composition of these two schemes, and
4. other advanced memory allocation schemes.
Allocation strategies
Fixed Partitioning
Reference from: https://www.tutorialandexample.com/fixed-partitioning/
Allocation strategies
Fixed Partitioning
Advantages of Fixed Partitioning memory management schemes:
Disadvantages of Fixed Partitioning memory management schemes:
Allocation strategies
Allocation strategies
Dynamic Partitioning
Reference from: https://www.tutorialandexample.com/fixed-partitioning/
Allocation strategies
Dynamic Partitioning
Advantages of Dynamic Partitioning memory management schemes:
Allocation strategies
Dynamic Partitioning
Disadvantages of Dynamic Partitioning:
Complex Memory Allocation: – In the case of dynamic partition, the task of allocation and deallocation is tough because the size of the partition varies whenever the partition is allocated to the new process. The operating system has to keep track of each of the partitions.
External Fragmentation: – Suppose we have three processes P1 (1MB), P2 (3MB), and P3 (1MB), and we want to load the processes in the various partitions of the main memory.
Now the processes P1 and P3 are completed, and space that is assigned to the process P1 and P3 is freed. Now we have partitions of 1 MB each, which are unused and present in the main memory, but we cannot use this space to load the process of 2 MB in the memory because space is not contiguous.
Allocation strategies
Problem with Compaction
Reference from: https://examradar.com/memory-management-2/
Allocation strategies
Bit Map Linked List
Bit Map
Allocation strategies
Bit Map
Reference from: https://www.javatpoint.com/memory-management-operating-system
Allocation strategies
Bit Map
Disadvantages of using Bitmap
2. To identify any hole in the memory, the OS need to search the string of 0s in the bitmap. This searching takes a huge amount of time which makes the system inefficient to some extent
Allocation strategies
Linked List
Reference from: https://www.javatpoint.com/memory-management-operating-system
Allocation strategies
Partitioning Algorithms
1. First Fit Algorithm
Allocation strategies
Partitioning Algorithms
2. Next Fit Algorithm
3. Best Fit Algorithm
Allocation strategies
Partitioning Algorithms
4. Worst Fit Algorithm
The first fit algorithm is the best algorithm among all because
Case study: FreeRTOS
typedef struct block_link {
struct block_link *p_next_free; /* The next free block in the list. */
size_t size; /* The size of the free block. */
} block_link_t;
Case study: FreeRTOS
block_link_t *p_cast_ptr = (block_link_t *) p_ptr;
then the compiler will treat this as a record of 8 bytes, as sis shown in Figure 5-14
p_cast_ptr->p_next_free = NULL;
p_cast_ptr->size = 42;
Case study: FreeRTOS
IMPLEMENTATION:
Case study: FreeRTOS
Case study: FreeRTOS
Case study: FreeRTOS
Allocation only:
1. First it checks to determine whether or not the allocated memory should be aligned with the word size of main memory. If so, it increases the size of n to a multiple of the word size.
2. Secondly, it ensures that the memory allocated neither exceeds the total memory that can be allocated nor causes an overflow.
3. Finally, if configured, it sets a hook (the error handling mechanism for FreeRTOS whereby this user-defined function is called) if the allocation failed.
1. all tasks are created and
2. all memory including that for queues and semaphores is allocated
Case study: FreeRTOS
Allocation only:
Case study: FreeRTOS
Best fit without coalescence:
Case study: FreeRTOS
Best fit without coalescence:
Case study: FreeRTOS
Best fit without coalescence:
Case study: FreeRTOS
Standard library malloc and free:
Case study: FreeRTOS
First fit with coalescence:
Case study: FreeRTOS
First fit with coalescence:
1. If all three are contiguous, they will be joined into a single block (so if the block of size 40 is unallocated, it would be merged with the two surrounding blocks forming one of size 16 + 40 + 24 = 80).
2. If the previous block is contiguous with the deallocated block, those two would be merged (so if the second block of size 16 is deallocated, it would be merged with the block of size 24).
3. If the next block is contiguous with the deallocated block, the would be merged (not shown).
4. Otherwise, the deallocated block becomes a link in the linked list (so if the block of size 56 is deallocated, it becomes another node in the linked list)
Case study: FreeRTOS
First fit with coalescence over multiple regions:
Other features: Clearing and Reallocation
Other features: Clearing and Reallocation
Reallocation in C++ and the move constructor and move assignment operator:
Other features: Clearing and Reallocation