Basic Principles of an Operating System: Learn the Internals and Design Principles 9789388511711

2,274 383 14MB

English Pages [379]

Report DMCA / Copyright

DOWNLOAD FILE

Polecaj historie

Basic Principles of an Operating System: Learn the Internals and Design Principles
 9789388511711

Citation preview

Basic Principles of an Operating System Learn the Internals and Design Principles by Dr. Priyanka Rathee

FIRST EDITION 2019 Copyright © BPB Publications, INDIA ISBN: 978-93-88511-71-1 All Rights Reserved. No part of this publication can be stored in a retrieval system or reproduced in any form or by any means without the prior written permission of the publishers LIMITS OF LIABILITY AND DISCLAIMER OF WARRANTY The Author and Publisher of this book have tried their best to ensure that the programmes, procedures and functions described in the book are correct. However, the author and the publishers make no warranty of any kind, expressed or implied, with regard to these programmes or the documentation contained in the book. The author and publisher shall not be liable in any event of any damages, incidental or consequential, in connection with, or arising out of the furnishing, performance or use of these programmes, procedures and functions. Product name mentioned are used for identification purposes only and may be trademarks of their respective companies. All trademarks referred to in the book are acknowledged as properties of their respective owners. Distributors: BPB PUBLICATIONS 20, Ansari Road, Darya Ganj New Delhi-110002 Ph: 23254990/23254991 MICRO MEDIA Shop No. 5, Mahendra Chambers, 150 DN Rd. Next to Capital Cinema, V.T. (C.S.T.) Station, MUMBAI-400 001 Ph: 22078296/22078297 DECCAN AGENCIES 4-3-329, Bank Street, Hyderabad-500195 Ph: 24756967/24756400 BPB BOOK CENTRE 376 Old Lajpat Rai Market,

Delhi-110006 Ph: 23861747 Published by Manish Jain for BPB Publications, 20 Ansari Road, Darya Ganj, New Delhi-110002 and Printed by him at Repro India Ltd, Mumbai Dedicated to My Parents, Brothers and My Amiable Husband Preface Operating system is essential component of computers, laptops, smart phones and any other devices. The operating system is a program that manages the computer hardware. It plays a role of intermediary among computer user and the computer hardware (CPU, memory, etc.). The primary goal of an operating system is to establish an environment where a user can easily interact with the computer to execute a program in a convenient and efficient manner. It works as a resource allocator. This book is basically going to be a complete text book that includes theory, implementation, case studies, lots of review questions, questions from GATE and some smart tips. Better and easy understanding of the concepts. In support of it a number of examples and diagrams are given in the book to explain the concepts. It will increase the readability and concept understanding for the readers. Key features: The strength of the book includes easy reading and understanding, in-depth covering of topic, explanation with relevant diagrams and examples, contains lots of review questions for clarity of the concept, Clarification of concepts using case studies. The book will help to achieve high confidence level and thus high performance of the reader. The book is organized into 11chapters Chapter 1: Introduction and structure of Operating system This chapter describes the basics of operating system. What does operating system is all about? History of operating system, its components and goals are given in this chapter. This chapter also includes the description of architecture of operating system. The hardware and software components of operating system are also explored and discussed in this chapter. Along with these concepts an overview of machine language, assembly language and high level language is also presented with inclusion of generations of computers. Chapter 2: Operating system services The operating system is a program that manages the computer hardware. It plays a role of intermediary among computer user and the computer hardware (CPU, memory, etc.). The primary goal of an operating system is to establish an environment where a user can easily interact with the computer to execute a program in a convenient and efficient manner. It works as a resource allocator. IBM played a vital role to produce the concept of an operating system. The secondary goal of an operating system is to maintain efficiency. This chapter describes the Application Programming interface, compiling, linking and loading. Design issues of operating system are also covered in this chapter. The types of operating system, basic features of operating system and interrupts are also explained under this chapter. Chapter 3: Process management In this chapter, we will get to know about processes, scheduling algorithms, threads, what thread is, how threads are implemented? We talked about various models of threads. This chapter also discussed the issues that are faced in using threads. Chapter 4: Inter-process communication and process synchronization This chapter gives an idea about how communication takes place between two processes, what are the methods of communication, how synchronization between two processes are achieved and modern tools of synchronization. Chapter 5: Deadlock When two or more processes are waiting on some event to happen which never happens when those processes are said to be involved in the deadlock. This chapter discusses the conditions to occur deadlock and various methods to handle deadlock. Chapter 6: Memory organization and management Memory is a large array of words or bytes each with its own address. CPU fetches instructions from memory according to the value of the program counter. Each process has a separate memory space. This chapter presents a various memory management and placement strategies. The concept of paging and segmentation is also given in this chapter.

Chapter 7: Virtual memory organization The basic memory management techniques suffer from limited memory space. One of its solutions is large memories but it is very expensive. Another solution is to create an illusion that more memory exists. This is the basic idea of virtual memory. This chapter includes various methods to organize virtual memory. This chapter also addresses some issues occurred in virtual memory organization. Chapter 8: File system organization and implementation This chapter covers the concept of file, its organization and implementation. The chapter discusses various file access methods, directory structure, file allocation methods, UNIX i-node implementation etc. Chapter 9: Secondary storage structure In this section, we will discuss the basics of disk management. On a disk, we can perform two operations, read and write. What will be the status on outcome of the read and write operation? The three outcomes are possible: Successful read or write: Operations are done fruitfully Partially successful read or write: Some of the disk data is falling on a bad sector. Unsuccessful read or write. Chapter 10: Protection and security The main security issues are reliability and protection. Reliability is the prevention of information from physical damage. This can be achieved by duplicating the copies of files. Protection is related to improper access of files. The two extreme policies regarding file access are either does not permit file access to other users or provide full access without any access restrictions. But these two are quite general approaches. The more specific approach is controlled access. In this approach, access is granted on the basis of the type of access requested. The various operations on a file like read, write, execute, delete, append, rename, list, copy etc. may be controlled. Chapter 11: CASE study This chapter covers the basic concept of UNIX, its features, architecture, shell programming, elementary UNIX/LINUX commands and some features of mobile operating systems Acknowledgment Doing this project was wonderful and challenging experience for me. During the period, many people have given their precious and invaluable support to me either directly or indirectly in shaping up my academic career. Without their support, it was hardly possible for me to complete this book writing work smoothly. I would like to express sincere gratitude to BPB Publications for giving me this opportunity and believing in me. I would like to express my thanks to Tayyab Khan for helping me in developing content. My highest gratitude goes to my parents, brothers, and specially to my beloved husband Mr. Deepak Singh for their relentless supports, blessing and encouragement. They provide a constant source of motivation in my life. Dr. Priyanka Rathee About the Author Dr Priyanka currently works as an Assistant Professor in the Department of Computer Science & Engineering, National Institute of Technology Hamirpur (H.P). In the past she has worked in University of Delhi. She received her PhD degree in 2018, M.Tech. degree (Computer Engineering) in 2011, and B.Tech. degree (Honors) in Computer Science and Engineering in 2008. She has published many research papers and book chapters in reputed national and international journals and conferences, including papers in IEEE Xplore, and SCI paper in wireless personal communication. She received two best paper and presentation awards in international conferences. Currently, she is serving as a Chairperson at IEEE Young Professional Delhi Section. Her LinkedIn profile: www.linkedin.com/in/priyanka-rathee-31066667 Table of Content Preface Acknowledgment

About the Author 1. Introduction and Structure of Operating System 1.1 What is Operating System? 1.2 History of Evolution of Operating System 1.3 Operating System Environments 1.4 Operating System Components and Goals 1.4.1 Core Operating System Components 1.4.2 Operating System Goals 1.5 Operating System Architecture 1.5.1 Monolithic Architecture 1.5.2 Layered Architecture 1.5.3 Microkernel Architecture 1.6 Hardware and Software Concepts 1.6.1 Evolution of Hardware Devices 1.6.2 Hardware Components 1.6.3 Hardware support for operating systems 1.6.4 Software Overview Review Questions 1. Operating System Services 2.1 Introduction 2.2 Application Programming Interface 2.3 Compiling, Linking, and Loading 2.4 Key Terminologies 2.5 Operating System Design and Implementation 2.5.1 Design Issues 2.5.2 Design Policies and Mechanisms 2.5.3 Operating System Implementation 2.6 Types of Operating System 2.6.1 Batch Processing System/Sequential Processing 2.6.2 Multiprogramming Operating System 2.6.3 Time-sharing and Multitasking Operating System 2.6.4 Real-time Systems 2.6.5 Distributed Operating System 2.6.6 Multiprocessor System/ Parallel or Tight Coupling System

2.7 CPU Modes 2.7.1 Basic Features of Operating System Services 2.7.2 Process Management 2.8 System Calls 2.8.1 Types of System Calls 2.9 System Programs 2.10 Interrupts 2.10.1 Interrupt Processing 2.10.2 Types of Interrupt Review Questions 1. Process Management 3.1 Introduction 3.1.1 Process Control Blocks/Process Descriptors 3.1.2 Process Operations 3.1.3 Context Switching 3.1.4 RPC and Client-Server Systems 3.2 Process States: Life Cycle of a Process 3.2.1 Process State Models 3.2.2 Process State Transitions 3.3 FORK() System Call 3.4 Process Scheduling 3.4.1 Scheduling Queues 3.4.2 Scheduler 3.5 Scheduling Techniques/Algorithms 3.5.1 Preemptive & Non-Preemptive Scheduling 3.5.2 First Come First Serve (FCFS) Scheduling Algorithm 3.5.3 Shortest Job First (SJF) Scheduling Algorithm 3.5.4 Shortest Remaining Time First (SJF-preemptive) 3.5.5 Longest Job First (LJF) Scheduling Algorithm 3.5.6 Round Robin Scheduling Algorithm 3.5.7 Priority Scheduling Algorithm 3.5.8 Multilevel Queue Scheduling Algorithm 3.5.9 Multilevel Feedback Queue Scheduling Algorithm 3.5.10 Highest Response Ratio First Algorithm 3.6 Thread Concepts

