[试题] 104-1 郑卜壬 系统程式设计 期末考

楼主: kevin1ptt (蚁姨椅yee)   2016-01-29 23:22:53
课程名称︰系统程式设计
课程性质︰资工大二上必修
课程教师︰郑卜壬 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

Links booklink

Contact Us: admin [ a t ] ucptt.com