COMP 321
April 24, 2019
Questions 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 fdopen
can 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 the
lecture notes, labs, and assignments that the instructors have provided to you or any notes that
you have taken for this class. You may also use CLEAR to develop and test your C code for this
exam. You may not consult any other sources of information, including other textbooks, web sites,
or people, aside from the instructors.

代写COMP 321作业、fdopen留学生作业代做、c/c++课程设计作业代做、代写c/c++编程语言作业
This is a 5 hour exam. You must take the exam in one continuous block of time. You may
take one break of up to one hour during the exam. Your exam period begins as soon as you look
at any page of the exam other than these instructions. Indicate in your exam submission the date
and time at which you start the exam, the time at which you start your break, the time at which
you 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 instructors
until the end of the finals period. If you find anything in the exam to be ambiguous, clearly state
all 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 that
is small in size. Always explain your answer and comment any C code you write to explain what
it does. Most of your score will be determined by your explanation. Turn in your answers through
Canvas 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 8
1. [20 points] Consider the following program.
#include "csapp.h"
void
two(void){Write(STDOUT_FILENO, "2", 1);}int
main(void)
{if (Fork() == 0)atexit(two);
if (Fork() == 0)
Write(STDOUT_FILENO, "0", 1);
else
Write(STDOUT_FILENO, "1", 1);
exit(0);}
Determine which of the following outputs are possible. Explain your answer. Note: The
atexit(3) function takes a pointer to a function and adds it to a list of functions (initially
empty) that will be called when the exit(3) function is called.
A. 001212
B. 010212
C. 022110
D. 102120
E. 112002
F. 210120
Suppose Write is replaced by printf. For example, Write(STDOUT FILENO, "2", 1); becomes
printf("2");. How would this change affect your answer?
2 of 8
2. [20 points] Modify the program cpfile.c from Figure 10.5 of the textbook so that it takes
an optional command-line argument that is a file name. If a file name is given on the command
line, then copy the contents of that file to standard output; otherwise, copy standard input
to 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 not
allowed to change any of the existing code. The program cpfile.c is shown below for your
convenience.
#include "csapp.h"
int main(int argc, char **argv)
{
int n;
rio_t rio;
char buf[MAXLINE];
Rio_readinitb(&rio, STDIN_FILENO); // line 9
while ((n = Rio_readlineb(&rio, buf, MAXLINE)) != 0) // line 10
Rio_writen(STDOUT_FILENO, buf, n); // line 11
}
3 of 8
3. [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. If
the user doesn’t type an input line within 5 seconds, tfgets returns NULL. Otherwise, it
returns a pointer to the input line.
4 of 8
4. [20 points] In this problem, you will examine the code generated by a C compiler for functions
that have structures as arguments and return values, and from this learn how these language
features are typically implemented.
The following C code has a function compute having structures as arguments and return
values, 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 str2
compute(struct str1 s1)
{
struct str2 result;
result.sum = s1.a + s1.b + *s1.p;
result.diff_a_b = s1.a - s1.b;
result.diff_b_p = s1.b - *s1.p;
return (result);
}
long
caller(long x, long y, long z)
{
struct str1 s1;
struct str2 s2;
s1.a = x;
s1.b = y;
s1.p = &z;
s2 = compute(s1);
return (s2.sum + s2.diff_a_b + s2.diff_b_p);
}
The gcc compiler on CLEAR generates the following assembly code for these two functions.
1: compute:
2: movq %rdi, %rax
5 of 8
3: movq 8(%rsp), %rsi
4: movq 16(%rsp), %rdx
5: movq 24(%rsp), %rcx
6: movq (%rcx), %rcx
7: leaq (%rdx,%rsi), %rdi
8: addq %rcx, %rdi
9: movq %rdi, (%rax)
10: subq %rdx, %rsi
11: movq %rsi, 8(%rax)
12: subq %rcx, %rdx
13: movq %rdx, 16(%rax)
14: ret
1: caller:
2: subq $104, %rsp
3: movq %rdx, 24(%rsp)
4: leaq 24(%rsp), %rax
5: movq %rdi, (%rsp)
6: movq %rsi, 8(%rsp)
7: movq %rax, 16(%rsp)
8: leaq 32(%rsp), %rdi
9: call compute
10: movq 32(%rsp), %rax
11: addq 40(%rsp), %rax
12: addq 48(%rsp), %rax
13: addq $104, %rsp
14: ret
A. We can see in lines 2-5 of the assembly code for compute that it appears as if four values
are being received from the caller, even though the function has only a single argument.
Describe what these four values are.
B. We can see in line 2 of the assembly code for caller that 104 bytes are allocated in the
stack frame. The first 56 bytes are used as seven fields of 8 bytes each. Describe how
each of these fields is used.
C. How would you describe the general strategy for passing structures as arguments to a
function?
D. How would you describe the general strategy for returning a structure as the return
value from a function?
6 of 8
5. [20 points] Chapter 8 of the textbook, “Exceptional Control Flow”, discusses Unix signals at
length, and Chapter 12, “Concurrent Programming”, introduces POSIX threads. However,
the textbook never discusses how signals should be handled in a multithreaded program.
Consider the following program fragment from a poorly written multithreaded web proxy.
...
FILE *proxy_log_file;
...
static void
SIGUSR1_handler(int sig)
{
(void)sig;
fflush(proxy_log_file);
}
...
int
main(int argc, char **argv)
{
...
proxy_log_file = Fopen("proxy.log", "a");
...
Signal(SIGUSR1, SIGUSR1_handler);
...
}
...
void
process_request(...)
{
...
fprintf(proxy_log_file, "%s\n", log_entry);
...
}
The author of this code defined the SIGUSR1 handler so that network administrators operating
this web proxy would be able to flush any buffered log entries to the log file so that other
applications could read those log entries. To trigger a flush, the network administrator simply
uses the kill command to send a SIGUSR1 signal to the web proxy.
Unfortunately, the approach to handling the SIGUSR1 signal in the above program fragment
is flawed. To understand the flaw, consider the following two facts. First, when a signal is
delivered asynchronously to a multithreaded process, the operating system may preempt an
7 of 8
arbitrary thread to execute the signal handler. (Of course, the operating system will not
preempt a thread that has blocked asynchronous delivery of the signal.) Second, although
the fflush(3), Fopen(), and fprintf(3) functions are thread-safe, they are not asyncsignal-safe.
These functions achieve thread-safety through locking the specified I/O stream.
Describe a concurrency problem that can arise from calling fflush(2) inside a
signal handler of a multithreaded.
The recommended approach to handling a signal in a multithreaded program is to block
the asynchronous delivery of that signal to all threads within the process and create a dedicated
thread that receives the signal synchronously by calling sigwait(2), sigtimedwait(2),
or sigwaitinfo(2). Asynchronous signal delivery can be blocked for an individual thread
using pthread_sigmask(3). Alternatively, a signal can be blocked for all threads using
sigprocmask(2). Modify the above program fragment to use this approach.

因为专业,所以值得信赖。如有需要,请加QQ:99515681 或邮箱:99515681@qq.com

微信:codinghelp

COMP 321的更多相关文章

  1. java:comp/env/jdbc/ 的两种配置方法

    1. 在 META-INF 下建立文件: context.xml <?xml version="1.0" encoding="UTF-8"?> &l ...

  2. 转载 从最简单的vector中sort用法到自定义比较函数comp后对结构体排序的sort算法

    转载自:http://www.cnblogs.com/cj695/p/3863142.html sort函数在使用中非常好用,也非常简单,而且效率与冒泡或者选择排序不是一个数量级.本文就sort函数在 ...

  3. 【转】 从最简单的vector中sort用法到自定义比较函数comp后对结构体排序的sort算法

    sort函数在使用中非常好用,也非常简单,而且效率与冒泡或者选择排序不是一个数量级.本文就sort函数在vector中的用法分为sort函数入门用法与自定义comp比较函数比较结构体这两个最基本的功能 ...

  4. centos7 下手动安装MySQL-5.6.32-1.linux_glibc2.5.x86_64.rpm-bundle

    由于centos7默认不再是mysql数据库,所以度算手动安装一个. 全程参考http://www.2cto.com/database/201501/371451.html 这里摘抄以下这个链接的内容 ...

  5. JNDI:对java:comp/env的研究

    这两天研究了一下 context.lookup("java:comp/env/XXX")和直接context.lookup("XXX")的区别 网上关于这两个的 ...

  6. 理解JNDI中 java:comp/env/jdbc/datasource 与 jdbc/datasource 的不同之处(转)

    在描述JNDI,例如获得数据源时,JNDI地址有两种写法,例如同是  jdbc/testDS 数据源: A:java:comp/env/jdbc/testDS B:jdbc/testDS   这两种写 ...

  7. 通过qsort(void * lineptr[], int left, int rifht, int (*comp)(void *, void *))解读指针函数和void指针

    原函数是<The C programint  language >5.11文本行排序的程序,如下: void qsort(void *v[], int left, int right, i ...

  8. JNDI中 java:comp/env 的理解

    J2EE 上下文环境变量前缀,一般有如下几种:java:/comp/env/jdbcjava:/comp/env/urljava:/comp/env/mailjava:/comp/env/jms在部署 ...

  9. 文件比较命令(comp)

    comp命令: // 描述: 逐字节比较两个文件或文件集的内容. 如果在没有参数的情况下使用,comp会提示你输入要比较的文件. // 语法: comp [<Data1>] [<Da ...

随机推荐

  1. ubuntu16.04安装mrpt

    源码地址 https://github.com/MRPT/mrpt 安装教程 https://github.com/MRPT/mrpt/blob/master/README.md#32-build-f ...

  2. yield关键字

    1.yield语句有两种形式 (1)yield return <expression>;一次返回一个元素 运行yield return 语句时,会返回一个 值,并记录当前位置及保留该值.下 ...

  3. linux svn权限

    svnserve -d -r /opt/svn                      //启动 创建仓库 svnadmin create /u02/svn/davesvn              ...

  4. Node.js 开发

    Node.js不必介绍,已经太火爆了.简单说是用Javascript开发Web服务端,基于Google V8引擎,单线程.不多说从零开始Windows平台下的Node.js的开发之旅. 环境工具为先 ...

  5. js 获取某个某个区间内的随机整数

    //获取某个某个区间内的随机整数 ,获取到的值域为[min,max)function get_random_num(min,max){ if(/^-?\d+$/.test(min) && ...

  6. JAVA RPC (四) 之thrift序列化普通对象

    先简单写一个thrift文件 本地通过thrift编译之后会生成一个java源文件.------编译口令 :thrift -gen java mytestrequest.thrift 编译后的源代码如 ...

  7. 为什么vue里面data里面的对象,无法用delete删除属性

    因为vue里面的data是用get赋值的,所以无法用delete, 这时你可以用Object.defineProperty() Object.defineProperty(basic,'photo', ...

  8. 多线程之Synchronized锁的基本介绍

    基本介绍 synchronized是Java实现同步的一种机制,它属于Java中关键字,是一种jvm级别的锁.synchronized锁的创建和释放是此关键字控制的代码的开始和结束位置,锁是有jvm控 ...

  9. 洛谷P4778 Counting swaps 数论

    正解:数论 解题报告: 传送门! 首先考虑最终的状态是固定的,所以可以知道初始状态的每个数要去哪个地方,就可以考虑给每个数$a$连一条边,指向一个数$b$,表示$a$最后要移至$b$所在的位置 显然每 ...

  10. 解决配置Windows Update失败问题

    大家都清楚电脑总是需要更新一些补丁,不过,很多系统用户发现更新了补丁之后,开机会出现windows update更新失败的情况,提示“配置Windows Update失败,还原更改,请勿关闭计算机”信 ...