u Menoufia University Faculty of science Mathematics and Computer Science Department Mid Exam, Spring 2021/2022 Course: Operating Systems Code: Gamal Abd El Nasr St. Shebin El-Koom, Menoufia Egypt. Phone: +20 48 2222753 Fax: +20 48 2235689 http://mu.menofia.edu.eg/SCI/Home/en Code: M338 Date: 7/4/2022 Time: 9:00AM-10:00AM Answer The Following Questions Q. 1: Define each of the following • • • • • • • • • [20 Marks] Caching: Copying information into faster storage system; main memory can be viewed as a cache for secondary storage Interrupt service routine: Separate segments of code determine what action should be taken for each type of interrupt Multiprogramming: Loading several processes in computer’s main memory at the same time so, CPU always has one to execute for improving system efficiency. System calls: Programming interface to the services provided by the OS. Typically written in a high-level language (C or C++) and it is mostly accessed by programs via a high-level API rather than direct system call use. Ready queue: set of all processes residing in main memory, ready and waiting to execute Context-switch time: is overhead; the system does no useful work to user processes while switching Cooperating process: Process can affect or be affected by the execution of another process OpenMP: is a set of compiler directives as well as an API for programs written in C, C++, or FORTRAN that provides support for parallel programming in shared-memory environments. Critical Sections: A section of code, common to n cooperating processes, in which the processes may be accessing common variables. • Semaphore: A semaphore S is an integer variable that, apart from initialization, is accessed only through two standard atomic operations: wait () and signal (). It is used to control access to shared resources (files, devices, variables, …etc.) between cooperating processes. When one process modifies the semaphore value, no other process can simultaneously modify that same semaphore value. Q. 2: Determine whether each of the following statements is true () or false (X): [20 Marks] 1. Each device controller is in charge of any device type ………………………………………………….( X 2. I/O is from the device to local buffer of controller ………………………………………………………( 3. The OS must be fully aware of all the registers………………………………………………………….( 4. Secondary storage devices are volatile storage capacities ……………………………………………….( X 5. The disk controller determines the logical interaction between the device and the computer …………… ( 6. Direct memory access is used for low-speed I/O devices …………………….…………………………( X 7. Mode bit provided by hardware to distinguish when system is running user code or kernel code ……… ( 8. Most details of OS interface hidden from programmer by API ………………….………………………( 9. Block and stack methods limit the number or length of parameters being passed ………………………( X 10. In layered approach, each layer is implemented only with operations provided by higher level layers … ( X 11. The microkernel OS Structure moves as much from the kernel into “kernel” space …………………… ( X 12. The long-term scheduler controls the degree of multiprogramming …………………………………….( 13. In blocking send, the sender sends the message and continue …………………………………………... ( X 14. Many to One multithreading model maps one user-level thread to Many kernel threads ……………….( X 15. I/O devices and the CPU can execute concurrently ……….……………………………………………..( 16. A thread is also called light-weight process ……………….……………………………………………..( 17. Threads maximize the context switching time .………………………………………………………….. ( X Mid Exam, April 4, 2022 M338: Operating Systems Page 1 of 5 ) ) ) ) ) ) ) ) ) ) ) ) ) ) ) ) ) 18. A process can be both single threaded and multithreaded ………………………………………………..( ) 19. In implicit threading, creation and management of threads done by programmers rather than compilers and run-time libraries …………………………………………………………………………………….( X ) 20. A process must release the lock when it exits the critical section ……………………………………….( ) Q. 3: Answer The following questions [ 20 Marks] • Differentiate between task parallelism and data parallelism [5 marks] • Data parallelism – distributes subsets of the same data across multiple cores, same operation on each • Task parallelism – distributing threads across cores, each thread performing unique operation • What does the bootstrap program do? [3 marks] • bootstrap program is loaded at power-up or reboot o Typically stored in ROM or EPROM, generally known as firmware o Initializes all aspects of system o Loads operating system kernel and starts execution • List three of OS services that are helpful to the user [3 marks] • User interface - Almost all operating systems have a user interface (UI). o Varies between Command-Line (CLI), Graphics User Interface (GUI), Batch • Program execution - The system must be able to load a program into memory and to run that program, end execution, either normally or abnormally (indicating error) • I/O operations - A running program may require I/O, which may involve a file or an I/O device • Process manipulation (creation, communication, deletion, ….) • File-system manipulation - The file system is of particular interest. Programs need to read and write files and directories, create and delete them, search them, list file Information, permission management. • Communications – Processes may exchange information, on the same computer or between computers over a network o Communications may be via shared memory or through message passing (packets moved by the OS) • Error detection – OS needs to be constantly aware of possible errors o May occur in the CPU and memory hardware, in I/O devices, in user program o For each type of error, OS should take the appropriate action to ensure correct and consistent computing o Debugging facilities can greatly enhance the user’s and programmer’s abilities to efficiently use the system • A semaphore S is an integer variable that, apart from initialization, is accessed only through two standard atomic operations namely Wait() and Signal(). [2 marks] • In windows, the primary data structures of a thread include ETHREAD, KTHREAD, TEB [3 marks] • ETHREAD (executive thread block) – includes pointer to process to which thread belongs and to KTHREAD, in kernel space • KTHREAD (kernel thread block) – scheduling and synchronization info, kernel-mode stack, pointer to TEB, in kernel space • TEB (thread environment block) – thread id, user-mode stack, thread-local storage, in user space Mid Exam, April 4, 2022 M338: Operating Systems Page 2 of 5 • Consider P1 and P2 that require S1 to happen before S2, explain how you do this using semaphore. [4 marks] Solution Create a semaphore “synch” initialized to 0 P1: S1; signal(synch); P2: wait(synch); S2; Because synch is initialized to 0, P2 will execute S2 only after P1 has invoked signal(synch), which is after statement S1 has been executed. Q. 4: Answer The following questions [ 20 Marks] a. Explain Peterson’s solution to handle critical section problem with an example? [12 marks] Solution • Peterson’s solution is proposed for solving the critical section problem for only two process to ensure mutual exclusion for one resource (say one variable or data structure) • It does not require any special hardware. • It uses busy waiting (a spinlock). • Assume that the load and store machine-language instructions are atomic; that is, cannot be interrupted • In this solution, the two processes share two variables: o int turn; The variable turn indicates whose turn it is to enter the critical section o Boolean flag[2]; The flag array is used to indicate if a process is ready to enter the critical section. flag[i] = true implies that process Pi is ready! To enter its critical section. • Shared variables are created and initialized before either process starts. The shared variables flag[0] and flag[1] are initialized to FALSE because neither process is yet interested in the critical section. The shared variable turn is set to either 0 or 1 randomly (or it can always be set to say 0). do { flag[i] = true; turn = j; while (flag[j] && turn = = j); /* do no-op; */ critical section flag[i] = false; remainder section } while (true); Mid Exam, April 4, 2022 M338: Operating Systems Page 3 of 5 Example: Process 0 Process 1 i = 1, j = 0 i = 0, j = 1 flag[0] := TRUE turn := 1 check (flag[1] = TRUE and turn = 1) - Condition is false because flag[1] = FALSE - Since condition is false, no waiting in while loop - Enter the critical section - Process 0 happens to lose the processor flag[1] := TRUE turn := 0 check (flag[0] = TRUE and turn = 0) - Since condition is true, it keeps busy waiting until it loses the processor - Process 0 resumes and continues until it finishes in the critical section - Leave critical section flag[0] := FALSE - Start executing the remainder (anything else a process does besides using the critical section) - Process 0 happens to lose the processor check (flag[0] = TRUE and turn = 0) - This condition fails because flag[0] = FALSE - No more busy waiting - Enter the critical section b. Using an example, explain what is meant by race condition. Solution Example: Consider the producer-consumer problem; • counter++ could be implemented as register1 = counter register1 = register1 + 1 counter = register1 • counter-- could be implemented as register2 = counter register2 = register2 - 1 counter = register2 • Consider this execution interleaving with “counter= 5” initially: Mid Exam, April 4, 2022 M338: Operating Systems [8 marks] Page 4 of 5 o o o o o o S0: producer execute register1 = counter {register1 = 5} S1: producer execute register1 = register1 + 1 {register1 = 6} S2: consumer execute register2 = counter {register2 = 5} S3: consumer execute register2 = register2 – 1 {register2 = 4} S4: producer execute counter = register1 {counter = 6 } S5: consumer execute counter = register2 {counter = 4} The correct value for counter is 5 and the obtained value is 4. We would arrive at this incorrect state because we allowed both processes (producer and consumer) to manipulate the shared variable counter concurrently. • A situation like this, where several processes access and manipulate the same shared data concurrently and the outcome of the execution depends on the particular order in which the access takes place, is called a race condition. End of Questions Best Wishes Prof. Dr. Saied El-Zoghdy Mid Exam, April 4, 2022 M338: Operating Systems Page 5 of 5