This is an appendix to Understanding Unix/Linux Programming written for people interested in Ada programming on POSIX systems.

Please make sure that you have this alias set up:

% alias gnatmake='gnatmake -gnat05 -gnatE -gnato -gnatv -gnati1 -gnatf -fstack-check -m -O3'

You can check if it is set correctly like this:

% alias gnatmake
gnatmake='gnatmake -gnat05 -gnatE -gnato -gnatv -gnati1 -gnatf -fstack-check -m -O3'
%

When you have added this to your shell profile, gnatmake will automatically have the appropriate settings for compiling the examples shown here.

Processes and Programs: Studying sh

We use some of the primitives which are not available in POSIX/Ada in some of the examples for this chapter. We do this to get a better understanding of what is actually going on at the operating system level.

Section 8.4.2, page 257-261

Here is a complete program that runs ls -l: exec1.adb

We compile and run the program:

% gnatmake -P exec1
[...]
% ./exec1
* * * About to exec ls -l
totalt 36
-rwxr-xr-x 1 sparre sparre 16934 2007-09-07 07:46 exec1
-rw-r--r-- 1 sparre sparre   856 2007-09-07 07:45 exec1.adb
-rw-r--r-- 1 sparre sparre  2554 2007-09-07 07:46 exec1.ali
-rw-r--r-- 1 sparre sparre   238 2007-09-07 07:46 exec1.gpr
-rw-r--r-- 1 sparre sparre  1680 2007-09-07 07:46 exec1.o
-rw-r--r-- 1 sparre sparre   126 2007-09-07 07:21 Makefile
%

[...] We call this program psh1.adb, which stands for a prompting shell.

We compile and run the program:

% gnatmake -P psh1
[...]
% ./psh1
Arg[0]? ls
Arg[1]? -l
Arg[2]? 
totalt 132
-rwxr-xr-x 1 sparre sparre 16934 2007-09-07 07:46 exec1
-rw-r--r-- 1 sparre sparre   856 2007-09-07 07:45 exec1.adb
-rw-r--r-- 1 sparre sparre  2554 2007-09-07 07:46 exec1.ali
-rw-r--r-- 1 sparre sparre   238 2007-09-07 07:46 exec1.gpr
-rw-r--r-- 1 sparre sparre  1680 2007-09-07 07:46 exec1.o
-rw-r--r-- 1 sparre sparre   126 2007-09-07 07:21 Makefile
-rwxr-xr-x 1 sparre sparre 33091 2007-09-07 08:13 psh1
-rw-r--r-- 1 sparre sparre  1372 2007-09-07 08:13 psh1.adb
-rw-r--r-- 1 sparre sparre  3996 2007-09-07 08:13 psh1.ali
-rw-r--r-- 1 sparre sparre   304 2007-09-07 08:13 psh1.gpr
-rw-r--r-- 1 sparre sparre  7108 2007-09-07 08:13 psh1.o
-rw-r--r-- 1 sparre sparre  4890 2007-09-07 08:13 ustrings.ali
-rw-r--r-- 1 sparre sparre  7172 2007-09-07 08:13 ustrings.o
%

Section 8.4.3, page 261-265

Example: forkdemo1.adb - Creating a new process

% gnatmake -P forkdemo1
[...]
% ./forkdemo1
Before: My process ID is 28574
After: My process ID is 28575, fork() said 0
After: My process ID is 28574, fork() said 28575
%

Example: forkdemo2.adb - Children creating processes

% gnatmake -P forkdemo2
[...]
% ./forkdemo2
[predict the output]
%

Example: forkdemo3.adb - Distinguishing parent from child

% gnatmake -P forkdemo3
[...]
% ./forkdemo3
Before: My process ID is 29639
I am the child.  My process ID is 29640
I am the parent.  My child has process ID 29640
%

Section 8.4.4, page 265-271

Example: waitdemo1.adb - Notification

% gnatmake -P waitdemo1
[...]
% ./waitdemo1
Before: My process ID is 30597
Child 30598 here.  Will sleep for 2.000 seconds.
Waiting ...
Child done.  About to exit.
Done waiting for 30598.  Wait returned: 30598
%

Example: waitdemo2.adb - Communication

% gnatmake -P waitdemo2
[...]
% ./waitdemo2
Before: My process ID is 32321
Child 32322 here.  Will sleep for 5.000 seconds.
Waiting ...
Child done.  About to exit.
Done waiting for 32322.  Wait returned: 32322
Status: exit = 42 signal = 0 core = FALSE
% ./waitdemo2 &
% Before: My process ID is 32429
Child 32430 here.  Will sleep for 5.000 seconds.
Waiting ...
kill 32430
% Done waiting for 32430.  Wait returned: 32430
Status: exit = 0 signal = 15 core = FALSE

[1]  + done       ./waitdemo2
%

Section 8.5, page 272-275

Our second version of the prompting shell is psh2.adb.

Explorations, page 280-281

8.2

Predict the output of exploration_8_2.adb.

How would the output chainge if the three lines with comments were removed?

8.4

Consider the code exploration_8_4.adb.

8.5

Consider the code exploration_8_5.adb.

8.6

Compile and run the program exploration_8_6.adb.

Programming exercises, pages 281-282

-- 

Go to next chapter.

Collected examples:
posix-in-ada.zip
Based on:
Understanding Unix/Linux Programming, Bruce Molay, ISBN 0-13-008396-8.
Main page:
http://edb.jacob-sparre.dk/Posix_in_Ada/
Written by:
Jacob Sparre Andersen.
Latest update:
11th of September, 2007