Tutorial Week 5
What features are desirable of a language designed for writing Operating Systems?
Ans:
At the low level: ability to read/write to absolute hardware addresses; use
data types that correspond to machine hardware types (unsigned/signed ints,
etc); be able to link with Assembler routines; ability to have inline Assembler.
At the high level: be able to offer a high-level API to applications.
What features are desirable of an applications programming language that needs
to use Operating System services?
Ans:
It must be able to link with the O/S libraries.
Must the languages used to write applications be the same as the language used
to write the O/S?
Ans:
Of course not - MSDOS is written in Assembler, but most programs are in Pascal
or C. You may lose the ability to exploit all of the O/S services if, for example,
the application programming language is unable to use the data types in the
O/S API.
The following programs in Ada and C print the odd numbers from 1 to 39 inclusive.
Discuss the similarities and differences between the two.
Ada:
WITH Text_IO;
WITH My_Int_IO;
PROCEDURE OddNumbers IS
BEGIN -- OddNumbers
OddNumber: Integer;
OddNumber := 1;
WHILE OddNumber <= 39 LOOP
My_Int_IO.Put(Item => OddNumber, Width => 3);
OddNumber := OddNumber + 2;
END LOOP;
Text_IO.New_Line;
END OddNumbers;
C:
#include
int main(int argc, char *argv[])
{
int OddNumbers;
OddNumbers = 1;
while (OddNumbers <= 39) {
printf(" %d", OddNumbers);
OddNumbers += 2;
}
putchar('\n');
}
Ans:
The obvious differences are syntactic, with operators like += in C, {...} instead
of LOOP...END LOOP, etc. The Ada exmple shows size formatting in the output
- the C printf can also do that, but it isn't shown. Ada does not have a predefined
entry point to a program so it defines a Procedure, whereas C has to have ``main''
as entry point. Ada uses the IO packages, and the Ada environment should elaborate
these automatically on linking. In C, the standard library is automatically
linked, and an O/S specific linker would also link in the O/S kernel libraries.
However, any other libraries may have to be linked explicitly. Note: I am using
the ``Indian Hill'' style for programs, and so is the recommended reading of
Kelley and Pohl. Use whatever style you want, but be consistent.
Write a C program which reads characters from standard input and writes to
standard output. It converts the characters read in the following way: all
upper case characters to the equivalent lower case, all lower case to the equivalent
upper case, and all others left unaltered.
Ans:
I use the library functions here cos I am lazy. The studnets may be better
off expanding them out. NB Use ANSI C, not K&R C. This is a change from last
year.
#include
int main(int argc, char *argv[])
{ int ch;
while ((ch = getchar()) != EOF) {
if (isupper(ch)) putchar(tolower(ch));
else if (islower(ch)) putchar(toupper(ch));
else putchar(ch);
}
exit(0);
}
Write a program that reads characters from standard input, checking that they
are all digits. The program should exit with a zero exit status if it reaches
end-of-line and all characters read are digits. If it reads a non-digit character
it should cease reading and exit with a non-zero exit status.
Ans:
#include
int main(int argc, char *argv[])
{
int ch;
while ((ch = getchar()) != '\n')
if (! isdigit(ch)) exit(1);
exit(0);
}
Write a function ``int tolower(int ch)'' that takes a single character and
converts it to lower case. If the character is not an upper-case character
it is unchanged.
Ans:
int tolower(int ch)
{
if ('A' <= ch && ch <= 'Z')
return (ch + 'a' - 'A');
return ch;
}
Write a function to calculate the factorial of a positive integer. If the number
is negative it should return zero, otherwise it should calculate the factorial
using the recusive formula
0! = 1
n! = n # (n-1)!
Ans:
The type should probably be long for a real factorial function.
int factorial(int n)
{
if (n == 0)
return 1;
return n * factorial(n - 1);
}
Laboratory Week 5
Look in the files /usr/include/limits.h. What are the largest and smallest
integers that are supported? Write and test a program to print out their values.
The following program has a common and very serious error. What is it? What
happens when you attempt to run it?
int main(int argc, char *argv[])
{
char *p;
*p = 'a';
}
Ans:
It crashes because the pointer is uninitialised and is probably pointing to
junk
Create and compile the tutorial questions. Use the debugger ``ups'' to step
through program execution.
Write a simple ``main'' program for question 8 that will read integers from
standard input until EOF is reached and prints the corresponding factorial.
If you have any left-over assignments that belong to students who moved to
other tutorials, can you give them to me so that I can spread them around?
If you have any marks too, pass them in and I will enter them into the marks
database.