TTK4145 - Sanntidsprogrammering

5.0(2)
studied byStudied by 23 people
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
Card Sorting

1/136

encourage image

There's no tags or description

Looks like no tags are added yet.

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

137 Terms

1
New cards

Modules

  • Consistent abstraction

  • Clear, central purpose and well-named to reflect that purpose

  • Abstract enough to treat the class as a black box, without needing to understand its internal implementation

  • Complete and self-contained services, ensuring no meddling with internal data

  • Hide its implementation details from other classes as much as possible

  • Independent and loosely coupled, avoiding assumptions about its users or derived classes

2
New cards

Routines

  • Name describes exactly what is does

  • Is the reason for creating it sufficient? (Reduce complexity, avoid duplicate code, abstraction, simplify boolean tests)

  • Performs one well-defined task/is functionally cohesive

  • All parts that would benefit from being put into their own … has been put into their own …

  • Obvious and clear interface

  • Loose coupling - the connections are small, intimate, visible and flexible

  • Functions are preferred over procedures

3
New cards

Data Names

  • Type names are descriptive enough to help document data declarations

  • Variables are named well

    • Count/Index over Num

    • Avoid i, j or k as loop index

    • Booleans named so when they are true is obvious

  • Are variables only used for the purpose for which they were named?

  • Are well-named enumerated types used instead of makeshift flags or boolean variables?

  • Are named constants used instead of magic numbers or magic strings?

  • Do naming conventions distinguish among type names, enumerated types, named constants, local variables and global variables?

  • Code is more read than written!

4
New cards

Data Organization

  • Are extra variables used for clarity when needed?

  • Are references to variables close together?

  • Are data types simple so that they minimize complexity? (Data over objects)

  • Is complicated data accessed through abstract access routines (abstract data types)?

5
New cards

Code Control

  • Is the nominal path through the code clear?

  • Are related statements grouped together?

  • Have relatively independent groups of statements been packaged into their own routines?

  • Does the normal case follow the if rather than the else?

  • Are control structures so simple that they minimize complexity? (The code should be geometric)

  • Does each loop perform one and only one function, as a well-defined routine would?

  • Is nesting minimized? (Prefer longer functions over nesting, though none of them are particularly good)

  • Have boolean expressions been simplified by using additional boolean variables, boolean functions and decision tables?

6
New cards

Layout

Does the program’s … show its logical structure?

7
New cards

Design

  • Is the code straightforward, and does it avoid cleverness?

  • Are implementation details hidden as much as possible?

  • Is the program written in terms of the problem domain as much as possible rather than in the terms of computer-science or programming-language structures?

8
New cards

Patterns for intelligent design

State Machines, Permissible Inconsistencies, and Purity and Testability

9
New cards

State Machines

Framework for Looping and Reacting, Prevent Invalid States, Effective Design and Structured Reaction

10
New cards

Framework for Looping and Reacting

Create a pattern that reacts conditionally to inputs based on previous states

11
New cards

Prevent Invalid States

Make it hard to program incorrect behaviour by structuring the state machine to disallow invalid combinations

12
New cards

Effective Design

The state machine pattern ensures clear separation of states and transitions, minimizes invalid states, and simplifies debugging and testing

13
New cards

Structured Reaction

By defining clear state transitions and persistently storing essential data, the system reacts predictably to inputs, maintaining a reliable and manageable structure

14
New cards

Implementations steps for State Machines

Loop Initialization, Identify Inputs and Outputs, Persist State Information, Minimize Stored Data, State Enumeration and Reaction

15
New cards

Loop Initialization

Establish an infinite loop to continuously check inputs and generate outputs

16
New cards

Identify Inputs and Outputs

Determine discrete-time inputs (events/changes) and outputs to stream-line processing and reduce unnecessary activity.

Example inputs: Button press/release, floor sensor activation, stop button, timers.

Example outputs: Motor direction, door control, floor indicator, button lights

17
New cards

Persist State Information

