PA GE NO 3 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. (10) SEE OVER PA GE NO 4 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. (10) SEE OVER PA GE NO 5 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, sev eral 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. (10) SEE OVER PA GE NO 6 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. (10) 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. (10) 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. (5) 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. (5) END OF PAPER
欢迎咨询51作业君