3.6.1 Process and Threads 3.7 Thread Operations 3.7.1 Types of Thread 3.8 Multithreading Model 3.9 Threading Issues 3.9.1 Fork() and exec System Call 3.9.2 Thread Cancellation 3.9.3 Signal Handling 3.9.4 Thread Pools 3.9.5 Thread-specific Data Review Questions 1. Inter Process Communication and Process Synchronization 4.1 Introduction 4.2 Key Terms to Understand Synchronization 4.2.1 Critical Section 4.2.2 Mutual Exclusion 4.2.3 Synchronization 4.3 Conditions to Achieve Synchronization 4.4 Peterson Solution of Critical Section Problem 4.5 Modern Tool of Synchronization-Semaphore 4.5.1 Binary Semaphore 4.5.2 Counting Semaphore 4.6 Hardware Implementation of Synchronization 4.7 Monitors 4.8 Interprocess Communication 4.8.1 Inter-process Communication Mechanisms/Models 4.9 Classical IPC Problems 4.9.1 Producer-Consumer Problem 4.9.2 Reader-writer Problem for Synchronization 4.9.3 Dining Philosopher Problem Review Questions 1. Deadlock 5.1 Definition of Deadlock 5.2 Conditions to Occur Deadlock Deadlock Characterization

5.3 Methods for Handling Deadlock 5.3.1 Deadlock Prevention 5.3.2 Deadlock Detection and Recovery 5.3.3 Deadlock Recovery 5.3.4 Deadlock Avoidance Review Questions 1. Memory Organization and Management 6.1 Background 6.2 Memory Organization 6.3 Memory Management Strategies 6.4 Memory Placement Strategies 6.4.1 First Fit Strategy 6.4.2 Best Fit Strategy 6.4.3 Worst Fit Strategy 6.4.4 Next Fit Strategy 6.5 Memory Allocation 6.5.1 Fixed Partition Multiprogramming 6.5.2 Variable Partition Multiprogramming 6.5.3 Compaction 6.6 Paging 6.6.1 Structure of Page Table 6.6.2 Hardware Support for Paging 6.6.3 Drawback of Paging 6.7 Segmentation 6.8 Segmentation involving Paging (Segmented Paging) Review Questions 1. Virtual Memory Organization 7.1 Background 7.2 Concept of Virtual Memory 7.2.1 Virtual Addresses to Real Address Mapping 7.3 Demand Paging 7.3.1 Page Fault 7.3.2 Dirty Bit or Modified Bit 7.4 Multilevel Paging 7.5 Inverted Paging

7.6 Page Replacement Algorithm 7.6.1 First in First Out (FIFO) 7.6.2 Optimal Page Replacement Algorithm 7.6.3 Least Recently used (LRU) Page Replacement Algorithm 7.7 Thrashing 7.7.1 Locality of Reference 7.7.2 Working Set Model Review Questions 1. File System Organization and Implementation 8.1 Introduction 8.1.1 Function of File Systems 8.1.2 File Operations 8.1.3 File Attributes 8.1.4 File Management 8.2 Access methods or file organization 8.2.1 Sequential Access 8.2.2 Direct Access 8.2.3 Indexed 8.3 Directory structure 8.3.1 Operations Performed on Directory 8.3.2 Logical Structures of Directory 8.4 File System Structure 8.5 File Allocation Methods 8.5.1 Contiguous File Allocation 8.5.2 Linked List File Allocation 8.5.3 Tabular File Allocation 8.5.4 Indexed File Allocation 8.6 UNIX i-node Implementation 8.7 Free Space Management in the Disk 8.7.1 Bit Vector 8.7.2 Linked List 8.7.3 Grouping 8.7.4 Counting Review Questions

1. Secondary Storage Structure 9.1 Introduction 9.1.1 Basic Terminologies 9.2 Disk Structure 9.2.1 Disk Attachment 9.3 Disk Management 9.3.1 Disk Formatting 9.3.2 Boot Block 9.3.3 Bad Blocks 9.3.4 Swap Space Management 9.4 Disk Scheduling Algorithms 9.4.1 First Some First Serve 9.4.2 Shortest Seek Time First 9.4.3 SCAN (Elevator Disk Scheduling) 9.4.4 C-SCAN 9.4.5 LOOK Scheduling 9.4.6 C-LOOK (Circular LOOK scan) 9.4.7 Last in first out (LIFO) Review Questions 1. Protection and Security 10.1 Introduction 10.2 Protection 10.2.1 Goals of Protection 10.2.2 Protection domain 10.2.3 Access Matrix 10.2.4 Access Control 10.3 Security 10.3.1 Security Levels 10.3.2 Authentication and Authorization 10.4 Threats to Operating System 10.4.1 Program Threats 10.4.2 System Threats 10.4.3 Difference between Virus, Worm, and Trojan Horse 10.5 Threat Monitoring

10.5.1 Firewall 10.5.2 Encryption Review Questions 1. Case Study 11.1 Introduction 11.1.1 History of UNIX 11.1.2 Features of UNIX Operating System 11.2 UNIX Architecture 11.3 UNIX File System and Shell Programming 11.3.1 UNIX File System Organization 11.3.2 UNIX File Permissions 11.3.3 File Modes 11.3.4 User Masking 11.3.5 Change Owner 11.3.6 UNIX Linked Files 11.3.7 UNIX Commands and Redirections 11.4 Elementary UNIX/LINUX Commands 11.4.1 UNIX File Colors 11.4.2 UNIX Files Notation 11.5 Mobile Operating System Review Questions CHAPTER 1 Introduction and Structure of Operating System 1.1 What is Operating System? Operating System is software that controls the hardware. Different applications may interfere with each other, if not programmed carefully. Therefore, as a result, a layer of software called operating system was introduced that allows each application to execute without any interference. The kernel is the software which consists of core of the operating system. The operating system behaves like a manager that manages software and hardware to get the required result. The primary goal of an operating system is to manage resources and hardware like Memory, Processors, Input/Output devices, and Communication devices. 1.2 History of Evolution of Operating System 1940s and 1950s In the 1940s, the digital computers did not have an operating system. The programmers used machine language and used to enter the machine language program 1 byte at a time by using punch cards. The programming process geared up by using assembly language. It used symbols in machine language. The first operating system was implemented by General Motors Research Laboratories in the early 1950s for its IBM 701 computer. The 1950s system executed only one job at a time, which involves a set of program instructions related to a particular computational task such as payroll or inventory. These systems were called Single-Stream batch processing systems . The 1950s operating system reduced inter-job transition time by using job stream, in this reader automatically read the next job after finishing up of the current job. But the programmer had to control system resources like Memory and Input/Output devices directly. This was a quite difficult and slow process. Also, in early systems, the entire program

needs to be loaded in memory for the program to run. 1960s The computers of 1960s used the concept of batch processing but executed several jobs at a time. These systems introduced peripheral devices like card readers, card punches, printers, tape drives, and disk drives. Systems improved resource utilization, thus allowing one job using the processor (CPU bound jobs) and another for peripheral devices (Input/Output jobs). The concept of multiprogramming was also introduced in the 1960s i.e., managing several jobs at a time. The primary goal of these systems is Resource Sharing . In the 1960s, the program submission for execution was done by using punch cards or computer tapes manually. The programmer had to sit for hours or even for days before it could be processed. Even a minor mistake like comma could create a blunder because after correction again the programmer had to sit for hours until the processing completes. In 1964, IBM introduced its system/360 family of computers where “ 360 ” indicates all points on the compass to show universal applicability. Then over the years, IBM announced the family of system 370 series, 390 series, and then z series. Then, more advanced systems provided the service for multiple interactive users at once. In the 1960s, the users interacted with the operating system through “ dumb terminals ” (devices that provided user interface without processor power) that were online (directly connected to the computer through the active line). Time-Sharing Systems and Real-Time Systems were also introduced in the 1960s. Real-time systems were bound to a specific time period. The operating system needed to distinguish between the type of users and the level of services provided to the particular user. Batch processing jobs could tolerate delay up to a certain limit. Interactive jobs needed some higher level of performance. Real-time systems could not tolerate the delays as they are time-critical systems. 1970s Primarily, the systems of the 1970s were multimode multiprogramming systems supporting batch processing, time-sharing, and real-time applications. The development in microprocessor technology was in progress. TCP/IP communication standard was used to improve communication in the Department of Defense (DOD) . The concept of Local Area Network (LAN) was introduced in the 1970s by Ethernet standard developed at Xerox’s Palo Alto Research Center (PARC) . With the increase of information transmission, the security problems also came in the picture. 1980s Microprocessor technology evolved in 1981, when IBM released a personal computer. In 1984, Apple Macintosh released personal computers. The GUI and Mouse were introduced by Xerox’s Palo Alto Research Center (PARC) . The GUI was embedded in Machintosh’s operating system. Distributed computer became popular via client-server model. The client is a user computer that send the request to the server machine and server fulfills the request. 1990s In 1990s, the development in hardware technology was on the boom. By the end of the decade, the storage capacity of hard disk increased up to gigabyte and the personal computer was able to execute hundred Million instructions per second (MIPS) . The distributed computing also got popular during 1990s. The operating system support for networking became standard with the increased demand for the Internet connections. During the 1990s, Microsoft made its hold in the software industry. Microsoft released the first Disk operating system (DOS) in 1981 for IBM personal computer. Microsoft developed windows operating system in the mid-1980s and it was graphical user interface built on top of DOS. In1990, Microsoft released another version of windows operating system with rich functionalities named as Windows 3.1, then Windows 95 followed by Windows 98. In 1993, Microsoft released Windows NT. During the 1990s, object-oriented technology became popular with the increased popularity of C++ and Java. During 1990s, the development in computing introduced open source software. The usage of free and open source software became common. Examples are the Linux operating system and Apache web server . Linux was created by Linus Torvalds. In 1980s, Richard Stallman made improvements in UNIX operating system to make the code available for free of cost. Then, Stallman created GNU (GNU’s Not Unix) project for the same purpose of not selling the code. To protect open source software and promote the advantages of open source programming, open source initiative was founded in the late 1990s. In the 1990s, the operating system became user-friendly by introducing the concept of Graphical user interface (GUI) and “ plug and play ” capability that allows the user to add and remove the hardware components without manually reconfiguring the operating system. 2000 and Beyond In 2000, middleware was introduced. It is software to link two separate applications. Middleware behaves like a courier to pass messages between database and web server. It simplifies communication between different architectures. Parallel computing got the fame in the current decade. Linux