Store minimal necessary inputs and outputs for conditional reactions.

Avoid duplication and invalid combinations by carefully analyzing and storing essential data

Example Stored Data: Button press status, current floor, movement status, door status and specific timers

18
New cards

Minimize Stored Data

Use heuristics to determine what needs to be stored and what can be derived or ignored.

Focus on essential states to reduce complexity: elevator’s position, stop button status, movement status, door status

Reduce to manageable combinations and identify invalid states to be avoided

19
New cards

State Enumeration and Reaction

Define a set of unique states based on stored data and possible inputs

Implement state transitions based on current state and received inputs

Use input-first or state-first approach depending on the structure that minimizes code duplication

20
New cards

Core and Shell

A way to structure code so that testable things are easy to test and not testable things don’t need much testing

21
New cards

Core

Pure Functions: Functions with explicit inputs and outputs, containing logic like decisions, algorithms, loops and conditions

No Side Effects: No access to global variables, network, disk or external communication

Testable: Easy to write tests for these functions because they are deterministic and independent of external state

22
New cards

Shell

State Management: Code that handles input, modifies state, and outputs results based on instructions from core functions

Side Effects: Includes all variables, communication (input/output), network, disk operations, etc.

Simple Structure: Contains minimal or no loops and conditionals, making it straightforward but less critical to test exhaustively

23
New cards

Benefits of Shell and Core

Separation of Concerns, Ease of Testing, Simplified Action Code and Integration with State Machines

24
New cards

Separation by Modification

Group by Modification Needs: Split data and outputs based on modification requirements. Each group should have its own state machine, avoiding unnecessary data grouping and ensuring clear responsibility.

Parallel state machines reduce the total number of enumerated states. Smaller individual states are easier to maintain

BAD!

25
New cards

Interacting State Machines

Create interfaces for state machine interactions, minimize interactions and avoid shared data/references.

Simplifies testing by isolating processes. Avoids the need for explicit synchronization. Ensures within-program communication mirrors between-program communication

BAD!

26
New cards

CAP Theorem

Consistency, Availability and Partition tolerance. Can only have two out of three

27
New cards

Protocols for Paradoxes

Limit contradictions and manage message acceptance/discarding. Use additive approaches, avoid negation and limit reset points.

28
New cards

Minimizing accessibility

Encapsulation, Simplicity and Minimalism, Completeness

29
New cards

Encapsulation

By minimizing accessibility to the members of a class or module, you encapsulate the internal workings of the class. This means the internal data structures and algorithms can be changed without affecting the code that uses the class

30
New cards

Simplicity and Minimalism

Exposing fewer details about the internals of a module simplifies and minimises the interface. A simpler interface is easier to understand and use correctly. Users of the module do not need to understand its inner workings, only the public interface. Only want/need orthogonal variables in the interface

31
New cards

Completeness

A complete interface provides all necessary functionalities without exposing the internal data. This means that all required operations can be performed through the provided methods, which should be well-documented and intuitive

32
New cards

Functional cohesion of functions

Isolation of Responsibilities, Reduced Coupling, Simple Building Blocks, Coherent Abstraction level, Debugging

33
New cards

Isolation of Responsibilities

A cohesive routine’s clear and isolated functionality prevents if from being influenced by the caller’s specific needs, avoiding hidden dependencies and complexity

34
New cards

Reduced Coupling

Cohesive routines reduce interdependence between parts of the code, making the codebase more modular and maintainable

35
New cards

Simple Building Blocks

Routines focused on one task can serve as reusable, composable building blocks, aligning with the concept of separation of concerns

36
New cards

Reliability

The software behaves according to its specification

37
New cards

Fault

The bug

38
New cards

Error

The event / the manifestation of a fault

39
New cards

Failure

System failed to behave according to spec

40
New cards

Failure/error modes

Different ways a system can fail

41
New cards

Fault Prevention

Reduce number of faults

42
New cards

Fault Tolerance

