作者jason1218 (zolution)
看板b04902xxx
标题Fw: [试题] 104-1 郑卜壬 系统程式设计 期末考
时间Sun Jan 8 17:33:53 2017
提醒一下 这里的Multiple Choices是多选题喔
※ [本文转录自 NTU-Exam 看板 #1MguFIcB ]
作者: kevin1ptt (蚁姨椅yee) 看板: NTU-Exam
标题: [试题] 104-1 郑卜壬 系统程式设计 期末考
时间: Fri Jan 29 23:22:53 2016
课程名称︰系统程式设计
课程性质︰资工大二上必修
课程教师︰郑卜壬 pj2
开课学院:电机资讯学院
开课系所︰资讯工程学系
考试日期(年月日)︰2016/1/13
考试时限(分钟):180
试题 :
1. (21 pts) Explanation.
(A) (3 pts) Suppose a thread is blocking the delivery of all the signals
and running in a critical section of user-level code. Can it get
context switched? Why or why not?
(B) (3 pts) Explain why communication through a pipe is limited to
processes that descend from a common ancestor?
(C) (4 pts) Draw a figure to explain how virtual memory supports the
copy-on-write technique.
(D) (4 pts) The call dup2(f_1, f_2) is equivalent to close(f_2) followed
by fcntl(f_1, F_DUPFD, f_2). Give an example to explain why dup2
should be an atomic operation for a process with single thread.
(E) (3 pts) Explain why the -f option is required in the following
interpreter file.
#!/usr/bin/awk -f
BEGIN {
printf "Hello World\n"
}
(F) (4 pts) Explain why BSS section just has a section header but
occupies no space in the ELF file; however, BSS segment actually
occupies space in virtual memory.
2. (18 pts) Multiple choices.
(A) (3 pts) Ignore error returns. Which functions return exactly once?
(a) fork (b) execve (c) dup (d) longjmp (e) waitpid
(B) (3 pts) Each pthread has its own
(a) heap (b) stack (c) errno value (d) signal mask (e) fd table
(C) (3 pts) Which items remain unchanged after invoking exec()?
(a) Penging signals
(b) Signal dispositions
(c) Saved set-user-id
(d) System/user CPU time
(e) File locks
(D) (3 pts) Which of the following statements about pthread are true?
(a) The signal disposition is shared by all threads in a process.
(b) A thread-safe function is also a reentrant function.
(c) pthread_cancel(tid) will wait for the target thread tid to
terminate.
(d) A new thread inherits a copy of its creating thread's signal
mask when pthread_create is called.
(e) Only one thread exists in a new, forked child process.
(E) (3 pts) Which guarantees are offered by the pipe facility?
(a) All writes to the pipe are atomic.
(b) More than two processes can read/write to the same pipe
simultaneously.
(c) As long as the pipe can be written to, readers will not receive
an EOF.
(d) Reading from the pipe will generate SIGPIPE if there is no writer.
(e) Only parent and child processes can communicate via pipes.
(F) (3 pts) Which of the following statements about user-level threads
are true?
(a) Thread switching does not require kernel privileges.
(b) The cost of creating and destroying threads is substantial,
compared to kernel-level threads.
(c) When a thread is blocked on a system call, the entire process is
blocked.
(d) A process can speed up its execution by making use of more than
one CPU.
(e) Scheduling can be application specific.
3. (29 pts) Alice writes a UNIX program that creates one producer thread and
two consumer threads. Producer generates data and puts it into a shared
buffer; consumers remove the data from the buffer. The following is the
code segment, where error returns and buffer management are ignored.
#define MAX_BUFFER_SIZE 100
#define block_signal( sig ) \
int ca; \
sigset_t sigs_to_block, sigs_to_catch; \
sigemptyset( &sigs_to_block ); \
sigemptyset( &sigs_to_catch ); \
sigaddset( &sigs_to_block, sig ); \
sigaddset( &sigs_to_catch, sig ); \
pthread_sigmask( SIG_BLOCK, &sigs_to_block, NULL );
cond_t cv = PTHREAD_COND_INITIALIZER;
mutex_t m = PTHREAD_MUTEX_INITIALIZER;
int count; // number of data in buffer; shared variable
int main() {
pthread_t tid_producer, tid_consumer1, tid_consumer2;
pthread_create( &tid_producer, NULL, producer, NULL );
pthread_create( &tid_consumer1, NULL, consumer, NULL );
pthread_create( &tid_consumer2, NULL, consumer, NULL );
// Region A
}
void *producer( void *p ) {
block_signal( SIGUSR1 );
while (1) {
sigwait( &sigs_to_catch, &ca );
pthread_mutex_lock( &m );
if ( count == MAX_BUFFER_SIZE )
pthread_cond_wait( &cv, &m );
// put data in buffer here
count++;
pthread_cond_signal( &cv );
pthread_mutex_unlock( &m );
}
return NULL;
}
void *consumer ( void *p ) {
block_signal( SIGUSR2 );
while (1) {
sigwait( &sigs_to_catch, &ca );
pthread_mutex_lock( &m );
if ( count == 0 )
pthread_cond_wait( &cv, &m );
// remove data from buffer here
count--;
pthread_cond_signal( &cv );
pthread_mutex_unlock( &m );
}
return NULL;
}
(A) (4 pts) What functions should be invoked in Region A to guarantee
all of the producer and consumer threads get a chance to execute?
Note that there are two possibilities.
(B) (5 pts) Describe what the producer thread (or the consumer thread)
does in detail.
(C) (2 pts) Which system call will initialize the value of the global
variable count to zero when Alice runs the program?
(D) (6 pts) Even though both of the functions producer and consumer
block and then wait for the signals, SIGUSR1 and SIGUSR2 might get
lost. In other words, the threads may never know about the
generation of SIGUSR1 and SIGUSR2. Why? Please describe how to fix it.
(E) (6 pts) When running the program, Alice finds sometimes the value of
count is changed to -1 (negative). Why? Describe how to fix the
problem.
(F) (6 pts) When running the program, Alice finds sometimes all three
threads are left sleeping. Why? Describe how to fix the problem.
4. (28 pts) When writing a program Score to automatically test students'
assignments, TA Alice plans to fork a child process to execute a
student's program (which takes no argument), and then block the parent
until the child terminates. In the following code segment of Score,
Alice puts the parent to wait for a specified amount of time in case
the student's program gets into an infinite loop. The wait() function
will suspend execution until the child terminates or until delivery
of the signal SIGALRM. Assume the program runs without errors. Please
answer the following questions.
#define MAX_TIMEOUT 30
static void sig_alarm( int signo ) { // nothing to do }
int main( int argc, char *argv[] ) {
pid_t pid;
int status;
// Region A
signal( SIGALRM, sig_alarm );
pid = fork();
if ( pid == 0 ) { // execute a student's program
// Region B
execl( argv[1], argv[1], 0 );
exit( 0 );
}
alarm( MAX_TIMEOUT );
if ( wait( &status ) < 0 ) {
if ( errno == EINTR ) fprintf( stderr, "timeout\n" );
}
// Region C
return 0;
}
(A) (3 pts) There is a race condition between the call to the function
alarm() and the call to the function wait(). Why does such race
condition occur?
(B) (6 pts) How do you correct the race condition problem in the program?
You do not need to write complete code. But you should explain which
system calls are needed, where they should be used, and how the
proposed changes correct the race condition problem. Note: You're
not allowed to terminate the process in sig_alarm().
Alice wants the students' programs to read data from the input file
through their standard inputs. Assume the input file is owned by Alice
and can only be read by owner. The Score file has its set-user-ID bit set
so that each student can run it to test his/her program. After the
termination of a child process, Alice hopes to write its status into the
output file whose owner is Alice. Only Alice can write the output file.
To achieve the goal, Alice adds some code in Regions A and C:
Region A: int fd;
fd = open( "input_file", O_RDONLY );
Region C: close( fd );
fd = open( "output_file", O_WRONLY );
// write to outupt_file here
close( fd );
(C) (3 pts) Please add some code in Region B to allow the student's
program to read the input file from its standard input. Close
unused file descriptors.
(D) (4 pts) If the student's program's set-user-ID bit is set, can the
student write to the output file by running Score? Why? Explain
the reasons based on real and effective user IDs.
(E) (4 pts) If the student's program's set-user-ID bit is not set, can
the student write to the output file by running Score? Why? Explain
the reasons based on real and effective user IDs.
(F) (8 pts) Help Alice fix the security problem by applying the least
privilege model. Modify the code (you can add your own variables)
and then explain why your solution is correct. Show how the real
and effective usre IDs and saved set-user-ID change. Note that Alice
has no superuser privilege.
5. (13 pts) A reentrant function can be temporarily interrupted by a signal
handler and safely called again in the handler before its previous
execution completes.
(A) (3 pts) Explain why malloc() is not a reentrant function.
(B) (4 pts) Please modify the function sum_it() to make it reentrant.
feel free to change its prototype or add variables.
int sum_it( int num )
{
static int sum = 0;
sum += num;
return sum;
}
(C) (6 pts) Consider the following function sum_it_mutex(). Explain
(1) why the function is thread-safe and (2) why the function is not
async-signal safe.
int sum = 0;
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
int sum_it_mutex( int num )
{
int val;
pthread_mutex_lock( &mutex );
val = sum + num;
pthread_mutex_unlock( &mutex );
return val;
}
6. (21 pts) What are the memory segments storing the variables?
Suppose dynamic linking is adopted by the compiler. Please complete
the form.
int a = 100;
char b[100];
int main( int argc, char *argv[] )
{
char *f;
f = malloc( 100 );
func( 100 );
}
void func( int e )
{
static int c;
int d = 100;
printf( "Hello World\n" );
}
memory address memory segments
&a
b
&argc
&c
&d
&e
&f
f
main
printf (3 pts)
p.s. 原本最後一页下面有附上一些function prototypes,此略。
--
※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 58.114.191.200
※ 文章网址: https://webptt.com/cn.aspx?n=bbs/NTU-Exam/M.1454080978.A.98B.html
1F:推 rod24574575 : 已收资讯系! 01/30 00:36
2F:推 nick02468 : 推推 01/30 12:52
3F:推 jason1218 : 勇者<(_ _)> 01/31 16:23
4F:推 silentazure : 勇者推 02/01 00:55
※ 发信站: 批踢踢实业坊(ptt.cc)
※ 转录者: jason1218 (140.112.16.185), 01/08/2017 17:33:53
※ 编辑: jason1218 (140.112.16.185), 01/08/2017 17:34:22
5F:推 peter0722: 推推 01/08 17:48