# midterm 2 Chp 3~5 ###### tags: `EXAM` ## 107. ### 1. Cooperating processes require an interprocess communication mechanism that willallow them to exchange data and information. Choose the correct statement(s). (A)~~Shared memory~~ <font color="#ff0000">message passing</font> is useful for exchanging smaller amounts of data, because noconflicts need be avoided. (B)Shared memory is ~~also easier~~ <font color="#ff0000">not easy</font> to implement in a distributed system thanmessage-passing. **(C )** Shared memory can be faster than message passing, since message-passingsystems are typically implemented using system calls. (D)In ~~message-passing systems~~ <font color="#ff0000">Shared memory system</font>, system calls are required only to establish sharedmemory regions. **(E)** In message-passing systems and under direct communication, each processthat wants to communicate must explicitly name the recipient or sender of thecommunication. ### 2. A thread is a basic unit of CPU utilization. Which of the following descriptionsare correct? (A) A thread is comprised a thread ID, a program counter, a register set, and a ~~heap~~ <font color="#ff0000">stack</font>. **(B)** Process creation is time consuming and resource intensive, however. It isgenerally more efficient to use one process that contains multiple threads. **(C )** In Multithreaded server architecture, when a request is made, the servercreates a new thread to service the request and resume listening for additionalrequests. **(D)** A single-threaded process can run on only one processor, regardless howmany are available. ### 3. There are some differences between user-level threads and kernel-level threads.Choose the correct statement. **(A)** User-level threads are easier and faster to create than kernel-level threads.They can also be more easily managed. (B)~~User-level~~ <font color="#ff0000">Kernel-level </font>thread is directly managed by operating system. (C )Both user-level threads ~~and kernel-level threads~~ are fast while doing contexts witching. (D)In ~~kernel-level~~ <font color="#ff0000">user-level</font> threads, whole process will be blocked if one of threads calls blocking system call. **(E)** In user-level threads, it has better utilization on multiprocessors. ### 4. Which of the following descriptions regarding the multithread models are correct? **(A)** In Many-to-One model, very few systems continue to use the model becauseof its inability to take advantage of multiple processing cores. (B)The Many-to-One model maps many ~~kernel-level~~ <font color="#ff0000">user-level </font>threads to one ~~user-level~~ <font color="#ff0000">kernel-level </font>thread. (C )In ~~One-to-One~~ <font color="#ff0000">many-to-one </font>model, only one thread can access the kernel at a time onmultiprocessor systems **(D)** In Many-to-One model, multiple threads are unable to run in parallel onmulticore systems. (E)In ~~One-to-One~~ <font color="#ff0000">Many-to-One model or Many-to-Many </font> model, developers can create as many user threads as necessary **(F)** In One-to-One model, because the overhead of creating kernel threads canburden the performance of an application, thus, it is necessary restrict thenumber of threads supported by the system. (G)The Many-to-Many model multiplexes many user-level threads to many kernel threads. The number of user-level threads and kernel threads can be anynumber. (H) The two-level model is the combination of ~~One-to-Many~~ <font color="#ff0000">one-to-one</font> model and Many-to-Many model **(I)** Many systems implementing either the many-to-many or the two-level modelplace an intermediate data structure between the user and kernel threads. Thisdata structure—typically known as a lightweight process(LWP). (J) If a kernel thread blocks, the LWP blocks as well. But, the user-level threadattached to the LWP ~~won’t~~ <font color="#ff0000">also</font> block. (K) In a quad-core system, if a process has only four LWPs, then the fifth request occurred. ~~It is possible to create another new LWP.~~ <font color="#ff0000">fifth request must wait for one of the LWPs</font> ### 5. Please describe the definition of concurrency and parallelism respectively. Is it possible to have concurrency but not parallelism? And Explain why. concurrency: 允許不同的工作可以一起有進展,不用一個工作做完才做下一個 parallelism: 同一時間可以執行兩件工作。 在只有一顆 CPU 的情況下,透過快速在工作間切換可以達到並行,但是並 沒有平行。 ### 6. Does multithreaded programming have to run on multicore/multiprocessor systems? Why or why not? Not really. Threads from one program can run **concurrently** on a single-core processor ### 8. Which is the correct output of the following code? ``` c #include <pthread.h> #include <stdio.h> #include <stdlib.h> int value = 0; void *runner(void *param); int main(int argc, const char *argv[]) { pid_t pid; pthread_t tid; pthread_attr_t attr; for(int i=0;i<2;i++){ pid = fork(); if(pid == 0){ pthread_attr_init(&attr); pthread_create(&tid, &attr, runner, NULL); pthread_join(tid, NULL); printf("CHILD: value = %d\n", value); } else if(pid > 0){ wait(NULL); printf("PARENT: value = %d\n", value); } return 0 } void *runner(void *param){ value += 5; pthread_exit(0); } ``` Ans. CHILD: value = 5 CHILD: value = 10 PARENT: value = 5 PARENT: value = 0 CHILD: value = 5 PARENT: value = 0 >[color=#0000FF] >解題關鍵: >fork() 可能會有以下三種回傳值: >-1 : 發生錯誤 >0 : 代表為子程序 >大於 0 : 代表為父程序, 其回傳值為子程序的 ProcessID >[name=Conan] >[color=#FF00FF]解題關鍵2: >fork的時候是連變數都一起copy一份的 >[name=Neko] >[color=#000000]解題關鍵3: >wait(NULL) 是當任何一個child 回傳之後才會往下做 >[name=peter] ### 9. Please insert acquire lock and release lock into the functions producer() and consumer() to protect critical sections with the best performance. ``` c void producer(){ while(1){ while(counter == BUFFER_SIZE); buffer[in] = next_produced; in = (in + 1) % BUFFER_SIZE; acquire();//add here counter++; release();//add here } } void consummer(){ while(1){ while(counter == 0) ; next_consumed = buffer[out]; out = (out + 1) % BUFFER_SIZE; acquire();//add here counter--; release();//add here } } ``` ## 106. multithreaded process share: Code section 、 global varibles 、 heap memory ### 4. What are the differences between user-level threads and kernel-level threads? (at least 2) | User-level thread | Kernel-level thread | -------- | -------- | | managed by **thread library** in user space. | managed by **kernel** . | | **Faster** context switching | **Slower** context switching | | Whole process will be blocked if one of threads calls blocking system call. | Whole process will **not** be blocked if one of threads calls blocking system call. | | Better utilization on multiprocessors | No advantage on multiprocessors. | ### 5. Which of the following descriptions regarding the multithread models are correct? If incorrect, why? (15%) (A) In ~~One-to-One~~ <font color="#ff0000">many-to-one </font> model, only one thread can access the kernel at a time on multiprocessor systems. (B) In ~~One-to-One model~~ <font color="#ff0000">Many-to-One model or Many-to-Many </font>, developers can create as many user threads as necessary. **(C )** In Many-to-One model, the entire process will block if a thread makes a blocking system call. **(D)** In Many-to-Many model, the number of kernel threads can be specific to either a particular application or a particular machine. **(E)** The two-level model is the combination of One-to-One model and Many-to-Many model.