The ability of a system to continue operating properly in the event of the failure of (or one or more faults within) some of its components

43
New cards

Full Fault Tolerance

Ensures the system continues operation as normal, at least for some time

44
New cards

Graceful Degradation

Reduces the functionality to only perform core tasks while the system waits for repairs

45
New cards

Fail Safe

The system stops all operation, but ensures its integrity. Ex, a failing pitch controller on a helicopter might resset the pitch to zero before shutting down

46
New cards

Redundancy

Extra elements introduced by system designers to detect and handle faults

47
New cards

Static Redundancy

Duplicate components that are present and operating simultaneously. Ex. N-version programming

48
New cards

Dynamic Redundancy

Extra/duplicate components are invoked when needed. This type of redundancy has four stages: detect, confine, recover, treat. Ex. recovery blocks

49
New cards

Forward Error Recovery

Continues with selected corrections, requiring a clear understanding of the error and its remedies to avoid worsening the situation

50
New cards

Backward Error Recovery

Steps back to a previous state (recovery point) and attempts the same action with an alternative method. Risk the domino effect if recovery points are not synchronized.

Often not acceptable since real time systems often have interactions with other systems that may depend on it to behave correctly. There is no time to waste in a real time system; when we report back to the module that initiated the failed operation, it might be too late to retry/fix

51
New cards

N-version programming

Developing multiple independent versions of the same software specification and executing them on different hardware platforms or using different algorithms. HW components often fail individually, while multiple instances of the same SW on the same computer will all fail together

52
New cards

Recovery blocks

An implementation of dynamic recovery applied to software. Defines all entries to a software block as a recovery point, and then have an acceptance test before the block can exit

53
New cards

Failure Mode Merging

Combining failure modes to simplify fault handling or recovery mechanisms. By merging to one error mode, even unknown errors can be handled because we only care that the program failed in some way

54
New cards

Acceptance Tests

Tests to check that everything is OK. Tests performed to validate whether a system meets the acceptance criteria set by the specifications. Has a merging of failure modes effect and can detect unexpected errors

55
New cards

Checkpoints

Markers within a system that record its state at a particular point in time, enabling rollback or recovery to that state in case of failure

56
New cards

Error Detection - Learn by heart list

Replication checks, Reversal checks, Reasonableness checks, Dynamic Reasonableness checks, Timing checks, Coding checks, Structural checks

57
New cards

Replication checks

In N-version programming, … checks start a recovery routine if one or more of the N results are incorrect, useful in systems with uncertainty about correct results

58
New cards

Reversal checks

Applied when input-output relationship is one-to-one, enabling reversal calculation and comparison to detect errors

59
New cards

Reasonableness checks

Validate system knowledge, such as range checks for date and time validation or assertions

60
New cards

Dynamic reasonableness checks

Compare current output to previous output, detecting abnormal jumps or changes in system behaviour

61
New cards

Timing checks

Implemented with a watchdog timer, ensuring each process resets within a set interval, specifically for time failures

62
New cards

Coding checks

Utilize extra information sent with data for comparison, common in communication protocols, like using checksum to verify data integrity

63
New cards

Structural checks

Ensure correct data structure, like verifying the number of items in a list, useful for detecting unexpected changes

64
New cards

Coordinating recovery points

A method for avoiding the domino effect. Using backward error recovery in a multi-thread system involves rolling back threads. As there usually is inter-thread communication, rolling back one thread can trigger rollback in other threads, hence the domino effect.

65
New cards

No

Is it possible to achieve fault tolerance without redundancy?

66
New cards

Acceptance test for allocate()

  • Number of free items reduced with one

  • Number of allocated items increased with one

  • Sum of allocated and unallocated items is constant

  • Compare number of allocated items with applications view of allocated items

  • Checksum of allocated items

  • Check non-existence of references to unallocated items

67
New cards

Reliable storage

Find fault model: Write - failed, wrong data, wrong place. Read - failed, old data, bad data, wrong place

