Fw: [試題] 104-1 鄭卜壬 系統程式設計 期末考

看板b05902xxx作者 (′‧ω‧‵)時間6年前 (2018/01/04 22:18), 編輯推噓2(200)
留言2則, 2人參與, 6年前最新討論串1/1
※ [本文轉錄自 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://www.ptt.cc/bbs/NTU-Exam/M.1454080978.A.98B.html

01/30 00:36, 6年前 , 1F
已收資訊系精華區!
01/30 00:36, 1F

01/30 12:52, 6年前 , 2F
推推
01/30 12:52, 2F

01/31 16:23, 6年前 , 3F
勇者<(_ _)>
01/31 16:23, 3F

02/01 00:55, 6年前 , 4F
勇者推
02/01 00:55, 4F
jason1218:轉錄至看板 b04902xxx 01/08 17:33 ※ 發信站: 批踢踢實業坊(ptt.cc) ※ 轉錄者: dyadi (118.169.35.48), 01/04/2018 22:18:11

01/05 06:05, 6年前 , 5F
實用推
01/05 06:05, 5F

01/08 18:37, 6年前 , 6F
樓上明明是單班的
01/08 18:37, 6F
文章代碼(AID): #1QJZUacb (b05902xxx)