課程名稱︰系統程式設計
課程性質︰大二必修
課程教師:施吉昇
開課學院:電機資訊學院
開課系所︰資訊工程學系
考試日期(年月日)︰2017/11/08
考試時限(分鐘):180分鐘
試題 :
1. (20 points) Multiple Choice: select the correct choice from the list and
there can be more than one correct choice.
a. (Multiple Choices, 5 points) Which of the following header files are
used to define POSIX standards?
i. <dirent.h>: directory entries
ii. <stdio.h>: standard I/O library
iii. <math.h>: mathematical function and type declaration
iv. <fcntl.h>: file control
v. <aio.h>: asynchronous I/O
b. (10 points) Please comparing the terms of each subquestion:
i. sticky bit on directory and file
ii. synchronous and asynchronous I/O
c. (Multiple Choices, 5 points) Which of the following limits are run-time
limits?
i. CHAR_MAX: max value of char
ii. ATEXIT_MAX: number of functions that can be registered with the
atexit function
iii. TIMER_MAX: maximum number of timers per process
iv. OPEN_MAX: one more than the maximum value assigned to a newly
created file descriptor
v. INT_MIN: min value of int
2. (20 points) Older versions of the UNIX system didn't support O_APPEND
option to append data to opened files. Hence, the program needed to set a
file's offset to the end of the file and then write. The following code
segment shows an example.
lseek(fd, 0L, SEEK_END);
write(fd, buf, 100);
The above code segment intends to append the stored content to the file and
works fine for a single process. However, it may not work as expected for
multiple processes when they are appending messages to the same file.
a. (3 points) Please describe atomic operation.
b. (7 points) Give a scenario to explain why the above code MAY NOT work as
expected, i.e., not always writing the content at the end of the file,
for two processes. Show the data structures for opened files, including
process open file tables and system open file table.
c. (5 points) Single UNIX Specification supports pwrite() and pread() that
allow applications to seek and perform I/O atomically. Please revise the
above code segment to achieve atomic appending without specifying
O_APPEND. If there is no way to achieve atomic appending according to
what we have learned in this course, please explain the causes.
(You may need to refer to appendix for file I/O functions.)
d. (5 points) Please describe the scenarios under which the code, shown in
main question, segment does not lead to interleaving write without
modification?
3. (25 points) Alice wants to write a server program allowing students to
submit their assignments via network connection simultaneously, Each
connection from a client sends a request to upload a file to the server.
Once the connection from a client is accepted by the server, Alice plans to
design a loop, which iteratively
- reads the data sent by the client via a new connected socket descriptor,
- writes the client's data to a file, and
- synchronizes standard C library's buffer or kernel's buffer cache if
necessary,
assuming that the file descriptor (or file stream) has been set up already.
She's got several options when developing such a program as follows:
- I/O models for networking: blocking I/O, non-blocking I/O, multiplexing
I/O
- File I/O: buffered I/O, unbuffered I/O
- Buffered I/O: different buffer sizes
- Unbuffered I/O: character-at-a-time I/O, line-at-a-time I/O
- Buffer/disk synchronization: fflush(), fsync()
- Asynchronous I/O: aio_read()/aio_write()
Please help Alice to make decisions.
a. (6 points) Please describe the design concept of non-blocking I/O and
its disadvantages.
b. (9 points) Consider I/O models for networking: blocking I/O, non-
blocking I/O, multiplexing I/O (only select() with short timeout is
considered in this question). Assuming that there is only one incoming
network connection. On server side, the server is ready to read data
from the client and may use one of these three models. Which model saves
the most in user CPU time? Which model wastes the most in user CPU time?
Which model needs larger time from starting to read at server until the
requested data is completely ready in user process? Explain your answer.
c. (10 points) Please compare the following table for performance compari-
son between different settings. Assume blocking I/O is adopted as the
network I/O model, the file to be uploaded is very large, and the pro-
blems of network traffic and system load are ignored here. The file
system used has 4K-byte blocks. In this table, the first line stands for
the baseline, which considers the combination of unbuffered I/O, 4K
buffer size, and no disk synchronization. A, B, C are time in seconds.
Please fill in the table with >X, <X, or ~X, where X is A, B, or C. They
mean the time required is much larger than, less than, and close to X,
respectively. (1 point for each row.)
┌────────────────────────┬──┬───┬───┐
│ Setting │User│System│Clock │
│ │CPU │CPU │Time │
├───────┬───────┬────────┼──┼───┼───┤
│unbuffered I/O│buf size 4KB │write() │ A │ B │ C │
├───────┼───────┼────────┼──┼───┼───┤
│unbuffered I/O│buf size 4KB │fsync() after │ │ │ │
│ │ │write() │ │ │ │
├───────┼───────┼────────┼──┼───┼───┤
│unbuffered I/O│buf size 1B │write() │ │ │ │
├───────┼───────┼────────┼──┼───┼───┤
│unbuffered I/O│buf size 8KB │write() │ │ │ │
├───────┼───────┼────────┼──┼───┼───┤
│unbuffered I/O│buf size 8KB │fsync() after │ │ │ │
│ │ │write │ │ │ │
├───────┼───────┼────────┼──┼───┼───┤
│buffered I/O │line-at-a-time│fputs() │ │ │ │
├───────┼───────┼────────┼──┼───┼───┤
│buffered I/O │line-at-a-time│fflush()+fputs()│ │ │ │
├───────┼───────┼────────┼──┼───┼───┤
│buffered I/O │line-at-a-time│fflush()+fsync()│ │ │ │
│ │ │+fputs() │ │ │ │
├───────┼───────┼────────┼──┼───┼───┤
│buffered I/O │fully buffer │fputc() │ │ │ │
├───────┼───────┼────────┼──┼───┼───┤
│buffered I/O │fully buffer │fllush()+fputc()│ │ │ │
├───────┼───────┼────────┼──┼───┼───┤
│buffered I/O │fully buffer │fflush()+fsync()│ │ │ │
│ │ │+fputc() │ │ │ │
└───────┴───────┴────────┴──┴───┴───┘
4. (20 points) Buffering in file systems.
a. (6 points) Please explain the operations conducted by fflush(FILE *fp)
and the memories being affected by this operations.
b. (6 points) Please explain the operations conducted when O_RSYNC is set
to open a file and the memories being affected by this operations.
c. (8 points) Alice writes a program to write data to a file using Standard
C library. To assure the data are written to the storage device, she
adds fflush() after fwrite() operations on the file streams. However,
she found some of the data are missing from the storage devices when
another process reads the file after this code segment. There are at
least two approaches to avoid inconsistent content:
i. open file with special flags: O_SYNC, O_DSYNC, or O_RSYNC
ii. call a function call to write updated buffer to storage devices
Please describe these two approaches, without detailed implementation,
and compare the impacts on process response times of these two
approaches.
5. (20 points) Cathy is working on a directory using the following code
segments.
/* Other code segments are ommited */
status = mkdir("foo", S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
if (status == 0) {
symlink("foo", "bar");
}
... /* other code segments are ommited */
status = chdir("bar");
if (status < 0)
perror("chdir"); exit(2);
/* Other code segments are ommited */
The working directory is /home/cathy. Please answer the following
questions:
a. (6 points) Please complete the following figure for the i-node in the
file system including filenames/directory names in dir blocks, the
pointers from i-nodes (1267, 2549, and 4582) to dir block or file data
block, and the pointers from entries in dir blocks to i-nodes. Note
that you may need to add i-nodes, dir-blocks, and data blocks in the
figure.
Figure: http://i.imgur.com/cKFfAx4.png
b. (4 points) Suppose that Cathy is only assigned to user group 'Users.'
However, the GID of directory foo is not 'Users.' Please explain the
possible issue.
c. (4 points) Assume that the size of struct DIR data structure in Cathy's
working environment is 4096 bytes. What are the size returned from
fstat() for "bar" and "foo"?
d. (6 points) When Cathy executes this program, she received the following
error message on console:
chdir: No such file or directory.
Please describe at least two possible causes for the error message.