Detect, merge error modes, inject errors: Enhance buffer with address, checksum, versionID and status bit in addition to data itself. All errors → fail. Induce “decay thread” for error injection testing

Handling with redundancy: Implement multiple copies of the buffer (using version ID for latest). All errors in read will rewrite the data that caused the error to repair it. Repair thread periodically checks data for errors and repairs as necessary. RAID and ECC. Refresh thread

68
New cards

Reliable communication

Find fault model: Message lost, reordered, modified, delayed, duplicate, wrong recipient

Detect, merge error modes, inject errors: All errors → message lost. Add checksum, version ID, sequence number on message, sessions. Send acknowledgement on well received message. Error injection by throwing some messages away

Handling with redundancy: Acknowledgement, timeout and resending. Sequence numbers, sessions/connections, retransmission buffers

69
New cards

Reliable calculations

Fault mode: 1 mode; not to do the next correct side effect

Detect, merge fault modes inject errors: all failed acceptance tests → panic/stop

Handling with redundancy: checkpoint/restart, process pairs, persistent processes

70
New cards

Atomicity

An action is considered atomic when the tasks executing it operate independently of any other tasks. During its execution, no active task is aware of the ongoing activities within the action. State changes are only revealed after the action is concluded.

71
New cards

Transactions

Atomic actions with backward recovery as standard error recovery. Are either done correctly or not done at all, no inbetween

72
New cards

Heuristic transactions

A resource manager makes the decision to commit or abandon the transaction without waiting for a global decision from the transaction coordinator.

73
New cards

Interposition

The ability to insert additional processing or functionality between the initiation and completion of a transaction, without directly modifying the transactional logic itself

74
New cards

Atomic actions as module design pattern

No successfully for low-level SW design: Because it it too abstract and requires extensive infrastructure. Atomic actions also do not have a reasonable composition, which leads to a unstructured forest of actions. We don’t get help from the structures to structure the functionality as AC do not have a fixed structure.

Might work good on system level for distributed systems: If we imagine a distributed system of servers using each other’s services, atomic actions may be a good way to ensure consistency and manage interactions

75
New cards

Drawbacks of transactions

Heavy infrastructure

Sometimes errors are acceptable

Performance and HW overhead

Transactions are a specialised skill set - not common knowledge among engineers

Infrastructure required for transactions might not be fully developed or integrated in the chosen platform/tools.

76
New cards

Boundaries for atomic actions

Start: To establish which participants may be affected, and to set a safe, consistent, starting point (if an error has occurred, it must have happened after the start point.)

Side: Limiting communication (restricting messages, locking variables...) to members to hinder error spreading out of the Action.

End: Ensuring a consistent system before leaving the action so that any errors do not spread / have consequences after end of Action.

77
New cards

Realization of boundaries

Start: In static systems this might be hard coded. If not; membership list, action manager to keep track of members of each action, recovery point if preparing for backward error recovery.

Side: Resource locking of resources in action. Transaction ID must be part of all communication, so all threads wanting to act on a message must join the transaction.

End: Acceptance test, any vote or synchronisation; two-phase commit protocol

78
New cards

Optimistic Concurrency Control

Assumes that conflicts are rare. Transactions proceed without restrictions, but before committing changes, they check if accessed data has been modified by other transactions. If conflict arises, the transaction is rolled back. This is an alternative to locking, so it reduces locking overhead and improves system concurrency. May result in more transaction retries in more high-contention environments.

79
New cards

Log

An alternative to recovery points. Tracks every single change, and is able to save before-state (undo), store intended effect/new values (redo) and use checkpoints where all active operations are tracked.

80
New cards

Log manager

Handles the creation, maintenance and retrieval of log records, centralizing log management. Facilitates easier recovery and inspection

81
New cards

Transaction manager

Responsible for coordinating and managing transactions in a database system. Handles the initiation, execution and termination of transactions

82
New cards

Resource manager

