FAMILY NAME: GIVEN NAMES: SIGNATURE:
STUDENT NUMBER:
SEMESTER 2, 2019 EXAMINATIONS Physics, Mathematics & Computing
This paper contains: 6 Pages (including title page)
INSTRUCTIONS:
This paper contains 6 pages and 6 questions You are required to attempt ALL 6 questions
Systems Programming
Programming and Systems Time Allowed: 2:00 hours
THIS IS A CLOSED BOOK EXAMINATION
SUPPLIED STATIONERY ALLOWABLE ITEMS 1 x Answer Booklet 18 Pages No Allowable Items.
PLEASE NOTE
Examination candidates may only bring authorised materials into the examination room. If a supervisor finds, during the examination, that you have unauthorised material, in whatever form, in the vicinity of your desk or on your person, whether in the examination room or the toilets or en route to/from the toilets, the matter will be reported to the head of school and disciplinary action will normally be taken against you. This action may result in your being deprived of any credit for this examination or even, in some cases, for the whole unit. This will apply regardless of whether the material has been used at the time it is found. Therefore, any candidate who has brought any unauthorised material whatsoever into the examination room should declare it
to the supervisor immediately. Candidates who are uncertain whether any material is authorised should ask the supervisor for clarification.
Candidates must comply with the Examination Rules of the University and with the directions of supervisors. No electronic devices are permitted during the examination.
All question papers and answer booklets are the property of the University and remain so at all times.
This page has been left intentionally blank
1) Consider the C99 function whose prototype is:
int *fillNewArray(char *range, int *length);
The role of the function is to return an array of integers that has been initialised with the sequence of values requested by its first argument. The first argument is a character string, representing two non-negative integer values separated by a hyphen character, such as “1-5”. Note that the two integers may be provided in decreasing order to request a decreasing sequence of values, such as with “5-1”.
On success, the function dynamically allocates an array of integers, initialises it with the requested values, sets the integer pointed to by its second parameter to the number of allocated integers, and returns a pointer to the allocated array.
If any errors are encountered with the function¡¯s parameters, or if the requested array cannot be allocated, the function should return the NULL pointer.
Consider the following variable definitions and successful calls to the function: int *a, length;
a = fillNewArray(“0-3”, &length);
// sets a = { 0, 1, 2, 3 } and length=4.
a = fillNewArray(“1-1”, &length);
// sets a = { 1 } and length=1.
a = fillNewArray(“3-8”, &length);
// sets a = { 3, 4, 5, 6, 7, 8 } and length=6.
a = fillNewArray(“14-7”, &length);
// sets a = { 14, 13, 12, 11, 10, 9, 8, 7 } and length=8.
Write the fillNewArray() function in C99.
2) Consider the C99 function whose prototype is:
int removeFilesContainingString(char *dirname, char *string); The role of the function is to remove all text files from the indicated directory that contain the
indicated string. On success, the function returns the number of files that were removed.
If the function encounters any problems with its parameters, or is unable to open the indicated
directory, then it should immediately return -1.
To remove a file from its parent directory the function should use the unlink() system-call,
whose prototype follows:
int unlink(char *fullpathname); On success, unlink() returns 0, and -1 on failure.
You may assume that the indicated directory contains only text files and other directories. The function does not recursively search the indicated directory.
Write the removeFilesContainingString() function in C99.
3) Imagine that there exists a command-line program named goodchessmove that considers the state of an ongoing chess game, chooses a random piece belonging to the player whose turn it is next, and attempts to find a good legal move for that piece. goodchessmove employs a random, heuristic algorithm, and so different invocations may choose a different move for any one piece. A typical command-line invocation of goodchessmove would be:
prompt> goodchessmove gamestate chosenmove
where gamestate is the name of a file containing the current state of the game (board positions, history of moves, whose turn it is, etc) and chosenmove is a file into which the program writes its output.
On a multi-core processor, several different instances of goodchessmove can run simultaneously and, if many instances are run over time, a different program could rank all output files to decide the best next move.
Write a C99 program to execute an indicated number of instances of goodchessmove on a multi-core computer. A typical command-line invocation of this program would be:
prompt> manychessmoves 40 gamestate goodmove
which requests that 40 distinct instances of goodchessmove each determine a good next move for the game stored in the file named gamestate, and that the instances write their output to unique files named goodmove-1, goodmove-2, … goodmove-40.
The manychessmoves program calls the library function: int numberOfCores(void);
to determine how many cores are available on the current computer (1, 2, 4…) and attempts to keep this number of instances of goodchessmove running at any one time until all have completed.
Use the C99 functions and system-calls fork(), execl(), and wait() to implement the manychessmoves program.
On success, manychessmoves will exit with an exit-status of 0, or with 1, as quickly as possible, if it encounters any form of failure.
4) Consider the command-line program named mycp, whose typical invocation follows: prompt> mycp filename destination
If destination is the name of an existing directory, then the indicated source file is copied into that directory. Otherwise, the indicated source file is copied to a file whose name is given by the value of destination.
On success, the program exits with an exit-status of 0, or 1 if any problems are encountered. Note that the source file may be any type of file, not just a text file.
Write the mycp program in C99.
5) With reference to one or more diagrams, explain what information must be managed internally by a Unix-based operating system kernel when a process invokes a fork() system-call and then a wait() system-call, and its child process invokes an execve() system-call and eventually invokes an exit() system-call.
6a) With reference to two distinct examples, explain The Principle of Referential Locality, and explain how an operating system kernel may use it to improve performance.
6b) Explain how an operating system¡¯s use of virtual memory can enable the operating system to appear to support the use of more memory than is physically installed in a computer.
END OF PAPER