NTU-Exam 板


LINE

课程名称︰系统程式设计 课程性质︰资讯工程学系大二必修 课程教师︰郑卜壬 开课学院:电机资讯学院 开课系所︰资讯工程学系 考试日期(年月日)︰2016/11/16 考试时限(分钟):180 试题 : 1. (17 pts) Explanation (as clearly as possible for full credits). (a) (4 pts) System calls allow processes to trap into the kernel. Give two examples of system calls to explain under what circumstances one would trigger a context switch and the other would not. (b) (3 pts) Explain under what circumstances advisory lock is safe even though there are other processes probably violating a lock and trying to access the locked file. (c) (3 pts) Explain why communication through a pipe should be limited to processes that descend from a common ancestor. (d) (3 pts) Exaplain under what circumstances a UNIX-like system would automatically turn off the set-group-ID bit of a newly created file. (e) (4 pts) Explain why it's common for a process to create and open a new file and then immediately unlink it; however, it's not very useful for a process to create and open a directory and then immediately remove it. 2. (20 pts) The cp utility copies the content of a source file to a target file. When cp implemented with unbuffered I/O system calls, the following four factors (A)~(D) would significantly affect its execution time. Please answer the questions. (A) The number of the while loops -- Each loop copies partial file content with read() or write() (B) The time to wait for data ready in memory (C) The time to copy data from kernel's buffer cache to user's buffer and vice versa -- User's buffer denotes the buffer specified in read() and write() (D) The time to move data from kernel's buffer cache to disk and vice versa (a) (2 pts) What factor(s) will significantly affect user CPU time? (b) (2 pts) What factor(s) will significantly affect system CPU time? (c) (2 pts) What factor(s) will significantly affect clock/response time? (d) (2 pts) What factor(s) will be significantly affected when nonblocking I/O is taken into account, compared with blocking I/O? (e) (2 pts) What time (user CPU time, system CPU time, and clock/response time) will be significantly affected by system call fsync()? (f) (6 pts) Suppose the target file is redirected to null device /dev/null. We run the cp utility with different buffer sizes in blocking mode and get the following results. ──────────────────────────────────── Buffer size User CPU (sec) System CPU (sec) Clock Time (sec) ──────────────────────────────────── 1 124.89 161.65 288.64 2 63.10 80.96 145.81 32 4.13 5.01 9.76 128 1.01 1.27 6.82 4096 0.03 0.16 6.86 8192 0.01 0.18 6.67 ──────────────────────────────────── (f.1) Increasing the buffer size beyond 4096 has little positive effect on system CPU time. Why? (f.2) When the buffer size is small, the difference between clock time and total CPU time (user CPU + system CPU) is also small, e.g., the sum of 124.89 and 161.65 is close to 288.64. But such difference increases significantly when the buffer size comes to 128. Why? (g) (4 pts) Suppose the time to read the source file is ignored. We run the cp utility with different sizes of source files in blocking mode and get the following results. ──────────────────────────────────── Source size User CPU (sec) System CPU (sec) Clock Time (sec) ──────────────────────────────────── 10M 0.000 0.008 0.010 300M 0.000 0.236 0.239 1000M 0.000 0.824 0.827 ──────────────────────────────────── Disk I/O is assumed to be time comsuming; however, clock time is close to system CPU time for different sizes. Why? 3. (16 pts) Alice and Bob propose a method to share files securely. Each of them creates two directories OutBox and InBox in her/his home directory. OutBox is the place to store the files to be shared; InBox is the place to store the links pointing to the shared files. If Alice wants to share her file hw with Bob, she can just put the file in ~Alice/OutBox and then create a link pointing to ~/Alice/OutBox/hw in ~Bob/InBox. For security issue, no one is able to traverse both of OutBox and InBox except the owner. Only the owner can remove his/her own files from OutBox. Assume Alice and Bob belong to different groups and they both have no superuser privileges. Please answer the following questions. (a) (2 pts) What is the advantage of creating a link in InBox over copying a file to InBox? (b) (3 pts) Alice can put a symbolic link or a hard link in Bob's InBox. Which choice is better? Explain your answer. (c) (6 pts) What minimum access rights (i.e., read, write and execute) should be used for the following directories? Only consider the others class. Your answer should be in the form of "rwx", "r-x", or the like. (1) Directory InBox (2) Directory OutBox if hard links are adopted (3) Directory OutBox if symbolic links are adopted (d) (2 pts) If a file put in Bob's InBox is owned by Alice and its permission is "r-- --- ---," can Bob remove the file from his InBox directory? Why? (e) (3 pts) One major weakness of the method is that: (1) anyone may create files in ~Bob/InBox, and (2) the owner of the link put in ~Bob/InBox may not be Bob, who feels uncomfortable with this. Please develop a set-user- id program to help them solve these problems. No code is required here. Just describe how your method works in detail. 4. (10 pts) Alice plans to develop a function, which opens a unique temporary file in read/write mode and then returns its file descriptor. In her program, as shown below, tmpnam() is invoked to obtain a pointer pointing to a valid filename that doesn't exist at the time it is called. Error checking is ignored. int temp_file() { char *fnptr; int fd; fnptr = tmpnam (NULL); fd = open (fnptr, O_CREAT | O_TRUNC | O_RDWR, 0600); return fd; } (a) (3 pts) Specify race condition. (b) (3 pts) Give an example to explain why the code leads to a race condition. (c) (4 pts) Propose a method to fix the problem without changing the way to call tmpnam() and open() in the code. That is, you cannot replace the arguments of the two functions with others. What system calls will be called in your method? Describe how your method works in detail. 5. (19 pts) Double fork is a common solution to the problem: the parent doesn't need to wait for the child to complete and the child doesn't become a zombie. (a) (4 pts) Give tow reasons to explain why UNIX-like systems need zombie processes. (b) (3 pts) How does the copy-on-write technique improve the efficiency of fork()? (c) (12 pts) Please implement function double_fork() with the prototype: pid_t double_fork() Your code should follow the rules: (1) Call both fork() and vfork(). Each is invoked exactly once. Do not call fork() twice. (2) Return 0 in the child. Return the process ID of the grandchild in the parent. (3) Use a pipe and unbuffered I/O in blocking mode to avoid a race condition. (4) Cannot produce any zombie processes. (5) Close all of the unused file descriptors. The errors returned from system calls can be ignored. 6. (18 pts) Alice and Bob are writing two programs to play chess in UNIX environment. In this game, their programs must alternately move one piece at a time until one is checkmated. Assume two functions, thinking() and making_ a_move(), are provided already. The function thinking() is to evaluate how good further moves would be; making_a_move() is just to move one piece. After calling making_a_move(), one should inform the other "it's your turn now." One simple solution to process synchronization is using pipe, as shown below, where process Alice calls write() to send message 'P' to Bob through standard output. Bob will be woken up by reading the message from standard input and make the next move. void main(void) { // Program Alice char buf; while (1) { thinking(); making_a_move(); buf='P'; write(STDOUT_FILENO, &buf, 1); read(STDIN_FILENO, &buf, 1); } } void main(void) { // Program Bob char buf; while (1) { read(STDIN_FILENO, &buf, 1); thinking(); making_a_move(); buf='P'; write(STDOUT_FILENO, &buf, 1); } } Please use pipe(), fork(), dup2(), execv(), and wait() to write a program, which forks two processes that can talk to each other with pipes. One process execute program Alice; the other executes program Bob. Your code doesn't need completeness and can ignore error returns. Unused file descriptors should be closed. Zombie processes are not allowed. // 题目後面有附上几个函式的prototype 这边略去 -- 第01话 似乎在课堂上听过的样子 第02话 那真是太令人绝望了 第03话 已经没什麽好期望了 第04话 被当、21都是存在的 第05话 怎麽可能会all pass 第06话 这考卷绝对有问题啊 第07话 你能面对真正的分数吗 第08话 我,真是个笨蛋 第09话 这样成绩,教授绝不会让我过的 第10话 再也不依靠考古题 第11话 最後留下的补考 第12话 我最爱的学分 --



