Linköpings Tekniska Högskola Department of Computer and Information Sciences (IDA) Klas Arvidsson Tentamen / Exam TDDI04 / TDDI12 Processprogrammering, operativsystem och realtidssystem Concurrent programming, operating systems and real-time operating systems Many example questions Jour: Klas Arvidsson Hjälpmedel / Admitted material: § § Engelsk ordbok / Dictionary from English to your native language Miniräknare / Pocket calculator General instructions § § § § § § § § § § § § This exam has 10 assignments and 7 pages, including this one. Read all assignments carefully and completely before you begin. It is recommended that you use a new sheet for each assignment. Number all your sheets, and mark each sheet on top with your name, person-number, and the course code. You may answer in either English or Swedish. Write clearly. Unreadable text will be ignored. Be precise in your statements. Imprecise formulations may lead to a reduction of points. Motivate clearly all assumptions, statements and reasoning. Explain calculations and solution procedures. The assignments are not ordered according to difficulty. The assignment named Real-Time Operating Systems should be solved by TDDI04 and TDDI12 students only, TDDB63 students solve instead the assignment named For TDDB63 only. The example is designed for 110 points (more than a real exam). Grading: U, 3, 4, 5. The preliminary threshold for grades are 50%, 67%, 84%. Student in close proximity of a higher/lower grade may be raised or lowered based on overall quality of answers. Good Luck! 1 (7) You are wise to point out unclear parts among the example questions here as this will lead to more clear and precise questions on the real exam. 1. (10p) Processes and threads a) Inside an operating system code can execute in four different contexts. As a kernel thread, as a user thread, as process, or in interrupt context. Clearly explain each concept and the differences and relationships between them. (7p) b) Explain the benefits of a thread pool. (3p) 2. (18p) Synchronization A single-threaded image manipulation program is to be modified to utilize the full potential of multiple CPU-cores. This mean distributing heavy computations over several threads. The OS takes care of distributing the threads over the available processors. A junior member of the team get the task to thread an automatic colour to black and white conversion. He produce the code shown on last page. You are not overly impressed and immediately spot some problems. a) Show a sequence of concurrent operations that produce errors. (4p) b) Explain the drawback of the while loops from efficiency point of view. With a brief motivation, state which loops are most inefficient. (3p) c) Correct the code so it will work correct and efficient. (7p) You may assume the following synchronization functions are available: lock_init(struct lock*) lock_aquire(struct lock*) lock_release(struct lock*) sema_init(struct sema*, int start) sema_up(struct sema*) sema_down(struct sema*) cond_init(struct cond*) cond_wait(struct cond*, struct lock*) cond_signal(struct cond*, struct lock*) // wake one cond_broadcast(struct cond*, struct lock*) // wake all d) On multiprocessors the overhead of synchronization by disabling interrupt may be greater than the overhead of a busy loop. Assuming the atomic swap operation is available, show how it can be applied to protect a critical section. (4p) 2 (7) 3. (16p) Memory management Shown are the contents of a certain process page table of a 6-bit logic/virtual address space. Index from bottom to top. Next is the memory contents of the physical memory in that system. Each page/frame is 8 bytes. The addresses goes in the same order as illustrate by the content, from bottom to top. And furthest to the right some content of OS memory management disk structures. Pagetable 4 60 545 3213 06 7 Physical memory YZ:) 0x38 UVWX QRST 0x30 MNOP IJKL 0x28 EFGH ABCD 0x20 6789 2345 0x18 yz01 uvwx 0x10 qrst mnop 0x08 ijkl efgh 0x00 abcd Disk (xx) (oo) page 2 page 5 :);) :P:D a) Look at the figures and information above and explain how one address binary written as 010110 (hex 16, octal 26, decimal 22), are separated in page number and offset? (How many bits on each? What is the page and offset?) (2p) b) Using the pagetable, memory and disk above, explain step by step the actions taken to look up the addresses composed of (page, offset): (1, 2), (2, 2) and (3, 2). What is the final result of reading each address? Some steps are the same for the three addresses. Only unique and important step give points. (10p) c) Look at the figure to the right. It shows the layout of two processes in memory. A third process needs a total memory corresponding to two of the free blocks in the picture. Explain how it will be put in memory when using: 1. Continuous allocation 2. Paging If there are any problems, explain and suggest solutions. (4p) 3 (7) P1 P2 4. (12p) CPU scheduling a) A single-CPU system have the following set of processes with arrival times (in milliseconds), expected maximum execution time (ms), and priority (1 is highest, 6 is lowest priority). Process Arrival time Execution time Priority (as applicable) A 0 6 5 B 1 2 3 C 5 5 1 D 7 3 4 E 8 1 2 F 14 1 6 For each of the following scheduling algorithms, create a Gantt chart (time bar diagram, starting at t = 0) that shows when the processes will execute on the CPU. Where applicable, the time quantum will be 3 ms. If several events occur at the same instance in time, assume arrival happens first. The example below shows a good structured way to show the solutions. (i) FIFO (1p) (ii) Round-robin (3p) (iii) Shortest Job First without pre-emption (3p) (iv) Priority Scheduling with pre-emption (3p) b) The schedule example below uses the same set of processes as above. Which algorithm was used to schedule them? (2p) Time Arrival A6 0 B2 Queue A6 B2 A C5 6 D3 E1 C5 B2 C5 B2 B2 D3 E1 B2 D3 C 11 E1 B2 D3 B2 D3 E B F1 14 This is an example of a suitable way to draw the scheduling charts in the second question. The letters indicate the process and the number the execution time remaining for that process. For example B2 means process B have 2 time units left. Any comments, explanations, or assumptions in any step are easily added on the line to the right. The bold indicate the process selected for execution process at that moment. D3 F1 D F1 18 F 4 (7) 5. (12p) Deadlocks a) There are four necessary conditions for deadlocks to occur. For each of them: 1. Name and briefly describe it. (4 x 1p) 2. Does bankers algorithm break the condition (yes/no with brief motivation). (4 x 1p) b) You are working on a system with a fixed number of resources used by several dynamically entering processes. Synchronization is done only with locks. Each lock has a unique number in the system and a compile time tool help you verify that a lock only can be taken if no lock with lower number is held. Only nesting locks with numbers in decreasing order are allowed (highest number acquired first). Similarly, releasing nested locks must be done in increasing order (lowest number released first). Now your boss heard that nested locks might lead to deadlock, which he dreads. He tells you to employ banker’s algorithm that he heard solve the problem. In this situation, explain to your boss the two strongest reasons either why you would agree with him or not (is bankers a good solution or not). (4p) 6. (5p) Disk I/O a) Describe how the SCAN algorithm work and why it is used. (3p) b) What improvement do C-SCAN offer over SCAN? (2p) 7. (7p) Protection and security a) With access to a list of password hashes it is possible to run an efficient dictionary attack by comparing each entry to each word in a pre-hashed dictionary. Describe how salting makes similar attacks impossible or at least considerably slower. (Still assuming access to the list of password hashes.) (3p) b) Explain any dangers with the following code: struct msg receive() { byte buffer[1024]; int i = 0; do { buffer[i] = recv(); } while (buffer[i++] != ‘\n’); return do_msg(buffer); } (Hint: Think of protection and security.) (4p) 5 (7) 8. (10p) File systems a) Describe the steps involved in, and datastructures used, when opening a file. You need to include the three most important steps and the structure used at each step. Mention the two most important data of each structure. (7p) b) Describe indexed allocation with its benefits and drawbacks. (3p) 9. (10p) Real-time operating systems Process Computation time per instance Period (= relative deadline) P1 10 40 P2 25 100 P3 24 50 The above set of periodic real-time tasks executing on a single processor are given. Assume that context switches have zero overhead. All relative release times are zero, i.e., the first instances of P1, P2 and P3 are released for execution at time 0. a) Give a simple argument (calculation) to show that the task set is schedulable when using the Earliest-Deadline First (EDF) algorithm. (2p) b) Is the task set schedulable according to the sufficient test with the rate-monotonic (RMA) scheduling approach? [Hint: 3*(21/3-1)=0.78] (2p) c) Show if it is RMA schedulable. (4p) d) What is the largest computation time P2 may have for the task set to become/remain schedulable? (2p) 10. (10p) General questions a) Explain the terms internal and external fragmentation. Use an example or picture. (4p) b) Briefly describe three different areas/solutions where starvation can occur. (3p) c) Clearly explain the term pre-emption. (3p) Good Luck! 6 (7) // communication structure struct signal { int total_sum; int average; bool average_available; int threads_done; } // help function to sum pixels on image part int pixel_sum(int img[], int start, int stop) { int sum = 0; for (int i = start; i < stop; ++i) sum += img[i]; return sum; } // help function to replace pixels on part of image void set_black_or_white(int image[], int start, int stop, int threshold) { for (int i = start; i < stop; ++i) image[i] = image[i] > threshold ? WHITE : BLACK; } // this function is started by in several threads below (:thread:) // it does the conversion for part of the image void partial_bw(signal* sig, int image[], int start, int stop) { // sum the color value of all pixels int sum = pixel_sum(image, start, stop); sig->total_sum += sum; sig->threads_done += 1; while (!sig->average_available) ; // wait for average to be calculated // replace pixels with black or white set_black_or_white(image, start, stop, sig->average); sig->threads_done += 1; cond_signal(sig->sigcond); } // main function to convert to black and white do_black_and_white(int image[], int image_size, int num_threads) { const int work_size = roof(image_size / num_threads); signal sig = { 0, 0, false, 0 }; int i; // create and start all working threads concurrently for (i = 0; i < (num_threads-1); ++i) { :thread:partial_bw(&sig, image, i * work_size, (i + 1) * work_size); } :thread:partial_bw(&sig, image, i * work_size, image_size); while (sig.threads_done < num_threads) ; // wait for complete sum to be available sig.threads_done = 0; sig.average = sig.total_sum / image_size; // calculate average sig.average_available = true; // and say it is available while (sig.threads_done < num_threads) ; // wait for picture to be completely converted } 7 (7)