Manages access to shared resources in a multi-user environment. Ensures that resource access aligns with transactional requirements by working closely with the transaction manager. Prevents conflicts and ensures data integrity by managing simultaneous access to resources

83
New cards

Two-Phase Commit Protocol

All participating systems either commit or roll back changes together, maintaining consistency. Coordinating transition on committing a transaction between phases (growing and shrinking) to avoid partial updates

84
New cards

Two-Phase Execution

To manage resource locking and ensure atomicity by dividing the execution into a phase where resources are acquired (growing) and a phase where they are released (shrinking). To reduce risk of deadlocks and ensure consistency

85
New cards

Read/Write- Locking of accessed resources

Control access to resources by allowing multiple readers and a single writer, preventing conflicts and ensuring data integrity. Reduces risk of race conditions and ensures safe data modifications

86
New cards

Key properties of atomic actions

Isolation, No Communication, State Consistency, and Indivisibility

87
New cards

Mechanisms to share errors in a multi-thread

Asynchronous notification, polling error status variable, message-based systems, asynchronous transfer of control (ATC)

88
New cards

Extended commit protocol

Share error status during commit protocol, forward error recovery can be triggered by coordinator

89
New cards

Asynchronous transfer of control

Handle errors asynchronously within the AA-frame. Enables one thread to interrupt another thread with termination mode, ensuring that the interrupted thread does not continue from where it left off after the interruption

90
New cards

Resumption model

After handling an error or signal, the execution resumes from the point where it was interrupted.

Often less used due to the need of polling and rechecking, state corruption and more difficult error handling.

91
New cards

Termination model

After handling an error or signal, the program control is transferred to a specific error handling block or routine, which manages the error and determines the subsequent steps. Assumes the program cannot continue in the same context, and must handle the error separately

92
New cards

Problems solved by setjmp and longjmp

Transforming a ‘Signal’ into ‘Termination Mode’, Zero-overhead error handling, General exception handling

93
New cards

Semaphore

A shared non-negative integer equipped with two methods; wait and signal. Atomic and eliminates the need for busy waiting.

94
New cards

wait(S)

If the value of the semaphore, S, is greater than zero then decrement its value by one; otherwise delay the task until S is greater than zero (and decrement its value)

95
New cards

signal(S)

Increment the value of the semaphore, S, by one

96
New cards

Critical Regions

An imagined language support for mutual exclusion and guards. Named code blocks are regions and they are mutually exclusive. Ensures mutual exclusion in critical regions of code to protect variables from concurrent access. Processes are blocked from entering a region if another task is active within it. Condition synchronization is managed through guards—logical conditions that must be true for a task to enter the region. If the guard is false, the task is delayed. There is no guaranteed order of access for tasks waiting to enter the same region. The guards can evaluate any variable. This becomes a problem since they must be polled continuously and can still be wrong when evaluating (NB ifs don’t work). Also the critical regions can be anywhere in the code, which is bad code quality. Imaginary because useless.

97
New cards

Monitors

A region with a collection of procedures and local variables, ie. module. There are condition variables, instead of guards, that can only be accessed from inside the …

98
New cards

Double Interactions

Involve two separate calls.

First Call (Non-blocking announcement): Checks conditions and signals intent without waiting to complete, essentially “registering” the request

Second Call (Blocking): This call actually performs the operation and wits until it can be safely executed

Non-Atomicity: The state might change between calls, leading to inconsistencies and difficult error recovery

Boundary Issues: State changes between calls complicate maintaining clear system boundaries

Usage in Ada: Ada doesn’t natively support this pattern easily, requiring careful design for consistency and synchronization

99
New cards

Requeue

Allows an ongoing operation (entry call) to be redirected to another entry. Allows for more complex synchronization patterns and can help manage situations where the state needs to be reassessed or additional conditions need to be met

100
New cards

Entry Families

A way to define multiple entries indexed by a parameter, forming an “array” of entries. Provides granular control by associating different conditions or actions with different indexes.