became more popular and introduced Portable Operating System Interface (POSIX) to improve the compatibility with other UNIX based operating systems. Apple introduced MacBook air (personal computer) in 2008. In October 2011, Acer, HP, Lenovo, LG, Asus, Toshiba, and Samsung release their ultra-books. 1.3 Operating System Environments There are various operating system environments related to general purpose computers. Embedded systems provide a different operating system design challenge. In embedded environments, resource management is the major consideration to build a successful operating system. Because of limited storage capacity, an operating system must provide services by using a minimum amount of code. Real-time systems are time-critical systems. The task needs to be finished within a given time frame. An example is an autopilot feature of aircraft. There may be soft real-time systems and hard real-time systems. In soft real-time systems task is finish within a given time limit, delay up to some limit is affordable. However, in hard real-time systems the task must be finished on time, no delay is affordable in this. Business critical systems are databases and servers. The business does not necessarily fail if a business-critical system does not meet its objectives. 1.4 Operating System Components and Goals Initially, there was no operating system in computer systems. Then as machines progressed to multiprogramming, time-sharing systems, personal computers, up to distributed computer systems. With the increased demand for features, the operating system evolved to fill new roles. 1.4.1 Core Operating System Components The kernel is software that contains core operating system components. The core components of operating systems are: Process scheduler : Determines when and for how long the process executes on a processor. Memory manager : Determines memory allocation on processor and alternative in case the main memory is full. Input/Output manager: Determines if Input/Output services are provided to hardware devices. Interprocess communication (IPC) manager : Determines the communication of process with each other. File system manager : Maintains data stored on disk and allow the retrieval of information from the disk. 1.4.2 Operating System Goals The goals of operating system are efficiency, scalability, portability, interactivity, robustness, extensibility, security, and usability. The efficient operating system means having high throughput and low average turnaround time. The throughput is a measure of the amount of work done by a processor within a given time period. Turnaround time is the time period from submission of request till the system returns the result. The robust operating system means the system is reliable and fault tolerant i.e. prone to hardware errors. Scalability means the more resources can be added in the existing system. This is the main feature of multiprocessing systems. As the processors are added, the processing power should increase proportionally. Portable operating systems can operate on many hardware configurations. This is a very important property as one application can run on various hardware configurations. It reduces the cost of developing different hardware configurations for different applications. Interactive operating systems responds quickly to user actions or events. The extensible operating system adapts new technologies and allows the extension of the original operating system with new features. Secure operating system disallows an unauthorized user to access the applications of the operating system. The usability property of the operating system refers to the user interface provided by the operating system whether it is GUI or CLI. 1.5 Operating System Architecture Operating System complexity increases with the increase in service provided by the operating system. This complexity can be reduced by organizing operating system components and specifying privilege given to the particular component. There are three kinds of operating system architectures. These are:

1.5.1 Monolithic Architecture Monolithic architecture is the oldest architecture of the operating system. The properties of monolithic architecture are as follows: Each component of an operating system is contained in the kernel. Each component can communicate with each other directly by using function calls. The property of direct intercommunication makes monolithic architecture efficient. It is less secure as the access of components is unrestricted and it is more susceptible to damage from malicious code. It is difficult to isolate the source of errors and bugs. ( figure 1.1 )

Figure 1.1 Monolithic architecture 1.5.2 Layered Architecture With the increased size and services of the operating system, monolithic operating systems were hard to manage. To overcome this problem, the layered approach was used where the components performing similar functions were grouped together in one layer. The properties of the layered architecture are: Each layer can communicate with its adjacent upper and lower layers. Lower layers provide services to the upper layers following the concept of abstraction. A layered architecture is modular as compared to the monolithic architecture. Modular approach simplifies validation, debugging, and modification as a modification at one layer does not affect the components of another layer. Performance in layered architecture degrades as compared to monolithic architecture because the request has to pass through various layers before it got serviced. Similar to monolithic architecture, a layered architecture is less secure because of the presence of unrestricted access. A first operating system using layered approach was “ THE ” system built by E.W. Dijkstra in 1968. Later windows XP and Linux used layered architecture up to some level. 1.5.3 Microkernel Architecture The Microkernel Architecture consists of a system with APIs to add plugins. The microkernel is providing a small number of simple abstractions such as address space and inter-process communication. All the system services such as file system, memory manager, CPU scheduling etc. are implemented as processes above microkernel. In other words, these operating system services run at the same privilege level as user-level applications. All of them are placed in their individual address space. Only the microkernel runs at the different level of privilege provided by the processor architecture. Since all the operating system services are implemented as server processes on top of a microkernel, they may have to

cooperate with each other in order to satisfy a particular user request. In that case, they may have to communicate with each other and will be required IPC that is provided by the microkernel. The properties of the microkernel architecture are: In Microkernel architecture essential components are contained in the kernel. Examples: memory management, synchronization etc. It provides a high level of modularity. Most operating system components like a file system, process scheduler, and device manager execute outside the kernel with a low level of privilege. As inter modular communication increases, performance decreases. ( figure 1.2 )

Figure 1.2 Microkernel architecture 1.6 Hardware and Software Concepts Today’s era is based on the technologies and most of the work is done by using computers. Every computer or cell phone contains hardware and software that are managed by the operating system. Therefore, an operating system should be designed by keeping in mind the requirements of hardware and software managed by the operating system. A few of hardware and software managed by the operating system are processors, memory, processes, threads, files, and databases. 1.6.1 Evolution of Hardware Devices With the development of technology, demand for computing power is also increased with the improved efficiency. Initially, hardware was managed by writing code using system programming. With the technological development, developers started writing software which manages hardware by sending requests to the operating system. As the hardware complexity increased, this type of management got complex. Therefore, developers started writing the operating system independent of hardware configurations. Then the operating system starts using the device driver, provided by the hardware manufacturer, to perform device-specific I/O operations. Many hardware components were designed to facilitate the extensibility of the operating system. Example: plug and play devices, which get install in the existing operating, by choosing an appropriate device driver with little or no human intervention. 1.6.2 Hardware Components Hardware components of a computer are processors, main memory, and input/output devices. The operating system manages hardware components to meet the user’s requirement. 1.6.2.1 Motherboard (Mainboard) Computers are equipped with one or more Printed Circuit Boards (PCBs) to enable the communication between independent devices. PCB, a hardware component provides electrical connection among devices on the board at various locations. The motherboard is a central PCB in a computer system and is called the Backbone of the computer system. Other components like processors, main memory, and other hardware devices are inserted into the slots provided by the motherboard. The computer users can customize their hardware components by adding or removing the components to and from the slot. The slots provide access to electrical connections between various hardware components.

The electric wires used for connections on the motherboard are extremely thin layer of silicon with microscopic electrical connections called Traces , which serve as a communication channel. The combination of a set of traces from a high-speed communication channel is known as the Bus . Motherboards also include some chips to perform low-level operations. For example, Basic Input/Output system (BIOS) . The function of BIOS includes instructions stored for basic hardware management and initialization, loading the initial portion of the operating system into memory. This process of loading is called Bootstrapping . The operating system can use BIOS to communicate with the system’s hardware to perform low-level Input/Output operations after loading into memory. The controller is another chip on the motherboard that manages data transfer at onboard buses. Chipset on a motherboard is a collection of controllers, coprocessors, buses, and other hardware integrated on a motherboard that determines the hardware capabilities of a system’s hardware. 1.6.2.2 Processors The processor is a hardware component that executes chain of instructions. Processors can be a Central Processing Unit (CPU), a Graphics Coprocessor and Digital Signal Processor (DSP) . CPU was designed to execute general purpose instructions. The Graphics Coprocessor and Digital Signal Processor were designed to execute special-purpose instructions like 3D transformations. The CPU is the primary processor of a system that executes a number of instructions. To improve the efficiency, CPU sends the specific task to the coprocessor or DSP. The number of instructions executed at a time or the type of instructions executed by a processor is determined by the instruction set and instruction architecture. A few components like an instruction fetch unit, branch predictor, execution units, registers, bus interface, and caches are present in all processors. Instruction fetch unit loads the instructions into instruction registers, a high-speed memory to decrease the execution time of the processor. Instruction decode unit interprets the instruction and passes the decoded instructions to an execution unit for execution. Arithmetic and logic unit (ALU) is the main component of the execution unit which performs an arithmetic operation like addition, subtraction, multiplication, division, and logical operations like logical comparisons. The bus interface helps the processor to interact with memory and other devices. Cache is the high-speed memory which stores frequently used data and increases processor efficiency by allowing fast access to instructions and data. The size of the cache memory is relatively smaller than the main memory because cache memories are more expensive than main memory. There are two types of caches: L1 cache : L1 cache is located on the processor. It is more expensive and faster. L2 cache: L2 cache is located on the motherboard. It is less expensive, slower, and larger than L1 cache. To improve the performance, it is integrated onto the processor. Registers are high-speed memories located on processors which hold data for immediate use by the processor. Accessing data from registers are faster than L1 cache as registers are located near execution unit and are hardwired to processor circuitry. The register size is dependent on the number of bits operated by a processor at a time. 1.6.2.3 Clocks The time in computer is estimated in terms of cycles, called Clock Tick. The cycle depicts one oscillation of an electrical signal supplied by a system clock generator. Clock generator determines the frequency at which buses transfer data, generally measured in cycles per second. 1.6.2.4 Memory hierarchy Memory hierarchy includes the cost/performance tradeoff. The latency of various memories in processor cycles from top to bottom is 0, 2-3, 10, 30, and 10 ⁶ respectively as shown in the figure 1.3 .

