讲解:COMP 321、fdopen、c/c++、c/c++Java|R

COMP 321April 24, 2019Questions on this exam may refer to the textbook as well as to the manual pages on CLEAR.Therefore, you should not start the exam until you are in a position to access both.As a reminder, the notation fdopen(3) means that the manual page for the function fdopencan be displayed with the command man 3 fdopen.You may review any section of the textbook and any manual page on CLEAR during the exam,and not just those explicitly referenced by the questions. Moreover, you may review any of thelecture notes, labs, and assignments that the instructors have provided to you or any notes thatyou have taken for this class. You may also use CLEAR to develop and test your C code for thisexam. You may not consult any other sources of information, including other textbooks, web sites,or people, aside from the instructors.This is a 5 hour exam. You must take the exam in one continuous block of time. You maytake one break of up to one hour during the exam. Your exam period begins as soon as you lookat any page of the exam other than these instructions. Indicate in your exam submission the dateand time at which you start the exam, the time at which you start your break, the time at whichyou end your break, and the time at which you end the exam.Once you begin the exam, you may not discuss the exam with anyone other than the instructorsuntil the end of the finals period. If you find anything in the exam to be ambiguous, clearly stateall of the assumptions that you are making in solving the problem.All of the questions have brief answers of either a single paragraph or a single C function thatis small in size. Always explain your answer and comment any C code you write to explain whatit does. Most of your score will be determined by your explanation. Turn in your answers throughCanvas by 5PM on Wednesday, May 1st. Submit your answers in the form of a text file (.txt),following the same formatting guidelines as for your programming assignments.1 of 81. [20 points] Consider the following program.#include csapp.hvoidtwo(void){Write(STDOUT_FILENO, 2, 1);}intmain(void){if (Fork() == 0)atexit(two);if (Fork() == 0)Write(STDOUT_FILENO, 0, 1);elseWrite(STDOUT_FILENO, 1, 1);exit(0);}Determine which of the following outputs are possible. Explain your answer. Note: Theatexit(3) function takes a pointer to a function and adds it to a list of functions (initiallyempty) that will be called when the exit(3) function is called.A. 001212B. 010212C. 022110D. 102120E. 112002F. 210120Suppose Write is replaced by printf. For example, Write(STDOUT FILENO, 2, 1); becomesprintf(2);. How would this change affect your answer?2 of 82. [20 points] Modify the program cpfile.c from Figure 10.5 of the textbook so that it takesan optional command-line argument that is a file name. If a file name is given on the commandline, then copy the contents of that file to standard output; otherwise, copy standard inputto standard output as before. The twist is that your solution must use the original copy loop(lines 9-11 in the textbook) for both cases. You are only allowed to insert code. You are notallowed to change any of the existing code. The program cpfile.c is shown below for yourconvenience.#include csapp.hint main(int argc, char **argv){int n;rio_t rio;char buf[MAXLINE];Rio_readinitb(&rio, STDIN_FILENO); // line 9while ((n = Rio_readlineb(&rio, buf, MAXLINE)) != 0) // line 10Rio_writen(STDOUT_FILENO, buf, n); // line 11}3 of 83. [20 points] Consider the function fgets and one of its possible implementations./** This implementation of fgets() is adapted from the first edition* of ‘‘The C Programming Language’’ by Kernighan and Ritchie.*/char *fgets(char *string, int size, FILE *stream){char *cs;int c;cs = string;while (--size > 0 && (c = getc(stream)) != EOF)if ((*cs++ = c) == ’\n’)break;*cs = ’\0’;return ((c == EOF && cs == string) ? NULL : string);}Using alarm(2) and non-local jumps, write a version of the function fgets, called tfgets,that times out after 5 seconds. The function tfgets accepts the same inputs as fgets. Ifthe user doesn’t type an input line within 5 seconds, tfgets returns NULL. Otherwise, itreturns a pointer to the input line.4 of 84. [20 points] In this problem, you will examine the code generated by a C compiler for functionsthat have 代写COMP 321作业、fdopen留学生作业代做、c/c++课程设计作业代做、代写c/c++编程语言作业 帮做Javstructures as arguments and return values, and from this learn how these languagefeatures are typically implemented.The following C code has a function compute having structures as arguments and returnvalues, and a function caller that calls compute.struct str1 {long a;long b;long *p;};struct str2 {long sum;long diff_a_b;long diff_b_p;};struct str2compute(struct str1 s1){struct str2 resu

你可能感兴趣的:(讲解:COMP 321、fdopen、c/c++、c/c++Java|R)