作者xavier13540 (柊 四千)
看板NTU-Exam
標題[試題] 105上 鄭卜壬 系統程式設計 期中考
時間Wed Nov 23 13:16:03 2016
課程名稱︰系統程式設計
課程性質︰資訊工程學系大二必修
課程教師︰鄭卜壬
開課學院:電機資訊學院
開課系所︰資訊工程學系
考試日期(年月日)︰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://www.ptt.cc/bbs/NTU-Exam/M.1479878169.A.BA5.html
→ 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
→ rod24574575 : 已收資訊系精華區! 11/25 14:50
推 twsmcc : 背得出來 跪惹 11/25 16:02
噓 hortune : 教授都把考卷收回了 還po? 11/30 11:29
※ dyadi:轉錄至看板 b05902xxx 11/08 16:40