Figure 1.3 Memory hierarchy Registers are fastest nearly as the speed of processor but are most expensive. As we go down the hierarchy, the speed, cost of the memories decreases, and capacity increases. The speed of the cache is measured on the basis of Latency i.e. the time required to transfer data. Next in the hierarchy is the Main Memory or Real Memory or Physical Memory . The size of the general-purpose main memory ranges from 100 megabits to 10 or 100 of gigabits. Registers, caches, and main memory are volatile as their data wash up when the power is switched off. At the end of the hierarchy is the Secondary Storage. It is least expensive, slowest, and having largest capacity. The advantages of secondary storage are that these are non-volatile, means data is persistent on the power loss and have a huge capacity of 100 of gigabits. Main Memory Main memory consists of Random Access Memory (RAM). It is a volatile memory. Random here means that processes can access data locations in any order. Memory latencies for each memory address are essentially the same but in secondary memories, this is not essential. The types of RAM are dynamic RAM and static RAM. Most common is DRAM . Difference between DRAM and SRAM is explained in table 1.1 . Table 1.1 Difference between DRAM and SRAM The main purpose behind designing the DRAM is to bridge up the gap between processor speed and memory transfer speed. It reduces overall latency and increases bandwidth (amount of data transferred per unit of time). Secondary Storage/Auxiliary Storage/Persistent Storage Due to volatile nature and less storage, main memories are not suitable for a large amount of data and the data that needs permanent storage even after the power loss. Therefore, secondary memories are a better option to store a large amount of data. Most common secondary storage is a hard disk. The access time of hard disk is much more than main memory because of mechanical movements while reading the data from hard disk. Mechanical movements are much slower than the speed of electrical signals between main memory and processors. The hard disk is an example of the block device, as it transmits data in fixed sized blocks of bytes. A few of secondary storage media facilitates data backup and data transfers between computers. For example, a Compact Disk (CD) which stores 700 MB of data. Digital Versatile Disks (DVDs) can store up to 5.6 GB of data. The size of DVD is same as the size of a CD but DVDs store data on much more thinner tracks. 1.6.2.5 Buses A bus is a collection of traces which transport information between hardware devices. Devices communicate with other devices by sending electrical signals over the bus. A bus consists of an address bus and data bus. A port is a bus which helps in connecting the two devices. The Input/Output channel is a bus that is shared by several devices to perform I/O operations. Accessing main memory is another issue for channels and processors. The controller is a hardware device that prevents two signals from colliding on the bus, the memory access is prioritized and channels are generally given priority over processors. This is called cycle stealing as I/O channel effectively steals cycle from the processor. Front side bus connects the processor to memory modules i.e. main memory. The speed of buses is measured in MHz . A few of chipset perform two memory transfers per cycle. This feature is called quad pumping . The Peripheral Component Interconnect ( PCI) bus connects peripheral devices like network cards and sound cards. 1.6.2.6 Direct memory access (DMA)

Earlier, Programmed I/O (PIO) was used to transfer data between main memory and an I/O device. In this system, the processor waits for each memory transfer to complete. DMA improves this technique. By using DMA technique, devices, and controllers were allowed to transfer data to and from main memory directly, that frees the processor to execute software instructions. I/O controller is used by DMA to manage data transfer between I/O devices and main memory. The I/O controller generates an interrupt after finishing up the operation to notify the processor. ( figure 1.4 )

Figure 1.4 Process of DMA The process of DMA is described as follows in three steps: The processor transmits I/O request to the controller. The request is then sent to the disk by the controller. After that the processor keeps on executing the instructions. The data is sent to an I/O controller by disk. The data is kept at the memory address as directed by the direct memory access command. The disk generates an interrupt to the processor in order to notify the completion of an I/O. 1.6.2.7 Peripheral devices Peripheral devices are hardware components that are not necessarily needed by the computer to execute software instructions. Peripheral devices include network devices like modem, network interface cards; I/O devices like mouse, printer, scanner, and storage devices like CD, DVD, and disk drive. Main memory, motherboards, and processors are not counted as peripheral devices. Internal peripheral devices like sound cards, internal CD ROM, and modems are called Integrated Peripheral Devices. Peripheral devices are connected to computers via ports and buses. The ports transfer one bit at a time are called serial ports , example: a keyboard and a mouse. The ports transfer several bits at a time are called parallel ports , example, a printer. The small computer system interface (SCSI) is a popular example of a parallel interface. 1.6.3 Hardware support for operating systems Hardware supports the operating system in improving the performance of the system. Hardware also provides the feature for protection that improves the integrity and security of the operating system. Processor Operating systems rely on processors for implementing protection mechanism by preventing unauthorized access and memory allocation to unwanted user. At the time, if any process tries to violate the rule set for protection, the processor sends the notification to the operating system. The operating system responds accordingly. User mode, kernel mode, and privileged instructions A computer system usually contains various execution modes. Variation in modes helps in building a system secure, robust, and fault tolerant. An

Application has to access a subset of instructions according to the particular mode in which the application is executing the instructions. In user mode , also called user state or problem state , the access is limited to certain types of instructions present in the instruction set. In kernel mode , also called supervisor state, the access is not limited. The user in kernel mode is authorized to all the instructions in the instruction set. The operating system provides kernel mode to more trusted users. One way to secure the system is by giving the privileges to a particular user. For example, some of the users get read-only privilege, while some of them get read and write both. Memory protection and management Memory protection prevents the memory access by processes that are not allocated to them. The memory protection mechanism is implemented by using processors registers and can be modified only by the privileged instructions. To prevent the access of memory by the process to which that memory location is not allocated, the processor checks the value of the register. For example, the processor using contiguous memory location, the beginning and end value of the memory location is stored in registers called Bound registers . Whenever the processor demands the particular memory location, the processor checks the bound register whether that particular memory location is in the specified range or not. If the location is present, the access is given to the process, otherwise, the processor notifies the operating system and the operating system responds accordingly. Some of the hardware converts the virtual address into the physical or real address at compile time or at runtime (dynamically). Interrupts and exceptions Processor notifies the operating system about the events like change in device status or program execution errors. The processor can do the same by periodically requesting the status to each device. This technique is called Polling . But, this introduces a lot of execution overhead if the requesting device has not changed its status. Therefore, the devices themselves send signals to the processor if any event occurs. These signals are called Interrupts . The operating system can respond accordingly after getting the notification from the processor. If the processor got interrupted for any kind of error or failure like logical error or protection violation etc., is called an Exception . Timer and clocks The timer generates an interrupt that causes the processor to invoke operating system. It uses timers to prevent the non-preemptive or monopolize nature of the processes. An example is the round robin approach. In this approach, whenever the time quantum of the particular process finishes, the process has to leave the processor so that another process can use the processor. The clock in the system maintains the “wristwatch time” and is a measure of continuity in the system. Bootstrapping The operating system should be loaded into main memory before it starts working. When the system is powered up, the BIOS initializes the hardware and the instructions are loaded from secondary memory (boot sector) to main memory. This process is called Bootstrapping . If boot sector is not found in a particular system, then the system will not be able to load instructions or operating system into main memory and in effect, the user will not be able to access any computer’s hardware components. The alternative of BIOS is Extensible Firmware Interface (EFI) developed by Intel Corporation which enables the users to access computer’s hardware directly. Plug and play The “ Plug and Play ” technology permits the operating system by configuring and using the newly installed hardware without human intervention. A “ plug and play ” hardware device recognizes its corresponding driver and allows the operating system to use it to configure the device. It identifies itself uniquely to the operating system and communicates with the operating system to indicate resources and services required to function properly. Caching and Buffering To improve performance, most of the systems use caching by keeping the frequently used files or information in cache memory. Cache memory is faster and quite expensive. Therefore, its size is smaller because of the cost. If the referenced information is not found in the cache, the system must retrieve the information from slower storage i.e. main memory. This effect is called Cache Miss . If the referenced information is found in the cache, it is called Cache Hit . Cache is the high-speed memory which stores frequently used data and increases processor efficiency by allowing fast access to instructions and data. The size of the cache memory is relatively smaller than the main memory because cache memories are more expensive than main memory. The buffer is temporary storage device when the speed of the sender and receiver is different. Buffer improves system performance by allowing hardware and software devices to transmit data and requests asynchronously (i.e. independent of one another). 1.6.4 Software Overview Programmers write instructions or programs in various languages. Some languages are directly understood by computer and other needs

translation. Generally, the types of programming languages are machine language, assembly language and high-level language, which are discussed below. 1.6.4.1 Machine language Machine language is specific to the computer and is defined by computer’s hardware design. A certain set of instructions are specific for a certain type of computer. It will not work on another computer. Machine language is a series of 0s and 1s. It is also called the Binary Language. Advantage No translation is needed. Execution time is quite less. Disadvantage Machine language is machine dependent i.e. a particular machine language can be used on only one type of computer. Machine language programming is error prone and is hard to understand. 1.6.4.2 Assembly language To overcome the problems faced in machine language, programmers began to use assembly language. Assembly language is the form of English like abbreviations or symbols to represent different operations. Assembly language is not directly understood by the computers. Translators are needed to convert assembly language to machine language. The translator for converting assembly language to machine language is called Assembler . 1.6.4.3 High-level language There are lot of high-level languages. But here we will discuss a few widely used high-level languages like FORTRAN, COBOL, C C++, Java. Table 1.2 explains the evolution of Programing Language. FORTRAN was developed by IBM in 1950s for creating engineering and scientific applications which require a lot of complex mathematical calculations. FORTRAN is popular for high-performance systems like mainframe and supercomputers. Common Business Oriented Language ( COBOL) was developed by a group of government agencies, industrial computer users, and computer manufacturers in the late 1950s. It was mainly designed for business purposes. A lot of business software is written by using COBOL even today. C was developed by Dennis Ritchie at Bell Laboratories in the early 1970s and got popularity as the programming language for UNIX operating system. C++ , an extension of C, it was developed by Bjarne Stroustrup at Bell Laboratories in the early 1980s. C++ uses the concept of object-oriented programming like objects with the reusability property. Java is another object-oriented programming language. It was developed by James Gosling at Sun Microsystem in 1995. It was capable of creating web pages dynamically. Because of this property, it became popular in the business sector. In 2000, Microsoft launches .net and C# in the market. Extra fact : The first computer programmer was a woman. A British mathematician, Ada Lovelace, published an English translation of an Analytical Engine article in 1843 written by Luigi Menabrea who was an Italian engineer. The first computer “bug” was named after a real bug. Table 1.2 Evolution of Programming languages 1.6.4.4 Generations of computers The generation of computers means the change or advancement in technology. Every new generation will be having some more advanced features than the previous one. Till date, there are five generations of the computer. These are described below in the table 1.3 along with their time period, features, and the technology components used in each generation.

Table 1.3 Generations of computers 1.6.4.5 Translators Translators are the programs which convert high-level language into machine language. The high-level language is the simple programs written by the programmer. The machine language is the set of binary strings, the machine-readable code. The code written in the high-level language is called source code and the machine language code generated after compilation is called the object code . Machine language is also called lowlevel language. Types of Translators There are three types of translators: Compiler

