Skip to content
Tutorials Center
My learning

Topics

Lesson 36 of 36 ยท Projects and Practice

Practice: Ada Concepts Review

Review tasking, exceptions, contracts, SPARK and systems programming in Ada with a mixed set of questions, worked exercises with answers and runnable C practice programs.

You have now seen the advanced half of Ada: concurrency with tasks and protected objects, exceptions and contracts, SPARK proofs, and systems programming. This lesson helps you check what stuck. It has a mixed quiz, a set of paper exercises with answers in the text, and two runnable C practice programs. The Ada examples are read-only (no Ada compiler on this site).

How to use this page

  1. Try each exercise on paper first, then compare with the answer.
  2. For every wrong answer, reread the lesson linked beside it.
  3. Write the Ada versions yourself in an Alire project (see GNAT, GPRbuild and Alire) when you have a compiler.

Exercises with answers

Exercise 1 (tasks). A procedure declares two tasks and then executes Put_Line ("done") as its only statement. When does the procedure return? Answer: only after both tasks have terminated. The procedure is their master, and a master waits for its dependents (Tasks and Task Types).

Exercise 2 (protected objects). You have Count : Natural shared by ten tasks. Which of procedure, function or entry do you use for Increment, and which for Value? Answer: a procedure for Increment (exclusive write) and a function for Value (shared read-only) (Protected Objects).

Exercise 3 (select). What is wrong with this task body?

ADA
loop
   select
      accept Start;
   or
      delay 1.0;
   or
      terminate;
   end select;
end loop;

Answer: terminate and delay alternatives cannot be mixed in the same select. Remove one: use delay for a watchdog, or terminate for clean shutdown (Select, Delay and Termination).

Exercise 4 (exceptions). X : Positive := Compute; appears in a procedure's declarations, and Compute raises Constraint_Error. The procedure has a handler for Constraint_Error. Is it triggered? Answer: no. Exceptions in the declarative part propagate to the caller (Propagation, Cleanup and Finalization).

Exercise 5 (contracts). Write a postcondition for procedure Swap (A, B : in out Integer). Answer: Post => A = B'Old and B = A'Old (Preconditions and Postconditions).

Exercise 6 (SPARK). Why does a proof of Sum := Sum + R (I) in a loop need a loop invariant? Answer: the prover does not run the loop; the invariant tells it how large Sum can be at each iteration (Proving Absence of Run-Time Errors).

Exercise 7 (layout). A 3-bit field Mode sits in bits 1 to 3 of a register. Which mask and shift read it from a 16-bit word R? Answer: (R >> 1) & 7 (Representation Clauses and Bit Layout).

Practice program 1: bit fields

Implement get_field and set_field as in the layout lesson and use them to decode a status word. The version below is a working solution; try writing yours first, then compare.

C Practice: decode a status word

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

The output should be ready = 1, mode = 6, speed = 5, error = 0x2B. Check each value by writing 0x2B5D in binary and grouping the bits.

Practice program 2: a guarded counter

Modify the barrier example so that a waiting thread resumes only when the count reaches exactly 3, then increase the producer to 5 increments. Predict what happens before you run it: a count >= 3 barrier opens, while count == 3 could be missed if the count moves past 3 before the waiter wakes up. This is why barriers use comparisons that stay true.

C Practice: barrier with a threshold

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

Mixed review quiz

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. Which Ada construct lets a server task stop automatically when all its possible callers are gone?

    terminate ends service tasks once their master and sibling tasks are finished or waiting to terminate.
  2. 2. Which is checked by Type_Invariant?

    Type invariants apply to private types and are checked after public operations.
  3. 3. What does GNATprove report for a run-time check it cannot show to be safe?

    Unproved is not the same as wrong; strengthen types, contracts or invariants.
  4. 4. Why is a protected function allowed to run concurrently with other functions of the same object?

    Read-only access needs no exclusion among readers.
  5. 5. Which pair correctly describes importing a C function into Ada?

    Import and the C convention declare a subprogram implemented in C.

Where to go next

  • Build a small Alire project that uses a protected buffer, a producer and a consumer.
  • Add contracts to a package you already wrote and enable assertions with -gnata.
  • Pick a safety property of a program you know and try to state it as a postcondition or invariant.
  • Browse the SPARK documentation on assurance levels and try to reach Bronze on a small unit.

Summary

  • Tasks, protected objects and selective accept cover concurrent design; masters wait for dependents.
  • Exceptions propagate to callers; declarations and tasks have special rules; controlled types clean up.
  • Contracts, predicates and invariants capture what must hold, and SPARK can prove them.
  • Representation clauses, C interfacing and the GNAT/Alire toolchain support systems work.
  • The projects tie these together: a bounded buffer and a state machine.

Next: back to the Ada tutorial home