Operating System For
Computer Science & Information Technology By
www.thegateacademy.com
Syllabus
Operating System
Syllabus for Operating System Processes, Threads, Inter-process communication, Concurrency, Synchronization, Deadlock, CPU scheduling, Memory management and virtual memory, File systems, I/O systems, Protection and security.
Analysis of GATE Papers (Operating System) Year
Percentage of marks
2013
12.00
2012
9.33
2011
5.00
2010
7.00
2009
8.00
2008
9.33
2007
9.33
2006
11.33
2005
2.67
2004
7.33
2003
8.00
Overall Percentage
8.12%
THE GATE ACADEMY PVT.LTD. H.O.: #74, Keshava Krupa (third Floor), 30th Cross, 10th Main, Jayanagar 4th Block, Bangalore-11 : 080-65700750, info@thegateacademy.com © Copyright reserved. Web: www.thegateacademy.com
Contents
Operating System
CONTENTS
#1.
#2.
Chapters
Page No.
Introduction to Operating System
1 – 10
1 2–3 3–5 5–6 6–8 9 10 10
Process Management
11 – 45
11 11 11 12 – 13 13 – 14 14
#3.
Introduction Evolution of Operating System Major Achievements Operating System Operations Micro Kernel Architecture Assignment Answer Keys Explanations
Process Process vs Program Process State Process Creation Process Termination Process Scheduling Interprocess Communication, Concurrency and Synchronization The Critical Section Problem Bakery Algorithm Semaphores The Readers and Writers Problem Monitors Solved Examples Assignment 1 Assignment 2 Answer Keys Explanations
14 – 17 17 – 19 19 – 20 20 – 23 23 – 26 26 – 29 30 – 33 34 – 38 38 – 41 42 42 – 45
Threads
46 -52
46 46 – 47 47 48 48 – 50 50 – 51 52 52
Thread Life Cycle Of Threads Multithreading Process Vs Thread User Level and Kernel Level Threads Assignment Answer Keys Explanations
THE GATE ACADEMY PVT.LTD. H.O.: #74, KeshavaKrupa (third Floor), 30th Cross, 10th Main, Jayanagar 4th Block, Bangalore-11 : 080-65700750, info@thegateacademy.com © Copyright reserved. Web: www.thegateacademy.com Page i
Contents
#4.
#5.
#6.
#7.
Operating System
CPU Scheduling
53 – 77
53 53 – 54 54 54 – 62 63 – 67 68 – 72 72 – 73 74 74 – 77
Basic Concept Schedulers Scheduling Methodology CPU Scheduling Algorithm Solved Examples Assignment 1 Assignment 2 Answer Keys Explanations
Deadlocks
78 -92
78 78 78 – 79 79 79 – 82 82 – 83 84 85 – 88 88 – 89 90 90 – 92
Definition Resource Allocation Graph Condition for Deadlock Deadlock Prevention Deadlock Avoidance Deadlock Detection Recovery From Deadlock Assignment 1 Assignment 2 Answer Keys Explanations
Memory Management
93-134
93 93 – 94 94 – 96 96 – 102 102 – 106 106 – 109 110 – 112 112 – 113 114 – 125 126 – 128 128 – 129 130 130 – 134
Introduction Logical and Physical Address Space Memory Management Requirements Memory Allocation Method Paging Segmentation Page Replacement Algorithm Thrashing Solved Examples Assignment 1 Assignment 2 Answer Keys Explanations
File System
135 – 153
135 135 – 137 137 – 138
Introdution File Concept Access Methods
THE GATE ACADEMY PVT.LTD. H.O.: #74, KeshavaKrupa (third Floor), 30th Cross, 10th Main, Jayanagar 4th Block, Bangalore-11 : 080-65700750, info@thegateacademy.com © Copyright reserved. Web: www.thegateacademy.com Page ii
Contents
#8.
#9.
Directory Structure Protection Free Space Management Solved Examples Assignment 1 Assignment 2 Answer Keys Explanation
Operating System
138 – 140 140 – 144 144 – 145 146 – 147 148 – 149 150 – 151 152 152 – 153
I/O System
154 - 172
154 154 – 155 155 – 157 157 – 158 158 – 159 160 – 163 164 – 167 167 – 168 169 169 – 172
Kernel I/O Subsystem Disk Structure Disk Scheduling Disk Management RAID Solved Examples Assignment 1 Assignment 2 Answer Keys Explanations
Protection and Security
173 – 185
173 173 – 176 176 – 178 179 – 180 181 – 182 182 – 183 184 184 – 185
Domain of Protection Access Matrix The Security Problem Solved Examples Assignment 1 Assignment 2 Answer Keys Explanations
Module Test
186 –199
186 – 194 195
Test Questions Answer Keys Explanations
Reference Books
195 – 199
200
THE GATE ACADEMY PVT.LTD. H.O.: #74, KeshavaKrupa (third Floor), 30th Cross, 10th Main, Jayanagar 4th Block, Bangalore-11 : 080-65700750, info@thegateacademy.com © Copyright reserved. Web: www.thegateacademy.com Page iii
Chapter 1
Operating System
CHAPTER 1 Introduction to Operating System
What is an Operating System? An operating system is a collection of programs and utilities. It act as the interface between user and computer. It creates a user friendly environment. The Operating System act as a Resource Manager. (A Computer system has many resources (Hardware and Software) which may require for completing a task). The computer uses resources like input/output device, memory, file, storage space, CPU time and so on. The operating system acts as a manager of these resources and allocates them to specific program and users as necessary for their tasks. Therefore we can say an operating system act as a resource manager. Generally resource sharing in two ways ‘in time’ and ‘in space’. For example a CPU is a time sharing resource and the main memory is a space sharing resource. The main difference between in time sharing and in space sharing resource is ‘in time’ resource is not divided into units, where as in space resources is divided into units. Structure of Operating System The structure of operating system consists of 4 layers; these are hardware, software, system program and application program. The hardware part consists of CPU, main memory, I/O device, Secondary storage etc. The software includes process management routines, memory management routines, I/O control routines, file management routines. The system program layer consist of complier, assembler etc. Last one is the application program that depends on users.
Components of a Computer System
THE GATE ACADEMY PVT.LTD. H.O.: #74, Keshava Krupa (third Floor), 30th Cross, 10th Main, Jayanagar 4th Block, Bangalore-11 : 080-65700750, info@thegateacademy.com © Copyright reserved. Web: www.thegateacademy.com Page 1
Chapter 1
Operating System
Evolution of Operating System i.
Serial Processing: Before 1950 the programmer directly interacts with hardware. There was no operating system at that time, if the programmer wish to execute a program on these days, the following steps are necessary. Type the program on punch card Convert punched card to card reader Submit to the computing machine, is there any error, the condition was indicated by lights. The programmer examines the registers and main memory to identify the cause of error. Take the output of error. Take the output on the printers. Then the programmer ready for the next program
This type of processing takes much time and next program should wait for the completion of previous one. The programs are submitted to the machine one after one. Therefore this machine is said to be serial processing. ii. Batch Processing: In older days (before 1960), it was difficult to execute a program using computer. Because the computer located in three rooms. One room for card reader second room for executing the program third room for printing the result. The user or machine operator needs to run between these three rooms to complete a job. We can solve this problem using Batch processing. In batch processing same type of task are grouped together and execute at a time. The carrier carries the group of job at a time from one room to another. Therefore the programmer need not to run between these 3 rooms several times. iii. Multiprogramming: Multiprogramming is a technique to execute number of programs simultaneously by a single processor. In multiprogramming more than one process can reside in main memory at a time. The operating system picks and begins execute one of the jobs in the main memory. In non multiprogramming system, the CPU can execute only one program at a time. If the running program is waiting for any I/O device, the CPU becomes idle, so it will effect the performance of the CPU. But in multiprocessing environment, any I/O wait happened in a process and then the CPU switches from the job to another job in the job pool. So the CPU is not idle at any time. iv. Time Sharing System: Time sharing or multitasking is a logical extension of multiprocessing. In time sharing system the CPU selects a job from the ready queue and switches the CPU to that job. When the time slot is expired the CPU switches from this job to another, in this method CPU is shared by different process, so it is said to be “Time Sharing System.” The main advantage of time sharing system over the batch processing system is the user can interact with the job when it is executing but it is not possible in batch system. Another advantage of time sharing system is efficient CPU utilization. v. Parallel System: If a system has more than one processor in close communication, sharing the computer bus, the clock, memory and peripheral devices, this system is referred as “tightly coupled” system. A system consisting of more than one processor and if it is a tightly coupled and then the system is called parallel system. One advantage of THE GATE ACADEMY PVT.LTD. H.O.: #74, Keshava Krupa (third Floor), 30th Cross, 10th Main, Jayanagar 4th Block, Bangalore-11 : 080-65700750, info@thegateacademy.com © Copyright reserved. Web: www.thegateacademy.com Page 2
Chapter 1
Operating System
parallel system is increased “Throughput”. The performance of CPU is measured in terms of Throughput. The number of jobs completed by a CPU with in a time period is said to be “Throughput”. Multiprocessor can also save money compared to multiple single system because the processor can share peripherals, cabinet and power supply. Another advantages of multiprocessor system is that they increase reliability. vi. Distributed System: In distributed system, the processor can’t share memory or clock, each processor has its own local memory. The processor communicate one another though various communication lines. These system are usually called loosely coupled system. The advantage of distributed system are: 1. Resource sharing 2. Computation speed up 3. Reliability vii.
Real Time System Used when there are rigid time requirements on the operating of a processor or the flow of data. Systems that control scientific experiments, medical imaging systems, industrial control systems and some display system are real time systems. Two types of real time systems are: A hard real time system: guarantees that critical tasks completed on time. Soft real time system: A less restrictive type of real time system is a soft real time system. Given their date of deadline support they are risky to use for industrial control and robotics
Spooling The expansion of spooling is simultaneous peripheral operations on line. Simultaneous means for example if two or more user issue print command and the printer accept the request even the printer is printing some other jobs. The printer printing one job at the same time the spool disk can load some other jobs. Operating System Functions 1. Program creation 2. Programs execution 3. Input output operation 4. Error detection 5. Resource allocation 6. Accounting 7. Protection
Major Achievements The major achievement of operating system are Process Management A process is a program in execution: (A program is passive in nature while a process is active) A process has resources (CPU time, files) and attribute that must be managed.
THE GATE ACADEMY PVT.LTD. H.O.: #74, Keshava Krupa (third Floor), 30th Cross, 10th Main, Jayanagar 4th Block, Bangalore-11 : 080-65700750, info@thegateacademy.com © Copyright reserved. Web: www.thegateacademy.com Page 3
Chapter 1
Operating System
Management of Processes Includes:
Process scheduling (priority, time management,…) Creation/termination Block/Unblock (suspension/resumption) Synchronization Communication Deadlock handling Debugging
Main Memory Management
Allocation/de-allocation for processes, files, I/O Maintenance of several processes at a time Keep track of who’s using what memory Movement of process memory to/from secondary storage.
File Management A file is a collection of related information defined by its creator, commonly files represent program (both source and object forms) and data. The operating system is responsible for the following activities in connections with file management:
File creation and deletion Directory creation and deletion Support of primitives for manipulating files and directories Mapping files onto secondary storage File backup on stable (non volatile) storage media
I/O Management
Buffer caching system Generic device driver code Drivers for each device- translate requests into position commands
Secondary Storage Management
Disk, tapes, optical Free space management ( paging/swapping) Storage allocation (what data goes where on disk) Disk scheduling
Networking
Communication system between distributed processors. Getting information about files/processes/etc. on remote machine.
THE GATE ACADEMY PVT.LTD. H.O.: #74, Keshava Krupa (third Floor), 30th Cross, 10th Main, Jayanagar 4th Block, Bangalore-11 : 080-65700750, info@thegateacademy.com © Copyright reserved. Web: www.thegateacademy.com Page 4
Chapter 1
Operating System
Can use either a message passing or a shared memory model.
Protection Of files, memory, CPU, etc. Means controlling of access Depends on the attributes of the file and user System Program Command Interpreters – Program that accepts control statements (shell, GUI interface, etc.) Compilers/linkers Communication (ftp, telnet, etc.) System Tailoring Modifying the operating System program for a particular machine. The goal is to include all the necessary pieces but not too many extra ones.
Typically a System can support many possible devices but any one installation has only a few of these possibilities. Plug and play allows for detection of device and automatic inclusion of the code (driver) necessary to drive these devices. A system is usually a link of many OS routines/modules in order to produce an executable code to run the drivers.
Operating System Operations Modern operating systems are interrupt driven. If there are no processes to execute, no I/O devices to service and no users to respond, an operating system will quietly waiting for something to happen. Events are almost always signaled by the occurrence of an interrupt or a trap. Traps and interrupts are events that disrupt the normal sequence of instruction executed by the CPU. A trap is an abnormal condition detected by the CPU, that usually is an indicative of an error. Trap condition can occur in following ways (a) Dividing by zero (b) Trying to access a memory location that does not exists or for which the program does not have access (c) Executing an instruction with an undefined opcode, (d) Trying to access a nonexistent I/O device. An interrupt is a signal sent to the CPU by an external device, typically an I/O device. It is the CPU’s equivalent of a pager, a signal requesting the CPU to interrupt its current activities to attend to the interrupting device needs. A CPU will check interrupts only after it has completed the processing of one instruction and before it fetches a subsequent one. The CPU responds to traps and interrupts by saving the current value of the program counter and resetting the program counter to a new address. This allows the CPU to return to the executing point where the trap or interrupt occurred, after it has executed a procedure for handling the trap or interrupt.
THE GATE ACADEMY PVT.LTD. H.O.: #74, Keshava Krupa (third Floor), 30th Cross, 10th Main, Jayanagar 4th Block, Bangalore-11 : 080-65700750, info@thegateacademy.com © Copyright reserved. Web: www.thegateacademy.com Page 5