Interpreter Assembler Compiler The compiler is a translator which converts high-level language to machine language. It diagnoses the error while converting program from highlevel language to machine language. It converts the entire program in one go. ( figure 1.5 ) The error report will be generated after compiling the whole code. Table 1.4 Differences between Compiler and Interpreter

Figure 1.5 Compiler Interpreter The interpreter is a translator which converts high-level language to machine language. It translates the program line by line and reports the error as soon as the error occurs during the translation process. It directly executes the operations mentioned in the source program if the input is given by the user. It provides better error diagnostics as compare to compiler. The error notification will occur as soon as the interpreter gets the first error. ( figure 1.6 )

Figure 1.6 Interpreter Assembler The assembler is a translator which translates the assembly language program into machine language. ( figure 1.7 )

Figure 1.7 Assembler Table 1.4 Differences between Compiler and Interpreter Review Questions Operating system manages only hardware? (True/False) What are the primary purposes of operating system? Why was assembly language developed? What was developments in 1970s that improved the communication between computer systems? What aspect of personal computers, popularized by the Apple MacIntosh, made them especially easy to learn and use? Write some benefits of open source developments? What is the defining characteristic of monolithic operating system? Why do monolithic operating systems tend to be efficient? What is the key weakness of monolithic kernels?

How layered operating systems are more modular than monolithic operating system? Why do layered operating systems tend to be less efficient than monolithic operating systems? What is the difference between a purely layered architecture and micro kernel architecture? How does microkernel promote portability? List some common hardware and software resources managed by operating systems. What is the primary function of motherboard? Why are the BIOS crucial to computer systems? Differentiate between a CPU and a coprocessor. All components of a system operate at the same clock speed. (True/False) What is the difference between persistent and volatile storage media? Why does the memory hierarchy assume a pyramidal shape? Compare main memory to disk in terms of access time, capacity and volatility? Why is main memory called random access memory? Why is accessing data stored on disk slower than accessing data in main memory? Compare and contrast CDs and DVDs? How does FSB speed affect system performance? How do controllers simplify access to shared buses? Why is DMA more efficient than PIO? What is the main difference between a peripheral device such as printer, and a processor? Compare and contrast USB and firewire? What is the difference between compiling and assembling? Could a java program run directly on a physical machine instead of virtual machine. There are no instructions smaller than machine language instructions. (True/False) Describe the role of firmware in a computer system? What are the costs and benefits of using middleware? How does EFI address the limitations of BIOS? Why should the operating system prevent users from accessing the boot sector? Is it necessary for a plug and play device to uniquely identify itself to the operating system? Why power management is important for mobile device? How does caching improve the system performance? Why do buffers generally not improve performance if one device or process produces data significantly faster than it is consumed? Computers typically execute assembly code directly. (True/False) Is software written in machine language portable? Discuss the benefits of high-level languages over assembly languages.

Why programs compiled to bytecode are more portable than those compiled to machine code? Classify each of the following programming languages as structured or object oriented: C# C Java C++ CHAPTER 2 Operating System Services 2.1 Introduction The operating system is a program that manages the computer hardware. It plays a role of intermediary between computer user and the computer hardware (CPU, Memory, etc.). The primary goal of an operating system is to establish an environment where user can easily interact with the computer to execute a program in a convenient and efficient manner. It works as a resource allocator. IBM played a vital role to produce the concept of an operating system. The secondary goal of an operating system is to maintain efficiency. 2.2 Application Programming Interface Applications need a lot of resources to fulfill the task. For example: data fetch request from a remote machine, files on disk etc. But, these applications can’t use these resources directly. They can use these resources via operating system. The operating system acts as a resource manager. To facilitate the applications to request for resources, Application Programming Interfaces ( APIs) were designed. Examples of APIs are Portable Operating System Interface (POSIX) standard for UNIX operating system and windows API for Windows operating systems. ( figure 2.1 ) In order to access system resources, the user submits the request to the operating system. The operating system checks whether the requested resource is available or not. If the resource is available, then access to that resource is granted to the user otherwise the user will be asked to wait until the resource released by other processes or users. ( figure 2.2 )

Figure 2.1 Application programming interface

Figure 2.2 Operating system interface 2.3 Compiling, Linking, and Loading Nearly, all programs are written in a high-level language . Before execution of the code, the high-level code should be translated into machine code then it is linked with another machine code if it is dependent on that code. Then after linking the converted and linking code is put in the memory for execution. In the below section, we will study these procedures step by step. Compiling As discussed in Chapter 1 , the high-level programs need to be converted into machine language before execution. For this purpose, the compiler is used. The compiler is a program that accepts high-level language program as source code and converts it into machine language code called object code . This process of compilation is divided into several steps. ( figure 2.3 )

Figure 2.3 Steps of compilation Linking A program may contain several independent sub-programs. A few of commonly used routines are packaged into a pre-compiled module called Libraries . All the modules referenced by a program are integrated into a single executable unit, this process is called linking . The program object module contains instructions and data retrieved from source file after compilation. If the object module is referenced by another module, then the referenced instructions are converted into external references, and given external names. These instructions are then stored by an object module in the symbol table. ( figure 2.4 )

Figure 2.4 Linking The output of the linker is the load module . Figure 2.5 , explains how to create the load module

Figure 2.5 Creating a load module Figure 2.6 , explains how to create the load module, if a program contains two or more subprograms.

Figure 2.6 Load module for subprograms The fields of object module format contain a header, machine code, initialized data, symbol table, and relocation information. ( figure 2.7 )

Figure 2.7 Object module format Relocation of an object module If a program contains several sub-programs, then every object module is supposed to be stored at location 0 according to the absolute address. Therefore, when these modules are loaded into memory, there may be location clash. Therefore, to avoid such situations, relocation of address is needed. Consider an example: There are two sub-modules of a program. Object module1 starts from zero and ends at location 99, i.e. of 100 bytes. Object module2 starts from 0 and ends at location 49, i.e. 50 bytes. So, the address of one of the module needs to be modified. This is called Relocating the address . ( figure 2.8 )

Figure 2.8 Relocation of object module into a load module Difference between relocating and linking The linker performs two tasks: Relocation: Relocation combines address spaces of all object modules into a single address space. Linking of object modules: Linking is the modification of addresses where one object module refers to another object module. ( figure 2.9 )

Figure 2.9 Relocation and Linking of module Loading After creating a load module, the linker passes it to the loader. Loader places every instruction and data at a particular memory location. This process is called Address Binding . If the loader is provided with the physical address, then the loader directly put the instruction and data at the specified memory location. This technique is called Absolute Loading . If the addresses given are not absolute, then these must be converted into absolute addresses, this technique is called Relocatable Loading . 2.4 Key Terminologies Control Program A control program manages the execution of user programs to prevent error and improper usage of the computer. Kernel Kernel can be considered as a heart of the operating system. The kernel is the software which allows users to share computer resources. It is also responsible for process scheduling, memory management, I/O operation etc. BootStrap Program The program which runs initially whenever a computer is rebooted or switched on is called a bootstrap program and it is stored in ROM . The bootstrap program makes sure that the operating system is loaded for the execution of the system. In order to fulfil the task, the bootstrap program need to be located and loaded in an operating system kernel memory. Then the operating system begins its execution of the first process, and it waits for some event to happen. The happening of an event is generally signaled by an interrupt either from software or hardware. The hardware can activate an interrupt at any point of time to the CPU by transmitting a signal. The software can trigger an interrupt by executing a specific operation known as a system call or monitor call.

Interrupt When CPU is interrupted it stops its working and transfer its execution immediately to a fixed location. That location contains the starting address of the location of Interrupt Service Routine (ISR) . The interrupt must transfer control to the appropriate interrupt service routine. After the completion of current execution, the CPU resumes the interrupted computation. Storage Structure The program must be located in the main memory (RAM) so that it can be directly accessed by the processor. The association is attained through a chain of load-store instructions to a particular memory address. The load instructions move a word within the CPU from main memory to the internal register. The context of a register to the main memory is moved by the stored instruction. A large portion of operating system code is dedicated to managing I/O because of the varying nature of devices. 2.5 Operating System Design and Implementation This section explains the design issues, mechanisms, and implementation of an operating system. 2.5.1 Design Issues There may be various design issues. A few of them are described as below. Choice of hardware : The choice of hardware depends on the compatibility of the operating system and the type of system used. Type of system : The design of the operating system also depends on the type of system used. The various types of system are a batch system, real-time system, distributed system, single user, and multi-user systems. User requirements : The user requirements for the operating system may be that the operating system should be easy to use, reliable, robust, secure, portable, etc. But these requirements vary from user to user. System requirements : The system requirements include compatibility of hardware to operating system, error-free operating system, flexible, scalable, robust, etc. But again, it depends on the type of system used. Therefore, no general solution exists for this design issue. 2.5.2 Design Policies and Mechanisms The Design policy is about what to do and mechanism states how to do. Consider an example, if there are many processes ready to execute, then the decision regarding which process will be executed is policy and how the process will be executed is a mechanism. These two concepts should be designed separately so that any change in policy may not affect mechanism and change in mechanism may not affect policy. The systems using microkernel architecture implements both the concepts separately. 2.5.3 Operating System Implementation After completion of the designing part, the implementation phase starts. To implement an operating system, the programming language is needed. Traditionally, operating systems were written in assembly language but today operating systems are also written in the high-level language . The first operating system written in a high-level language was the Master Control Program (MCP). It was written in ALGOL. The code written in the high-level language is much easier to understand, faster to write, debug, and is portable from one hardware to another. For example, Linux is written in C language and portable but MS-DOS is written in assembly language (Intel 8088) and can run only on Intel family of CPUs. 2.6 Types of Operating System There are various types of operating systems. Some of them are discussed as follows: 2.6.1 Batch Processing System/Sequential Processing Batch is a collection of jobs. In this system, the next process will be executed after the completion of the first process. ( figure 2.10 )

