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.
shWe 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.
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 %
% 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 %
% gnatmake -P forkdemo2 [...] % ./forkdemo2 [predict the output] %
% 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 %
% 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 %
% 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 %
Our second version of the prompting shell is psh2.adb.
Predict the output of exploration_8_2.adb.
How would the output chainge if the three lines with comments were removed?
Consider the code exploration_8_4.adb.
Consider the code exploration_8_5.adb.
Compile and run the program exploration_8_6.adb.
Go to next chapter.
Understanding Unix/Linux Programming, Bruce Molay,
ISBN 0-13-008396-8.