※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 140.112.248.40
※ 文章网址: https://webptt.com/cn.aspx?n=bbs/NTU-Exam/M.1479878169.A.BA5.html
1F:→ xavier13540 : 有事先下 晚点再继续打@@ 11/23 13:17
※ 编辑: xavier13540 (140.112.248.40), 11/23/2016 16:43:34 ※ 编辑: xavier13540 (140.112.248.40), 11/24/2016 00:35:37 ※ 编辑: xavier13540 (140.112.248.40), 11/24/2016 00:38:46 ※ 编辑: xavier13540 (140.112.248.40), 11/24/2016 00:40:38 ※ 编辑: xavier13540 (140.112.248.40), 11/24/2016 00:43:41
2F:→ rod24574575 : 已收资讯系! 11/25 14:50
3F:推 twsmcc : 背得出来 跪惹 11/25 16:02
4F:嘘 hortune : 教授都把考卷收回了 还po? 11/30 11:29
dyadi:转录至看板 b05902xxx 11/08 16:40







like.gif 您可能会有兴趣的文章
icon.png[问题/行为] 猫晚上进房间会不会有憋尿问题
icon.pngRe: [闲聊] 选了错误的女孩成为魔法少女 XDDDDDDDDDD
icon.png[正妹] 瑞典 一张
icon.png[心得] EMS高领长版毛衣.墨小楼MC1002
icon.png[分享] 丹龙隔热纸GE55+33+22
icon.png[问题] 清洗洗衣机
icon.png[寻物] 窗台下的空间
icon.png[闲聊] 双极の女神1 木魔爵
icon.png[售车] 新竹 1997 march 1297cc 白色 四门
icon.png[讨论] 能从照片感受到摄影者心情吗
icon.png[狂贺] 贺贺贺贺 贺!岛村卯月!总选举NO.1
icon.png[难过] 羡慕白皮肤的女生
icon.png阅读文章
icon.png[黑特]
icon.png[问题] SBK S1安装於安全帽位置
icon.png[分享] 旧woo100绝版开箱!!
icon.pngRe: [无言] 关於小包卫生纸
icon.png[开箱] E5-2683V3 RX480Strix 快睿C1 简单测试
icon.png[心得] 苍の海贼龙 地狱 执行者16PT
icon.png[售车] 1999年Virage iO 1.8EXi
icon.png[心得] 挑战33 LV10 狮子座pt solo
icon.png[闲聊] 手把手教你不被桶之新手主购教学
icon.png[分享] Civic Type R 量产版官方照无预警流出
icon.png[售车] Golf 4 2.0 银色 自排
icon.png[出售] Graco提篮汽座(有底座)2000元诚可议
icon.png[问题] 请问补牙材质掉了还能再补吗?(台中半年内
icon.png[问题] 44th 单曲 生写竟然都给重复的啊啊!
icon.png[心得] 华南红卡/icash 核卡
icon.png[问题] 拔牙矫正这样正常吗
icon.png[赠送] 老莫高业 初业 102年版
icon.png[情报] 三大行动支付 本季掀战火
icon.png[宝宝] 博客来Amos水蜡笔5/1特价五折
icon.pngRe: [心得] 新鲜人一些面试分享
icon.png[心得] 苍の海贼龙 地狱 麒麟25PT
icon.pngRe: [闲聊] (君の名は。雷慎入) 君名二创漫画翻译
icon.pngRe: [闲聊] OGN中场影片:失踪人口局 (英文字幕)
icon.png[问题] 台湾大哥大4G讯号差
icon.png[出售] [全国]全新千寻侘草LED灯, 水草

请输入看板名称,例如:WOW站内搜寻

TOP