Figure 2.10 Batch processing The problem in this system is that at a time, only one job is executed. Consider an example, while executing an instruction, when CPU is doing some work, then I/O processor is idle. Similarly, If the instruction is using I/O processor, at that time CPU is idle. Therefore, the CPU and I/O processor are not used efficiently. If a job requires less CPU time and more I/O then the CPU is idle for most of the time i.e. CPU utilization is reduced. The other processes waiting for CPU also suffer from starvation. 2.6.2 Multiprogramming Operating System It is an improvement of the batch operating system. The objective of a multiprogramming operating system is to increase CPU utilization. This kind of operating system increase the CPU utilization by organizing jobs i.e. data and code so that the CPU has always one process to execute. An operating system retains various jobs in the memory concurrently to pick up one job and begin to execute, and then pick up next and so on. ( figure 2.11 , 2.12 )

Figure 2.11 Multiprogramming process

Figure 2.12 Process execution Advantage Efficient utilization of various system resources like CPU, memory, peripheral devices. High throughput. (Throughput is the number of jobs completed per unit of time.)

Note : Multiprogramming also decides whether to run job parallelly or serially. If two jobs are independent then we can run in parallel otherwise we run the jobs serially. Disadvantage Poor response time.

Figure 2.13 Showing process migration from CPU to a printer When users gave job to the computer, operating system will select one of them. In case if job1 and job2 given to processor are lengthy and taking long time to get completed, user2 and user3 are waiting for their job to be done. As once the processor gets free from its earlier job then only it can proceed with the job of user2 and user3, so the response time is poor in this case. ( figure 2.13 ) To remove this problem timesharing and multitasking operating system came in the picture. 2.6.3 Time-sharing and Multitasking Operating System Time-sharing and multitasking operating system overcomes the problem of poor response time. It is a logical extension of multiprogramming operating system. Problem with a time-sharing system: Critical jobs do not respond quickly due to lack of proper time slot. Consider an example, suppose there are four users (user1, user2, user3, user4) and user4 gives a critical job, but CPU is executing the job of user1, then the critical job has to wait for the time till the job of user1 gets completed. 2.6.4 Real-time Systems These systems handle the problem faced by critical jobs in time sharing and multitasking operating systems. The most important jobs are to be responded quickly on the basis of priority and interrupt vector. Real-time systems are subdivided into two categories: Hard real-time operating system Soft real-time operating system. Advantage High recognition speed. Disadvantage Bottleneck problem : Suppose there are more than two critical jobs to execute. In that case, CPU can execute one at a time. This problem is called the bottleneck problem . 2.6.5 Distributed Operating System The bottleneck problem occurred in real time system is resolved in distributed systems. The examples of distributed systems are Internet and Online banking. ( figure 2.14 )

Figure 2.14 Distributed operating system 2.6.6 Multiprocessor System/ Parallel or Tight Coupling System The multiprocessor systems consist of more than one CPU. All CPUs can work in parallel. Therefore, these are also known as parallel systems . The major advantage of multiprocessor systems is reliability. If one system goes down, then the load of that particular CPU can be shifted to another CPU. ( figure 2.15 )

Figure 2.15 Multiprocessor operating system Question 2.1 : In multiprogramming, turnaround time for short jobs and long jobs respectively are: (a) Shortened and unchanged (b) Shortened and lengthen (c) Shortened and shortened (d) Lengthen and shortened Answer: (c) Solution : For short jobs: finishing time will be shortened due to multiprogramming. For long jobs: finishing time shortened due to multiprogramming as multiprogramming increase throughput. 2.7 CPU Modes There are two processors (CPU) modes of operation: Kernel (supervisor) mode / privileged mode / monitor mode / system mode / executive mode. User mode (no privileged mode). The processor (CPU) will be in a kernel mode when CPU bit in the status register is set to zero. The processor enters in kernel mode at power up or as a result of interrupt, exception or error. CPU mode bit indicate the current CPU mode. Here, 0 indicate the kernel mode and 1 indicate the user mode. The processor leaves kernel mode

and enters to user mode when the CPU mode bit is set to 1 by executing some non-privileged instructions. ( figure 2.16 )

Figure 2.16 CPU mode The trap is a software interrupt generated by user mode applications when an exception is thrown. Example: division by zero, invalid memory access, system calls. Important points Privileged instructions can be executed in kernel mode only. Non-privileged instructions can be executed in user mode and kernel mode. Privileged instructions can’t be executed in user mode. In user mode, the program has access only to CPU and floating-point unit (FPU) registers. In kernel mode, the program has full access to the processor including CPU registers, execution of CPU instruction, hardware, peripheral device etc. Memory address space is divided into two categories. These are user address space and kernel address space . The user address space has an address range of [0- 7FFFFFFF ¹⁶ ]. The kernel address space has an address range of [80000000 ¹⁶ – FFFFFFFF ¹⁶ ]. When an interrupt or execution occurs, the hardware has to perform actions, such as: Saving contents of Program counter (PC). Program counter gets new value and interrupt code is saved. Mode of CPU operation set to kernel mode. System crash in user mode are always recoverable but not in kernel mode. Programs in user mode cannot interfere with interrupts and context switching. The operating system runs in privileged mode, it can interact with hardware directly and can manipulate hardware abstractions. User programs run in unprivileged mode and cannot interact with hardware directly. Switching from user to kernel mode The steps to transfer control from user mode to kernel mode are: Step1 : The user-mode program places values in registers and creates a stack frame with arguments to indicate what specific service it requires from the operating system. Step 2 : The user-mode program then performs the trap instruction. Step 3 : The CPU switches to kernel mode immediately and jumps to instructions at a fixed location in memory. Step 4 : The instructions known as the trap or system call handler read the details of the requested service and arguments, and then performs this request in kernel mode. Step 5 : After completion of system call the operating system resets the mode to user mode and returns from the system call. 2.7.1 Basic Features of Operating System Services The basic services of an operating system are process management, memory management/storage management, file management, I/O devices management, protection, security, and error detection etc. 2.7.2 Process Management

Process management includes: Process Process initiation Process control block Process scheduling Process synchronization Interprocess communication (IPC) Process: It is a program in execution. When a program is loaded into RAM (main memory) from ROM , it becomes a process. A program is a passive entity. A process is an active entity. A process needs certain resources such as CPU time, memory, files, and I/O devices to accomplish its task. These resources are allocated to the process either when it is created or while it is executing. ( figure 2.17 )

Figure 2.17 Process in memory The operating system creates a process and gave it to CPU. Now, CPU has executable code, local variable, static variable, global variable etc. and it can execute the process easily. Process initiation : Jobs are entered into the computer for execution, in many ways, which are: The user may submit the program by clicking on Internet Explorer or C/Java/any programming language program. The jobs may be created by the operating system itself. By, going to task manager -> see the process -> calculate address space Through system call as fork() ( figure 2.18 )

Figure 2.18 Fork()- P2 is controlled by P1 Login ( figure 2.19 )

Figure 2.19 Hierarchy of instance creation Process control block : Each process is represented in the operating system by a Process Control Block (PCB) i.e. every process has its different PCB and all PCB's are connected via a linked list. Note : PCB exists physically not logically in the operating system

Figure 2.20 Attributes of the process The attributes of a process, shown in figure 2.20 , are described as follows: Process ID : It recognizes the process uniquely. Process state : It depicts the current state of the process i.e. ready, running, block, terminate etc. Process priority : This field of process control block provides information about the most important process to be run according to the priority of the process. The process having highest priority will be executed first. Program counter : It stores the address of next instruction. When a partially executed process begins or resumes, it starts from where it was stopped rather than the beginning. Process address space : While modifying the data item, the address of the data segment (DS) must be known. The address of DS is told by physical address space (PAS). ( figure 2.21 )

Figure 2.21 Process address space Role of a link ( figure 2.22 ) :

Figure 2.22 Role of link Interprocess communication: A process within a system may be independent or cooperative. Suppose a process is independent, it is not affected by the execution of other processes. If it is a cooperative process, it can be affected by other executing programs. So, Interprocess communication (IPC) is a mechanism which allows processes to communicate with each other and to synchronize their actions. For inter-process communication, the process must be a cooperative process. Why we use IPC? IPC is required for cooperative processes for following purposes: Information sharing : The information may be shared between the processes. The process may be present in the system or in another system. They will be communicated with the help of networking.

Resource sharing : Along with the information, the cooperative processes also share resources within a system or outside the system. Computation speed up : The computation speed of the system is increased if we are using inter-process communication. Because resources are shared, so the system can also work with limited resources. Synchronization : It will help each cooperative process to communicate with each other in an efficient manner. Modularity : It increases modularity. 2.8 System Calls The operating system provides services via system calls . System calls are generally written in C and C++ , but certain low-level instructions may be written in assembly language. For example, copying a program from one file to another, will be done by making a number of system calls. Like opening file1, copying data from file1, opening file2, paste the data of file1 in file2. These all steps require different system calls. While performing these steps, there may occur an error like "file not found" while performing a paste operation. Again a number of system calls are needed. The error message flashed on the screen and program terminates. System calls may differ from operating system to operating system. There are three way to pass parameters to the operating system i.e. by using registers, blocks or tables and stack via push and pop operations. 2.8.1 Types of System Calls The categories of system calls are process management, file management, device management, information maintenance, and communications. Table 2.1 , explains the system calls. Table 2.1 Types of system calls Process management A running program may abort abnormally or end normally. In both cases, a message is generated. If there is an error, an error message flashed on the screen. The dump of memory is written on the disk and is examined by debugger to analyze and correct errors. In any condition, whether normal or abnormal, the operating system transfers control to command interpreter , to reads the next command. In an interactive system , the command interpreter starts with the next command by assuming that the user will issue a fresh command to respond the error message. In the GUI system , the pop window might alert the user and ask for further proceeding. In batch systems , the command interpreter terminates the entire job and starts with the fresh one. File management For any system call, there exist a file and every system call require the name of the file and its attributes. To record or write something on file, the file needs to be opened and must be closed after completion of a read or write operations. To access the attribute of files, the system calls use " get attribute" and " set attribute ". Device management To execute the instructions, a process may need several resources like memory, disk drives, file access etc. If these resources are available, they can be allotted immediately to the demanding process, otherwise, the process has to wait till the other processes release the resources. If there are n number of users, and all of them demand resources at the same time, then the operating system needs to apply a certain kind of scheduling algorithm to avoid deadlock . Information maintenance A lot of system calls are there to provide the maintenance for information of the system like get time, set time are related to the system clock , get data, and set data give the data stored on disk/memory. The operating system also keeps the record of processes running on the operating system. Communication Communication is somewhat exchanging the messages or information. Communication may be local or remote. To communicate with other computer or process, first, the connection must be created. For this purpose, the " create connection " system call is used. Once the connection is established, the messages are sent and received by " send messag e" and "receive message" system calls respectively. Information transfer status can be checked while transferring the message by using the "transfer status" system call. After the completion of communication, the connection needs to be closed. It is done by using "delete connection" system call. The “Attach remote device” and “detach remote device” system calls are used to attach and detach with the remote devices. The communication can be done by simply “message passing” or using “shared memory ” method.

