• Home
  • Textbooks
  • Real-Time Systems and Programming Languages: Ada, Real-Time Java and C/Real-Time POSIX
  • Concurrent programming

Real-Time Systems and Programming Languages: Ada, Real-Time Java and C/Real-Time POSIX

Alan Burns, Andy Wellings

Chapter 4

Concurrent programming - all with Video Answers

Educators


Chapter Questions

Problem 1

A particular operating system has a system call (RunConcurrently) which takes an unconstrained array. Each element of the array is a pointer to a parameterless procedure. The system call executes all the procedures concurrently and returns when all have terminated. Show how this system call can be implemented in Ada using the Ada tasking facilities. Assume that the operating system and the application run in the same address space.

Check back soon!

Problem 2

Write Ada code to create an array of tasks where each task has a parameter which indicates its position in the array.

Check back soon!

Problem 3

Show how a cobegin can be implemented in Ada.

Check back soon!

Problem 4

Can the fork and join method of task creation be implemented in Ada without using intertask communication?

Check back soon!
01:42

Problem 5

How many POSIX processes are created with the following procedure?
for $(i=0 ; i<=10 ; i++)\{$
fork();
\}

James Kiss
James Kiss
Numerade Educator

Problem 6

Rewrite the simple embedded system illustrated in Section 4.8 in Java and C/RealTime POSIX.

Check back soon!
01:14

Problem 7

If a multithread process executes a POSIX-like fork system call, how many threads should the created process contain?

James Kiss
James Kiss
Numerade Educator

Problem 8

Show, using concurrent tasks, the structure of a program to control access to a simple car park. Assume that the car park has a single entrance and a single exit barrier, and a full sign.

Check back soon!

Problem 9

Explain, with the help of the following program, the interactions between Ada's rules for task termination and its exception propagation model. Include a consideration of the program's behaviour (output) for initial values of the variable C of 2,1 and 0 .
with Ada.Text_Io; use Ada.Text_Io;
procedure Main is
task A;
task body $A$ is
C : Positive : = Some_Integer_Value;
procedure $\mathrm{P}(\mathrm{D}$ : Integer) is
task T;
A : Integer;
task body $\mathrm{T}$ is
begin
delay 10.0;
Put("T Finished"); New_Line;
end $\mathrm{T}$;
begin
Put ("P Started"); New_Line;
$\mathrm{A}:=42 / \mathrm{D}$;
Put ("P Finished"); New_Line;
end $\mathrm{P}$;
begin
Put ("A started"); New_Line;
$\mathrm{P}(\mathrm{C}-1)$;
Put("A Finished"); New_Line;
end $\mathrm{A}$;
begin
Put("Main Procedure Started"); New_Line;
exception
when others $\Rightarrow$
Put("Main Procedure Failed"); New_Line;
end Main;

Check back soon!

Problem 10

For every task in the following Ada program indicate its parent and guardian (master) and if appropriate its children and dependants. Also indicate the dependants of the Main and Hierarchy procedures.
procedure Main is
procedure Hierarchy is
task A;
task type B;
type $\mathrm{Pb}$ is access $\mathrm{B}$;
Pointerb : $\mathrm{Pb}$;
task body $A$ is
task C;
task D;
task body C is
begin
-- sequence of statements including
Pointerb := new $B$;
end $C$;
task body $D$ is
Another_Pointexb : $\mathrm{Pb}$;
begin
-- sequence of statements including
Another_Pointerb := new $B$;
end $D$;
begin
-- sequence of statements
end $A_{\text {; }}$
task body B is
begin
-- sequence of statements
end $\mathrm{B}$;
begin
-- sequence of statements
end Hierarchy;
begin
-- sequence of statements
end Main;

Check back soon!

Problem 11

To what extent can Figure 4.2 be used to represent the state transition diagram of (a) C/Real-Time POSIX pthreads and (b) Java threads?

Check back soon!

Problem 12

Given the following:
public class Calculate implements Runnable
\{
public void run()
\{
* long calculation */
\}
\}
Calculate MyCalculation = new Calculate();
what is the difference between:
MyCalculation.run();
and
new Thread(MyCalculation) .start ();

Check back soon!

Problem 13

Explain how a Java thread can be protected against being destroyed by an arbitrary thread.

Check back soon!