COMPUTER AND NETWORK TECHNOLOGY Chapter 2: Introduction to Operating Systems and System Software Lecture 8
Contents The human interface; the operating principles and characteristics of printers, display devices, input devices, biometric devices, etc. Multitasking: interrupts, concurrency, scheduling, memory management and virtual memory System software: Internet browsers, email systems, security products (e.g. anti virus software) System performance and its evaluation: definition, measurement and benchmark
Multitasking - 1 In many current systems, the CPU contains multiple cores, which means it can run several processes at the same time. In addition, each core is capable of “multitasking”, which means it can switch from one process to another quickly, creating the illusion that many processes are running at the same time. The part of the operating system that implements multitasking is the “kernel”.
Multitasking - 2 In an operating system, the kernel is the lowest level of software, surrounded by several other layers, including an interface called a “shell.” At its most basic, the kernel’s job is to handle interrupts. An “interrupt” is an event that stops the normal instruction cycle and causes the flow of execution to jump to a special section of code called an “interrupt handler”.
Multitasking - 3 A hardware interrupt is caused when a device sends a signal to the CPU for e.g., a network interface might cause an interrupt when a packet of data arrives, or a disk drive might cause an interrupt when a data transfer is complete. Most systems also have timers that cause interrupts at regular intervals, or after an elapsed time. A software interrupt is caused by a running program for e.g., if an instruction cannot complete for some reason, it might trigger an interrupt so the condition can be handled by the operating system. Some floating-point errors, like division by zero, are handled using interrupts
Multitasking - 4 When a program needs to access a hardware device, it makes a system call , which is similar to a function call, except that instead of jumping to the beginning of the function, it executes a special instruction that triggers an interrupt, causing the flow of execution to jump to the kernel. The kernel reads the parameters of the system call, performs the requested operation, and then resumes the interrupted process.
Context Switching - 1 Context Switching defines the characteristics of a multitasking operating system in which, multiple processes shared the same CPU to perform multiple tasks without the need for additional processors in the system It is a technique or method used by the operating system to switch a process from one state to another to execute its function using CPUs in the system. When switching perform in the system, it stores the old running process's status in the form of registers and assigns the CPU to a new process to execute its tasks.
Context Switching - 2 While a new process is running in the system, the previous process must wait in a ready queue. The execution of the old process starts at that point where another process stopped it.
Need for Context Switching - 1 Following are the reasons that describe the need for context switching in the Operating system: The switching of one process to another process is not directly in the system. A context switching helps the operating system that switches between the multiple processes to use the CPU's resource to accomplish its tasks and store its context. It can resume the service of the process at the same point later. If the currently running process's data or context is not stored, the stored data may be lost while switching between processes.
Need for Context Switching - 2 If a high priority process falls into the ready queue, the currently running process will be shut down or stopped by a high priority process to complete its tasks in the system. If any running process requires I/O resources in the system, the current process will be switched by another process to use the CPUs. And when the I/O requirement is met, the old process goes into a ready state to wait for its execution in the CPU. Context switching stores the state of the process to resume its tasks in an operating system. Otherwise, the process needs to restart its execution from the initials level.
Need for Context Switching - 3 If any interrupts occur while running a process in the operating system, the process status is saved as registers using context switching. After resolving the interrupts, the process switches from a wait state to a ready state to resume its execution at the same point later, where the operating system interrupt occured. A context switching allows a single CPU to handle multiple process requests simultaneously without the need for any additional processors.
What is a PCB? - 1 A PCB (Process Control Block) is a data structure used in the operating system that contains information that are related to the process - for example, when a process is created in the operating system, updated information of the process, switching information of the process etc. The PCB is allocated by the operating system at the time a process is created.
What is a PCB? - 2 Among other things, the PCB keeps track of the process state, which is one of the below: Running: if the process is currently running on a core. Ready: if the process could be running, but isn’t, usually because there are more runnable processes. Blocked: if the process cannot run because it is waiting for a future event like network communication or a disk read. Done: if the process has completed, but has exit status information that has not been read yet.
Example of Context Switching - 1 Suppose that multiple processes are stored in a Process Control Block (PCB). One process (Process A) is in running state to execute its task with the use of CPUs. As Process A is running, another process (Process B) arrives in the ready queue, which has a high priority of completing its task using CPU. Here we used context switching that switches the current process (Process A) with the new process (Process B) which requiring the CPU to finish its tasks.
Example of Context Switching - 2 While switching the process, a context switch saves the status of the old process (Process A) in registers. When Process A reloads into the CPU, it starts the execution of the process where the new process (Process B) has stopped the old process (Process A). If we do not save the state of the process, we have to start its execution at the initial level. In this way, context switching helps the operating system to switch between the processes, store or reload the process when it requires executing its tasks.
Steps for Context Switching - 1 There are several steps involved in context switching of the processes. The following diagram represents the context switching of two processes, P1 to P2, when an interrupt, I/O needs, or priority-based process occurs in the ready queue of PCB.
Steps for Context Switching - 2 As can be seen in the diagram, initially, the P1 process is running on the CPU to execute its task, and at the same time, another process, P2, is in the ready state. If an error or interruption has occurred or the process requires input/output, the P1 process switches its state from running to the waiting state. Before changing the state of the process P1, context switching saves the context of the process P1 in the form of registers and the program counter to the PCB1 . After that, it loads the state of the P2 process from the ready state of the PCB2 to the running state.
Steps for Context Switching - 3 The following steps are taken when switching Process P1 to Process 2: First, the context switching needs to save the state of process P1 in the form of the program counter and the registers to the PCB, which is in the running state. Now update PCB1 to process P1 and moves the process to the appropriate queue, such as the ready queue, I/O queue and waiting queue. After that, another process gets into the running state, or we can select a new process from the ready state, which is to be executed, or the process has a high priority to execute its task.
Steps for Context Switching - 4 Now, we have to update the PCB for the selected process P2. It includes switching the process state from ready to running state or from another state like blocked, exit, or suspend. If the CPU already executes process P2, we need to get the status of process P1 to resume its execution at the same point where the system interrupt occurred. Process P1 is reloaded from PCB1 to the running state to resume its task at the same point. Otherwise, the information is lost, and when the process is executed again, it starts execution at the initial level.
Scheduling - 1 There might be hundreds of processes on a computer, but usually most of them are blocked. Most of the time, there are only a few processes that are ready or running. When an interrupt occurs, the scheduler decides which process to start or resume. On a workstation or laptop, the primary goal of the scheduler is to minimize response time; that is, the computer should respond quickly to user actions.
Scheduling - 2 As an analogy, suppose you are making an apple pie. The crust takes 5 minutes to prepare, but then it has to chill for half an hour. It takes 20 minutes to prepare the filling. If you prepare the crust first, you can prepare the filling while the crust is chilling, and you can finish the pie in 35 minutes. If you prepare the filling first, the process takes 55 minutes. Most schedulers use some form of priority-based scheduling, where each process has a priority that can be adjusted up or down over time. When the scheduler runs, it chooses the runnable process with the highest priority.
Scheduling - 3 Here are some of the factors that determine a process’s priority: A process usually starts with a relatively high priority so it starts running quickly. If a process makes a request and blocks before its time slice is complete, it is more likely to be interactive or I/O-bound, so its priority should go up. If a process runs for an entire time slice, it is more likely to be long-running and CPU-bound, so its priority should go down.
Scheduling - 4 If a task blocks for a long time and then becomes ready, it should get a priority boost so it can respond to whatever it was waiting for.
Concurrency C oncurrency is the execution of a set of multiple instruction sequences at the same time. This occurs when there are several process threads running in parallel. These threads communicate with the other threads/processes through a concept of shared memory or through message passing. Because concurrency results in the sharing of system resources - instructions, memory, files - problems can occur like deadlocks and resources starvation.
Principles of Concurrency - 1 With current technology such as multi core processors, and parallel processing, which allow for multiple processes/threads to be executed concurrently (i.e. at the same time), it is possible to have more than a single process/thread accessing the same space in memory, the same declared variable in the code, or even attempting to read/write to the same file. The amount of time it takes for a process to execute is not easily calculated – thus it is difficult to predict which process will complete first.
Principles of Concurrency - 2 The amount of time a process takes to complete depends on the following: The activities of other processes The way operating system handles interrupts The scheduling policies of the operating system
Advantages of Concurrency - 1 Running of multiple applications Having concurrency allows the operating system to run multiple applications at the same time. Better resource utilization Having concurrency allows the resources that are NOT being used by one application can be used for other applications. Better average response time Without concurrency , each application has to be run to completion before the next one can be run .
Advantages of Concurrency - 2 Better performance Concurrency provides better performance by the operating system. When one application uses only the processor and another application uses only the disk drive then the time to concurrently run both applications to completion will be shorter than the time to run each application consecutively .
Memory Management Memory Management is the process of controlling and coordinating computer memory, assigning portions known as blocks to various running programs to optimize the overall performance of the system. It is the most important function of an operating system that manages primary memory. It helps processes to move back and forward between the main memory and execution disk. It helps OS to keep track of every memory location, irrespective of whether it is allocated to some process or it remains free.
Why use Memory Management? It allows you to check how much memory needs to be allocated to processes that decide which processor should get memory at what time. Tracks whenever inventory gets freed or unallocated. According to it will update the status. It allocates the space to application routines. It also make sure that these applications do not interfere with each other. Helps protect different processes from each other It places the programs in memory so that memory is utilized to its full extent.
Memory Management Techniques - 1 Single Contiguous Allocation It is the easiest memory management technique. In this method, all types of computer's memory except a small portion which is reserved for the OS is available for one application. For example, MS-DOS operating system allocates memory in this way.
Memory Management Techniques - 2 Partitioned Allocation It divides primary memory into various memory partitions, which is mostly contiguous areas of memory. Every partition stores all the information for a specific task or job. This method consists of allotting a partition to a job when it starts & de-allocate when it ends.
Memory Management Techniques - 3 Paged Memory Management This method divides the computer's main memory into fixed-size units known as page frames. This hardware memory management unit maps pages into frames which should be allocated on a page basis.
Memory Management Techniques - 4 Segmented Memory Management Segmented memory is the only memory management method that does not provide the user's program with a linear and contiguous address space. Segments need hardware support in the form of a segment table. It contains the physical address of the section in memory, size, and other data like access protection bits and status.
Methods involved in Memory Management
Methods involved in Memory Management - 1 1. What is Swapping? Swapping is a method in which the process should be swapped temporarily from the main memory to the backing store. It will be later brought back into the memory to continue execution. Backing store is a hard disk or some other secondary storage device that should be big enough in order to accommodate copies of all memory images for all users. It is also capable of offering direct access to these memory images.
Methods involved in Memory Management - 2
Methods involved in Memory Management - 3 Benefits of Swapping It offers a higher degree of multiprogramming. Allows dynamic relocation. For example, if address binding at execution time is being used, then processes can be swap in different locations. Else in case of compile and load time bindings, processes should be moved to the same location. It helps to get better utilization of memory. Minimum wastage of CPU time on completion so it can easily be applied to a priority-based scheduling method to improve its performance.
Methods involved in Memory Management - 4 2. What is Memory allocation? Memory allocation is a process by which computer programs are assigned memory or space. The main memory is divided into two types of partitions Low Memory - Operating system resides in this type of memory. High Memory - User processes are held in high memory.
Methods involved in Memory Management - 5 3. Partition Allocation Memory is divided into different blocks or partitions. Each process is allocated according to the requirement. Partition allocation is an ideal method to avoid internal fragmentation.
Methods involved in Memory Management - 6 Below are the various partition allocation schemes: First Fit : In this type fit, the partition is allocated, which is the first sufficient block from the beginning of the main memory. Best Fit: It allocates the process to the partition that is the first smallest partition among the free partitions. Worst Fit: It allocates the process to the partition, which is the largest sufficient freely available partition in the main memory. Next Fit: It is mostly similar to the first Fit, but this Fit, searches for the first sufficient partition from the last allocation point.
Methods involved in Memory Management - 7 4. What is Paging? Paging is a storage mechanism that allows OS to retrieve processes from the secondary storage into the main memory in the form of pages. In the Paging method, the main memory is divided into small fixed-size blocks of physical memory, which is called frames. The size of a frame should be kept the same as that of a page to have maximum utilization of the main memory and to avoid external fragmentation. Paging is used for faster access to data, and it is a logical concept.
Methods involved in Memory Management - 8 5. What is Fragmentation? Processes are stored and removed from memory. This creates free memory space, which are too small to use by other processes. After sometimes, that processes not able to allocate to memory blocks because its small size and memory blocks always remain unused is called fragmentation. This type of problem happens during a dynamic memory allocation system when free blocks are quite small, so it is not able to fulfill any request.
Methods involved in Memory Management - 9 Two types of Fragmentation methods are: External fragmentation can be reduced by rearranging memory contents to place all free memory together in a single block. The internal fragmentation can be reduced by assigning the smallest partition, which is still good enough to carry the entire process.
Methods involved in Memory Management - 10 What is Segmentation? Segmentation method works almost similarly to paging. The only difference between the two is that segments are of variable-length, whereas, in the paging method, pages are always of fixed size. A program segment includes the program's main function, data structures, utility functions, etc. The OS maintains a segment map table for all the processes. It also includes a list of free memory blocks along with its size, segment numbers, and its memory locations in the main memory or virtual memory.
What is Virtual Memory? - 1 Virtual memory is a memory management technique where secondary memory can be used as if it were a part of the main memory. Virtual memory is a common technique used in a computer's operating system (OS). Virtual memory uses both hardware and software to enable a computer to compensate for physical memory shortages, temporarily transferring data from random access memory (RAM) to disk storage. Mapping chunks of memory to disk files enables a computer to treat secondary memory as though it were main memory.
What is Virtual Memory? - 2 Today, most personal computers (PCs) come with at least 8 GB (gigabytes) of RAM. However, sometimes, this is not enough to run several programs at one time and this is where virtual memory comes in. Virtual memory frees up RAM by swapping data that has not been used recently over to a storage device, such as a hard drive or solid-state drive (SSD). Virtual memory is important for improving system performance, multitasking and using large programs.
What is Virtual Memory? - 3 However, users should not overly rely on virtual memory, since it is considerably slower than RAM. If the OS has to swap data between virtual memory and RAM too often, the computer will begin to slow down and this is called thrashing . Virtual memory was developed at a time when physical memory -- also referenced as RAM -- was expensive. Computers have a finite amount of RAM, so memory will eventually run out when multiple programs run at the same time.
What is Virtual Memory? - 4 A system using virtual memory uses a section of the hard drive to emulate RAM. With virtual memory, a system can load larger or multiple programs running at the same time, enabling each one to operate as if it has more space, without having to purchase more RAM.
How does the Virtual Memory works? - 1 Virtual memory uses both hardware and software to operate. When an application is in use, data from that program is stored in a physical address using RAM. If, at any point, the RAM space is needed for something more urgent, data can be swapped out of RAM and into virtual memory. The computer's memory manager is in charge of keeping track of the shifts between physical and virtual memory.
How does the Virtual Memory works? - 2 If that data is needed again, the computer's Memory Management Unit will use a context switch to resume execution. While copying virtual memory into physical memory, the OS divides memory with a fixed number of addresses into either page files or swap files. Each page is stored on a disk, and when the page is needed, the OS copies it from the disk to main memory and translates the virtual addresses into real addresses.
How does the Virtual Memory works? - 3 However, the process of swapping virtual memory to physical is rather slow. This means using virtual memory generally causes a noticeable reduction in performance. Because of swapping, computers with more RAM are considered to have better performance.
Advantages of Virtual Memory Virtual Memory allows you to run more applications at a time. It allows you to easily fit many large programs into smaller programs. It helps to easily implement a multiprogramming environment. Common data can be shared easily between memory. With the help of virtual memory, speed is gained when only a particular segment of the program is required for the execution of the program. The process may even become larger than all of the physical memory.
Disadvantages of Virtual Memory Virtual memory reduces the stability of the system. The performance of Virtual memory is not as good as that of RAM. If a system is using virtual memory then applications may run slower. Virtual memory negatively affects the overall performance of a system. Virtual memory occupies the storage space, which might be otherwise used for long term data storage. This memory takes more time in order to switch between applications.