2.9 System Programs System programs are necessary for the working of a computer. For program development and execution, system programs provide a convenient environment. System programs are the interface between the user and system calls. The categories of system programs are: File maintenance: The system programs under this category are used to create, delete, rename, print, sort, modify files, and directories. File modification: To create and modify the file, editors are used. By using these editors, the changes are made and saved back. The contents of the file can be searched by using the search option. Program loading and execution: After compilation, the program is loaded into memory before execution. The types of loaders include absolute loaders, relocatable loaders, linkage editors, and overlay loaders. While compiling, if there occurs an error, then debuggers are available to debug the program. System status information: There are system programs which tell the status of the computer like memory available, memory usage, date, time, system performance etc. If any error occurs at any point of time, the system program flashed the message on the screen to alert the user. Programming language support: To support the various programming languages, the system programs like compilers, assemblers, interpreters, and debuggers are provided to the user with the operating system. Communication: The system programs used for communication provide the mechanism for creating connections for communications. These programs allow users to browse web pages, send emails, to log on remotely, and transfer a file from one computer to another. Along with the system programs, application programs or system utilities are also provided by operating system like a word processor, web browsers, compilers, text formatters, spreadsheets, etc. 2.10 Interrupts Interrupts are the signals generated that enable software to respond to signals from hardware. In response to each type of interrupt, a specific set of instruction called an interrupt handler is to be executed. Interrupts may be synchronous and asynchronous . Synchronous interrupts are the interrupts generated as a result of executing a process's instruction. These are often called trap . Example: divides by zero. Asynchronous interrupts are the interrupts generated as a result of some event that is unrelated to process's current instruction. Example: interrupts generating from pressing any key from keyboard or movement of the cursor from the mouse. Interrupt generates low overhead and replaces the polling mechanism which generates high overhead. 2.10.1 Interrupt Processing An electrical connection between processor and mainboard (interrupt line) becomes active. The devices like timer controllers and peripheral cards send signals that activate the interrupt line to inform the processor that an event has occurred. Interrupt controllers in processors send the interrupt to a processor in priority order so that high priority interrupts are serviced first. After the activation of interrupt line, the processor executes the current instruction but pauses the current process's execution by saving the current state of a process so that it can be resumed from the correct place. The processor then passes control to the appropriate interrupt handler. Interrupt vector is an array of pointers to interrupt handlers which stores unique value corresponding to each type of interrupt. Interrupt handler perform respective action according to interrupt generated. After completion, the state of an interrupted process is restored. ( figure 2.23 )

Figure 2.23 Interrupt procedure 2.10.2 Types of Interrupt

The interrupts are classified as hardware and software interrupt . The hardware interrupts are further categorized into maskable, spurious, nonmaskable, and inter-process interrupts. Figure 2.24 describes the types of interrupts.

Figure 2.24 Classification of interrupt Hardware interrupt The hardware interrupt results in stopping the current execution of processor by saving its current state of execution and start execution of interrupt handler. Maskable : Maskable interrupts are the kind of hardware interrupts and can be ignored by the processor. These interrupts are generally generated by peripheral devices. Example: mouse-click, memory read etc. Non-maskable : Non-maskable interrupts are also called hardware interrupts but cannot be ignored by the processor. Examples: power failure, software corruption. Inter-process interrupt : Inter-process interrupt are generated in multiprocessors. Example: one processor interrupts another processor. Spurious interrupt : Spurious interrupt is a type of hardware interrupt and it is unwanted. These interrupts are usually generated by system conditions such as electrical interference on interrupt line or because of incorrectly designed hardware. Software Interrupt Software interrupts are generated by executing an instruction. These are generally used to implement system calls. System calls will be discussed in the next chapter . Review Questions A server cannot be a client. (True/False) Name the technologies which are used to bridge the gap between different operating systems? How these technologies make possible to execute the same application on multiple platforms? Why distributed computing is useful for computations performed by the mobile devices? What type of system a temperature monitor in a nuclear power plant be described as? Why? How does device driver support to contribute the extensibility property of operating system? What is the major difference between networked and distributed operating system? What is the primary advantage of a distributed operating system? Why operating system is more difficult to design today than 50 years ago? How do drivers and interfaces such as plug and play facilitate operating system extensibility? What problems may arise if one component on a bus has an extremely high multiplier and another component on a same bus has an extremely high divider? How do expectations differ from other type of interrupts?

How does an interval timer prevent one process from monopolizing a processor? How does linking facilitate the development of large programs built by many developers? What is the one possible drawback and benefit of using a dynamic linker? How absolute loading limits a system degree of multiprogramming? How dynamic loading improves a system’s degree of multiprogramming? How interactive computing and its improvement in turnaround time increase programmer productivity? How processes issue system calls to request operating system services? CHAPTER 3 Process Management In this chapter, we are going to learn the process, different stages of a process, process scheduling, and scheduling algorithms. 3.1 Introduction A process is a program in execution. The process is an entity i.e. every process has its own address space which consists of: Stack region: Instructions and local variables for active procedure calls are stored in the stack region. Text region: The text region contains the code of the executing process. Data region: The dynamically allocated memory and variables used by a process during execution are stored in a data region. The local variables and instructions for active procedure call are stored in the stack region, a data region and text region. The stack contents expand as the process issues nested procedure calls and shrink as the called procedures return. The program is a passive entity and process is an active entity. 3.1.1 Process Control Blocks/Process Descriptors When a process is created, the operating system should be able to identify the process. Therefore, the Process Identification numbers (PID) are given to the processes. Operating systems also create a Process Control Block (PCB)/Process Descriptors that contain the information required by the operating system to manage the processes. The information contained by PCBs is PID, process state, program counter, scheduling, priority, address space, parent process, child process, flags, and execution content. The operating system maintains pointers to each PCB corresponding to the particular process ID. ( figure 3.1 )

Figure 3.1 Process descriptors

3.1.2 Process Operations There are various process operations performed by the operating system. ( figure 3.2 ) These are: Creating process: The new process may be created by the operating system or the process itself. If the process creates a new process, then the process which is producing a sub process is called a parent process and the produced process is known as a child process. Destroying process: Destroying a process means removing a process from the system. If a process has child processes then destroying a process is little complicated. Operating system destroy child processes when a parent is deleted, in case if child process is not deleted on deleting parent process then first destroy the child processes then remove the parent process. Resuming/waking up the process: Resuming means ready to execute again on the completion of some I/O event. Suspending/Blocking process: Suspending a process means to deallocate the process from processor either because of timeout or the process exit voluntarily for some event i.e. blocking itself. There are two substages- suspended ready and suspended blocked. Changing the process's priority: To change the priority of a process, the priority value in PCB should be changed or modified. Interprocess communication: A process within a system may be independent or cooperative. A process is independent, if it is not affected by the execution of other processes. If it is a cooperative process, it can be affected by other executing programs. Then Interprocess Communication (IPC) came in the picture. Interprocess Communication is a mechanism which allows processes to communicate with each other and synchronize their actions. For inter-process communication, the process must be a cooperative process.

Figure 3.2 Process operations 3.1.3 Context Switching Context switching is performed by the operating system to stop executing the current running process and start executing a previously ready process. When the interrupt occurs, the kernel saves the current state before starting the execution of the next one. ( figure 3.3 )

Figure 3.3 Context switching Context switching is a complete overhead. During context switching, the processor cannot perform any useful task. Therefore, it should be minimized. Question 3.1: Let the time taken to switch from user mode to kernel mode is t1, from kernel mode to user mode is t2, and time taken to switch to processes be t3. Which of the following is true? t3>t1>t2 t3> t2>t1 t3>t1≥t2 t3>t1≤t2 Answer: (c) Solution : The context of the old process is saved by the kernel in the PCB of this process on the occurrence of the context switch. It loads the saved context of the new process which is scheduled to run. The mode switching will be less than the context switch time as the mode switching is a part of the context switch. Alternatively, whenever a context switch occurs among the two user processes then there will be changes in two modes, one from user to monitor and second from monitor to a user. To switch from user to kernel mode, we need system call but from kernel to user we do not need to interrupt. Therefore, the user to kernel mode is slower than the kernel to the user. Though both involve change mode bit so, t1≥t2 or t1>t2. 3.1.4 RPC and Client-Server Systems The client-server paradigm is used in structuring system services in a distributed system. If we are using a file server in a local area network every day, we are using a client-server system when we are accessing a remote file server . The Remote Procedure Call (RPC) is a mechanism that is used in building this kind of client-server relationship in a distributed system. ( figure 3.4 ) What if the client and the server are on the same machine? It will also not be a good way to structure the relationship between the client and servers using RPC. Even if the clients and the servers happen to be on the same machine, it seems logical to structure client of a system even on the same machine using this RPC paradigm. But the main concern is the performance and the relationship between performance and safety. Safety, about which we have talked a lot while discussing operating system structures, you have to make sure that Servers and Clients are in different address spaces or different protection domain . Even if they are on the same machine, they will be running on different processors of an SMP , but they are still on the same machine. You have to only give them the separate protection domain for each of the servers. It we focus only on safety, there is going to be a hit on performance. Because of the fact that an RPC has to go across the outer spaces, a client is on particular outer space and the server is on different outer space. So, there is going to be a performance penalty. Now as operating system designers, what we would like to be able to do is to make RPC calls across protection domains as efficient as normal procedure call that is happening inside a given process. If you could make the RPC across protection domains as efficient as a normal procedure call, it would encourage system designers

to use RPC as a vehicle for structuring services even within the same machine. Is that a good idea? Every service must be provided with its own protection domain. What that means is to go across these protection domains, a protected procedure call or RPC call is made from one protection domain to another protection domain. And that is going to be more expensive than a simple procedure call. It won't encourage system designers to use the separate protection domains to provide the services independently.

Figure 3.4 Client-server paradigm 3.2 Process States: Life Cycle of a Process Processes should be able to communicate with the operating system to perform different tasks. Services which operating system provides to processes are: Creating and destroying the process Suspending and resuming processes Blocking and waking up the processes Dispatching processes Changing the priority of process During execution, the process changes its state according to the current activity ( figure 3.5 ) . A process may be in various states such as: New: A process is being produced Running: The instructions are executing on the processor Ready: The process is waiting for a processor to be assigned Blocked: The process is waiting for some event to happen like I/O completion, to proceed further

Figure 3.5 Process life cycle The ready list of ready processes and blocked list of blocked processes are maintained by the operating system. The processes are listed

according to priority in the ready list. The processes in a blocked list are queued randomly according to the process which blocked first for some event to occur. 3.2.1 Process State Models 5-STATE MODEL

Figure 3.6 5-State model New state: Process is being created. When the process enters into a computer, it will keep into a new state. The process is brought to RAM from secondary memory. Running: The process is executing in the CPU. Blocked/waiting: In waiting state, the process will be waiting for some event to happen like signal reception or an I/O completion. Ready: The process is waiting in the ready queue to be allotted to a processor. Terminate: The process has completed its execution. Note : PCB exist physically not logically in the operating system 6-STATE MODEL The process can also be viewed in the 6-state model ( figure 3.7 ).

Figure 3.7 6-State model Note : When jobs go to disk then only Code Segment (CS), Data Segment (DS), Stack Segment (SS), heap goes to disk. PCB still resides in RAM only. 5-state model or 6-state model are general, some operating systems have a direct connection from the new state to disk. State model depends upon the designer. 3.2.2 Process State Transitions

Process state transition means moving a process from one state to another. For example, a process in the ready queue is allocated to the processor, and then its state changes from ready to running. This procedure is called dispatching of the process. It is performed by the system entity called the dispatcher . To prevent the processor from monopolizing one process, the timer is set by an operating system which allows one process to execute for a specific time period called quantum . As the quantum of the process expires, the operating system takes over the control, exit the running process forcefully and queue it up in the ready queue. Then the next process in the ready queue is allocated to a processor. If a process exits voluntarily for some I/O event that process is called blocked . The processes in a ready and running state are called awake as they can be executed as soon as processor goes available. The processes in a blocked state are called asleep as they cannot be executed even if the processor is available. There are six possible state transitions: new to ready, ready to running, running to ready, running to blocked, blocked to ready and running to terminate. 3.3 FORK() System Call FORK() System Call is a system call, which creates the child process. It is used for concurrent programming. ( figure 3.8 )

Figure 3.8 Fork system call Creating a new process using fork system call requires a context switch i.e. we need to switch from user mode to system mode. But, it is an overhead that is why thread comes into the picture. When the child process is created by using the fork system call, both parent and child will have: Relative address: Same for both parent and child process. Absolute address: Different for both parent and child process. Example 3.1 : The process executes the code Inst1- main { Inst2- fork(); Inst3- fork(); Inst4- fork(); Inst5} The total number of child processes created are: (a) 6 (b) 7 (c) 8 (d) 9

Out of n fork system calls, the number of child processes will be 2 n -1. In this example, fork system call is made thrice. If a fork() is called n times, then the total running processes will be 2 n including the parent process. Therefore, total number of child processes will be 2 n-1 . Therefore, the number of child processes will be 2 ³ -1 = 7. Example 3.2: Inst1- main () { Inst2- print ‘1’; Inst3- fork(); Inst4- print ‘2’; Inst5- fork(); Inst5- print ‘3’; } What will be the output of above program? Solution: Answer: 1 2 2 3 3 3 3 Return type of fork() { Int i= fork (); Three possibilities: Case I : if (fork()==0 i.e. i=0) { // this code will be executed by child process i.e. fork() return the value zero to the newly created child process. }

Case II : if (fork() != 0 i.e. i!=0) i.e. when i>0 { // this code will be executed by parent process i.e. the process id of the child process returned to the parent process. } Case III : if (fork()t1>t2 t3> t2>t1 t3>t1≥t2 t3>t1≤t2 Answer: (c) Solution : The context of the old process is saved by the kernel in the PCB of this process on the occurrence of the context switch. It loads the saved context of the new process which is scheduled to run. The mode switching will be less than the context switch time as the mode switching is a part of the context switch. Alternatively, whenever a context switch occurs among the two user processes then there will be changes in two modes, one from user to monitor and second from monitor to a user. To switch from user to kernel mode, we need system call but from kernel to user we do not need to interrupt. Therefore, the user to kernel mode is slower than the kernel to the user. Though both involve change mode bit so, t1≥t2 or t1>t2. 3.1.4 RPC and Client-Server Systems The client-server paradigm is used in structuring system services in a distributed system. If we are using a file server in a local area network every day, we are using a client-server system when we are accessing a remote file server . The Remote Procedure Call (RPC) is a mechanism that is used in building this kind of client-server relationship in a distributed system. ( figure 3.4 ) What if the client and the server are on the same machine? It will also not be a good way to structure the relationship between the client and servers using RPC. Even if the clients and the servers happen to be on the same machine, it seems logical to structure client of a system even on the same machine using this RPC paradigm. But the main concern is the performance and the relationship between performance and safety. Safety, about which we have talked a lot while discussing operating system structures, you have to make sure that Servers and Clients are in different address spaces or different protection domain . Even if they are on the same machine, they will be running on different processors of an SMP , but they are still on the same machine. You have to only give them the separate protection domain for each of the servers. It we focus only on safety, there is going to be a hit on performance. Because of the fact that an RPC has to go across the outer spaces, a client is on particular outer space and the server is on different outer space. So, there is going to be a performance penalty. Now as operating system designers, what we would like to be able to do is to make RPC calls across protection domains as efficient as normal procedure call that is happening inside a given process. If you could make the RPC across protection domains as efficient as a normal procedure call, it would encourage system designers

to use RPC as a vehicle for structuring services even within the same machine. Is that a good idea? Every service must be provided with its own protection domain. What that means is to go across these protection domains, a protected procedure call or RPC call is made from one protection domain to another protection domain. And that is going to be more expensive than a simple procedure call. It won't encourage system designers to use the separate protection domains to provide the services independently.

Figure 3.4 Client-server paradigm 3.2 Process States: Life Cycle of a Process Processes should be able to communicate with the operating system to perform different tasks. Services which operating system provides to processes are: Creating and destroying the process Suspending and resuming processes Blocking and waking up the processes Dispatching processes Changing the priority of process During execution, the process changes its state according to the current activity ( figure 3.5 ) . A process may be in various states such as: New: A process is being produced Running: The instructions are executing on the processor Ready: The process is waiting for a processor to be assigned Blocked: The process is waiting for some event to happen like I/O completion, to proceed further

Figure 3.5 Process life cycle The ready list of ready processes and blocked list of blocked processes are maintained by the operating system. The processes are listed

according to priority in the ready list. The processes in a blocked list are queued randomly according to the process which blocked first for some event to occur. 3.2.1 Process State Models 5-STATE MODEL

Figure 3.6 5-State model New state: Process is being created. When the process enters into a computer, it will keep into a new state. The process is brought to RAM from secondary memory. Running: The process is executing in the CPU. Blocked/waiting: In waiting state, the process will be waiting for some event to happen like signal reception or an I/O completion. Ready: The process is waiting in the ready queue to be allotted to a processor. Terminate: The process has completed its execution. Note : PCB exist physically not logically in the operating system 6-STATE MODEL The process can also be viewed in the 6-state model ( figure 3.7 ).

Figure 3.7 6-State model Note : When jobs go to disk then only Code Segment (CS), Data Segment (DS), Stack Segment (SS), heap goes to disk. PCB still resides in RAM only. 5-state model or 6-state model are general, some operating systems have a direct connection from the new state to disk. State model depends upon the designer. 3.2.2 Process State Transitions

Process state transition means moving a process from one state to another. For example, a process in the ready queue is allocated to the processor, and then its state changes from ready to running. This procedure is called dispatching of the process. It is performed by the system entity called the dispatcher . To prevent the processor from monopolizing one process, the timer is set by an operating system which allows one process to execute for a specific time period called quantum . As the quantum of the process expires, the operating system takes over the control, exit the running process forcefully and queue it up in the ready queue. Then the next process in the ready queue is allocated to a processor. If a process exits voluntarily for some I/O event that process is called blocked . The processes in a ready and running state are called awake as they can be executed as soon as processor goes available. The processes in a blocked state are called asleep as they cannot be executed even if the processor is available. There are six possible state transitions: new to ready, ready to running, running to ready, running to blocked, blocked to ready and running to terminate. 3.3 FORK() System Call FORK() System Call is a system call, which creates the child process. It is used for concurrent programming. ( figure 3.8 )

Figure 3.8 Fork system call Creating a new process using fork system call requires a context switch i.e. we need to switch from user mode to system mode. But, it is an overhead that is why thread comes into the picture. When the child process is created by using the fork system call, both parent and child will have: Relative address: Same for both parent and child process. Absolute address: Different for both parent and child process. Example 3.1 : The process executes the code Inst1- main { Inst2- fork(); Inst3- fork(); Inst4- fork(); Inst5} The total number of child processes created are: (a) 6 (b) 7 (c) 8 (d) 9

Out of n fork system calls, the number of child processes will be 2 n -1. In this example, fork system call is made thrice. If a fork() is called n times, then the total running processes will be 2 n including the parent process. Therefore, total number of child processes will be 2 n-1 . Therefore, the number of child processes will be 2 ³ -1 = 7. Example 3.2: Inst1- main () { Inst2- print ‘1’; Inst3- fork(); Inst4- print ‘2’; Inst5- fork(); Inst5- print ‘3’; } What will be the output of above program? Solution: Answer: 1 2 2 3 3 3 3 Return type of fork() { Int i= fork (); Three possibilities: Case I : if (fork()==0 i.e. i=0) { // this code will be executed by child process i.e. fork() return the value zero to the newly created child process. }

Case II : if (fork() != 0 i.e. i!=0) i.e. when i>0 { // this code will be executed by parent process i.e. the process id of the child process returned to the parent process. } Case III : if (fork()