Anda di halaman 1dari 60

Introduc tion to Opera ting s ys tem, Process and threads | 1.

SYLLABUS
RAJASTHAN TECHNICAL UNIVERSITY Detailed Syllabus B.Tech. (Comp. Engg.) V-VI Sem. 2010-11

5CS5 OPERATING SYSTEMS (Common to Comp. Engg. & Info. Tech)

Units

Content Of the Subject


Introduction and need of operating system, layered architecture/logical structure of operating system, Type of OS, operating system as resource manager and virtual machine, OS services, BIOS, System Calls/Monitor Calls, Firmware- BIOS, Boot Strap Loader. Process management- Process model, creation, termination, states & transitions, hierarchy, context switching, process implementation, process control block, Basic System calls- Linux & Windows. Threads- processes versus threads, threading, concepts, models, kernel & user level threads, thread usage, benefits, multithreading models. Inter-process communication- Introduction to message passing, Race condition, critical section problem, mutual exclusion with busy waiting- disabling interrupts, lock variables, strict alteration, Petersons solution, TSL instructions, busy waiting, sleep and wakeup calls, semaphore, monitors, classical IPC problems. Process scheduling- Basic concepts, classification, CPU and I/O bound, CPU scheduler- short, medium, long-term, dispatcher, scheduling:- preemptive and non-preemptive, Static and Dynamic Priority, Cooperative & Non-cooperative, Criteria/Goals/Performance Metrics, scheduling algorithms- FCFS, SJFS, shortest remaining time, Round robin, Priority scheduling, multilevel queue scheduling, multilevel feedback queue scheduling, Fair share scheduling. Deadlock- System model, resource types, deadlock problem, deadlock characterization, methods for Deadlock handling, deadlock prevention, deadlock avoidance, deadlock detection, recovery from deadlock. Memory management- concepts, functions, logical and physical address space, address binding, degree of multiprogramming, swapping, static & dynamic loading- creating a load module, loading, static & dynamic linking, shared libraries, memory allocation schemes- first fit, next fit, best fit, worst fit, quick fit. Free space management- bitmap, link list/free list, buddys system, memory protection and sharing, relocation and address translation. Virtual Memory- concept, virtual address space, paging scheme, pure segmentation and segmentation with paging scheme hardware support and implementation details, memory fragmentation, demand paging, pre-paging, working set model, page fault frequency, thrashing, page replacement algorithms- optimal, NRU, FIFO, second chance, LRU, LRU- approximation clock, WS clock; Beladys anomaly, distance string; design issues for paging system- local versus global allocation policies, load control, page size, separate instruction and data spaces, shared pages, cleaning policy, TLB ( translation look aside buffer) reach, inverted page table, I/O interlock, program structure, page fault handling, Basic idea of MM in Linux & windows. File System- concepts, naming, attributes, operations, types, structure, file organization & access(Sequential, Direct ,Index Sequential) methods, memory mapped files, directory structures one level, two level, hierarchical/tree, acyclic graph, general graph, file system mounting, file sharing, path name, directory operations, overview of file system in Linux & windows. Input/output subsystems- concepts, functions/goals, input/output devices- block and character, spooling, disk structure & operation, disk attachment, disk storage capacity, disk scheduling algorithm- FCFS, SSTF, scan scheduling, C-scan schedule.

1. 3 |Opera ting S ys tem Concepts

Objectives
Introduction to OS, Layered architecture Types of OS and OS services BIOS, System Calls and Firmware Process Management Model Process Implementation PCB and Basic System Calls (Unix & Windows) Threads Concepts and models Types of threads and thread usage Multithreading Architecture

CHAPTER- 1
Introduction to OS, Process management and Threads

Introduc tion to Opera ting s ys tem, Process and threads | 1.4

1.1

Introduction

This chapter describes about Operating system, its architecture, services, system calls etc. The concept of process management and threads are also described.

1.2
1.2.1

Operating System Concepts


Operating system definition

An Operating System acts as an intermediary between the user of a computer and the computer hardware. It is software that manages the computer hardware. Operating system is a collection of software routines, which acts as an interface between the user and the computer. The goals of an operating system are to provide an environment in which a user can execute programs in a convenient and efficient manner and to increase the efficiency of the computer system.

1.2.2

Need of Operating System

The first computer didnt have Operating systems; the programmer gave instructions directly to the CPU and as a consequence the CPU was idle for most of the time. An Operating system can overcome this problem by providing effective execution environment thus the need of an Operating system emerges. Operating system is like an interface between the user and machine hardware, which is responsible for managing various resources, handling system calls, executing privileged instructions and various other functions.

1.2.3

Layered architecture /Logical structure of Operating system

Layered architecture and logical structure of operating system are described below

Logical structure of Operating system (Abstract View):


As shown In the Fig1.1 Operating system acts as an intermediate controller program b/w computer hardware and user driven application programs. Various users are shown as USER 1, USER 2 etc and they operates upon various tools i.e. Compiler, assembler, text editor etc. Here compiler and assembler are system programs because system makes use of this program inherently for some specific purpose (generally they support execution of application programs) and Text Editor, database systems are Application program for performing a specific task as specified by user. Characteristics of logical structure of Operating system: OS is a layer of software, which takes care of technical aspects of a computers operations. It shields the user of the machine from the low level details of the machine operations and provides frequently needed facilities.

1. 5 |Opera ting S ys tem Concepts USER 1 USER 2 USER 3 ........ USER N

Compiler

Assembler

Text Editor

........

Database System

System and Application Programs Operating System (Intermediary b/w hardware and user) Computer Hardware

Fig.1.1: Abstract View of the components of a computer system (Logical Structure) OS as User View: The users view of the computer varies according to the interface being used. PC User: A typical computer user site in front of a PC, consisting of a monitor, keyboard, mouse and system unit (CPU).such systems are designed with a goal of maximizing the work performed by user. The OS is designed mostly for ease of use, with some attention paid to performance and none paid to resource utilization. Mainframe/ minicomputer User: a user sits at a terminal connected to a mainframe or minicomputer. Other users share resources and may exchange information , the OS in such cases is designed to maximize resource utilization to assure that all available CPU time, memory, and I/O are used efficiently and that no individual user takes more than his fair share. WorkStation User: users sit at workstations connected to networks of other workstations and servers. These users have dedicated resources at their disposal, but they also share resources such as n/w and servers (file & print server). Therefore the OS is designed to compromise b/w individual usability and resource utilization. Handheld computers: such devices are standalone units for individual users. Some are connected to networks, either directly by wire or through wireless modems and networking. Because of power, speed and interface limitations, they perform relatively few remote operations. Such OS are designed mostly for individual usability but performance per amount of battery life is important as well.

OS as System View: From the computers point of view, the OS is the program most closely involved with the hardware. Resource Allocator: a computer system has many resources that may be required to solve a problem: CPU time, memory space, file-storage space, I/O devices etc. The OS acts as the manager of these resources. When multiple conflicting requests occurs the OS must decides how to allocate them to specific programs and users so that it can operate the computer system efficiently and fairly.

Introduc tion to Opera ting s ys tem, Process and threads | 1.6

Control Program: here the OS acts as a controller program which control various I/O devices and user programs in order to prevent errors and improper user of the computer.

Layered Architecture of Operating system:


Dijkstra introduced the layered architecture for operating system when he developed an operating system named THE.

Operator
User Program Input/ Output Management Device Drivers for Operators console Memory (primary and secondary) Management Processor Allocation and Multiprogramming Bare Machine Hardware Fig.1.2: Operating System functions as layers

Timers, clocks, I/O Devices, Memory Chips etc

Shell & Command Interpreters CPU scheduling & Virtual Memory Kernel Interface to H/W

System Software (compiler, loader etc) Application Software (Word Processor)

End User Applications (Networked or Standalone)

Fig.1.3: Layered Architecture of Operating System

1. 7 |Opera ting S ys tem Concepts Above fig 1.3 depicts the Layered architecture of an operating system. The core part of a system is made of Hardware components like Timers, clocks, Memory Chips etc. this hardware is the actual part that causes some motion or real life activity. Above the hardware there is Operating system, which controls the hardware components by giving commands and executing software programs as specified by user. OS is the most important part of system as it ensures the reliability, efficiency and effectiveness of program execution. OS consist of 3 components: 1. Kernel which is the brain of an OS. Kernel handles all the controlling and monitoring activities. Kernel does not interact directly with End User instead it acts as an interface b/w hardware & shell. A kernel connects the application software to the hardware of a computer.

Application S/W

Kernel

Hardware

Fig.1.4: Kernel as interface b/w Application S/W & H/W 2. Virtual Memory is a concept that provides user an illusion of having large amount of memory available for process execution. Computer generates logical address of 32 bit which addresses virtual memory. Paging, segmentation etc are some concepts related to Virtual memory. 3. Shell/command interpreter acts as a command editor and executor. It provide interface between end user and internal parts of OS (Kernel). Shell provides an abstraction from the internal details by allowing user to just enter specific commands from a library and then showing the corresponding outcome. User interface can be in form of a Command Editor window or a Graphical user interface.

End User

Shell
Fig.1.5: Shell as interface b/w End user & Kernel

Kernel

Above the OS there are Software Utilities consisting of System software & application software. Application software is a set of program that is intended for a specific purpose. System softwares are more concerned with System helpers as they helps to execute application software. At the top level, various users with their applications make use of system. Advantages
Modularity Each layer uses functions and services of the its lower layer, hence better utilization and flow of information.

Disadvantages
Overhead of defining so much layers and protecting them from interfering to each others functions. Each layer adds its overhead, and this way chaining goes on, and overhead increases with number of layers.

Introduc tion to Opera ting s ys tem, Process and threads | 1.8

1.2.4

Responsibilities of Operating system

An operating system has three main responsibilities 1. Basic operation and monitoring: Operating system perform basic tasks, such as recognizing input from the keyboard, sending output to the display screen, keeping track of files and directories on the disk and controlling peripheral devices such as disk drives and printers. 2. Ensure protection while concurrent execution: Operating system also ensures that different programs and users running at the same time do not interfere with each other. 3. Provide software platform: Operating system provides software platform on top of which other programs (i.e. application softwares) can run.

1.2.5

Types of Operating System

Various types of operating systems are as follows.

Operating System

Real Time OS Multi-User v/s Single-User Multiprocessor OS

Distributed OS Multi-Tasking v/s Single-Tasking

Embedded OS

Embedded OS

Fig.1.6: Various Types of Operating System

1. Real Time Operating System (RTOS):


A real-time operating system is a multitasking operating system that aims at executing real-time applications. Real-time operating systems often use specialized scheduling algorithms so that they can achieve a deterministic nature of behavior. The main object of real-time operating systems is their quick and predictable response to events. They either have an event-driven or a time-sharing design. An event-driven system switches between tasks based on their priorities while time-sharing operating systems switch tasks based on clock interrupts. A real-time operating system (RTOS) is a multitasking operating system intended for applications with fixed deadlines (real-time computing). Such applications include some small embedded systems, automobile engine controllers, industrial robots, spacecraft, industrial control, and some large-scale computing systems. Key factors in a real-time OS are minimal interrupt latency and minimal thread switching latency.

1. 9 |Opera ting S ys tem Concepts

Real Time Operating System

Soft Real Time OS

Hard Real Time OS

Fig.1.7: Various Types of Real Time Operating System Real time systems are further classified into two categories:

Soft Real-Time System: No hard time limits. Used for applications like multimedia, simulations etc. Hard Real-Time System: Time limits are hard (deterministic). Specific OS required. For fast accessing & timeliness, data is stored solely onto volatile storage (ROM).

2. Embedded Operating System:


An Embedded system is a special purpose computer system designed to perform one or a few dedicated functions and usually embedded as a part of a complete device.
Software
Memory FPGA/ASIC Auxiliary System (power cooling)

Human interface A/D conversion

Processor

Diagnostic port D/A conversion

Sensor

Electromechanical backup and safety

Actuator

External Environment

Fig.1.8: Embedded Operating System Architecture

Introduc tion to Opera ting s ys tem, Process and threads | 1.10

These operating systems are designed to be compact, efficient, and reliable, forsaking many functions that non-embedded computer operating systems provide, and which may not be used by the specialized applications they run. Embedded operating systems are designed to be used in embedded computer systems. They are designed to operate on small machines like PDAs with less autonomy. They are able to operate with a limited number of resources. Example: Windows CE and Minix 3 An important difference between most embedded operating systems and desktop operating systems is that the application, including the operating system, is usually statically linked together into a single executable image. Unlike a desktop operating system, the embedded operating system does not load and execute applications. This means that the system is only able to run a single application. Fig1.8, based on [KOOP96], shows in general terms an embedded system organization. In addition to the processor and memory, there are a numbers of elements that differ from the typical desktop or laptop computer: o o o o o There may be a variety of interfaces that enable the system to measure, manipulate, and otherwise interact with the external environment. The human interface may be as simple as a flashing light or as complicated as real-time robotic vision. The diagnostic port may be used for diagnosing the system that is being controllednot just for diagnosing the computer. Special-purpose field programmable (FPGA), application specific (ASIC), or even non-digital hardware may be used to increase performance or safety. Software often has a fixed function and is specific to the application.

Most of the Embedded Systems are Real Time in nature but some embedded systems use operating systems such as Palm OS, BSD, and GNU/Linux, which do not support real-time computing. Embedded Operating Systems are used in ATM (Automatic teller machine), Sensor Toys, Security Cameras, and Automatic Washing Machines etc. Characteristics: Real-time operation Reactive operation Configurability I/O device flexibility Streamlined protection mechanisms Direct use of interrupts

3. Distributed Operating System:


A distributed operating system manages a group of independent computers and makes them appear to be a single computer. The development of networked computers that could be linked and communicate with each other, gave rise to distributed computing.

1. 11 |Opera ting S ys tem Concepts Distributed computations are carried out on more than one machine. When computers in a group work in cooperation, they make a distributed system.

Client

Client

Client

Client

Interconnection Network (Communication Channel)

Server

Fig.1.9: Distributed System model In distributed system there are multiple computers which communicate to each other through message passing. The fig1.9 shown above describes a typical distributed system. Multiple clients are connected to Communication n/w via bidirectional links. The Remote server is also connected to the same channel via a full duplex link.

Message Passing in Distributed System Using RPC:

Fig.1.10: Remote Procedure Call in Distributed System The above fig1.10 depicts the RPC process through which Message Passing is carried out in distributed systems. Stub is a copy of destination (Receiver) at the sender side which makes the Remote Procedures to be called as Local Procedures. The Stub at client is referred as Proxy of remote object and the stub at server side is referred as Skeleton of remote object.

Introduc tion to Opera ting s ys tem, Process and threads | 1.12

Characteristics: It makes use of distributing the computations among various physical processors. Message communication takes place on High speed Buses. Each processing element has its own local memory which is inaccessible to any other processing element. Such systems are referred as LOOSLY COUPLED SYSTEMS. Remote method invocation is a variant of RPC; it is used in Object Oriented Environment. For consistency purposes, Replication of data is performed in which multiple copies of dynamic data are floating through network. There is no global clock and no shared memory in Distributed systems. Major issues regarding Distributed systems are Synchronization, Concurrency and Security.

Advantages Fast & fault tolerant Transparent to user No single point of failure

Disadvantages Large Synchronization overhead Absence of global clock make synchronization more difficult
Absence of shared memory makes it difficult to manage consistency in system.

4. Multi-User v/s Single-User Operating System:


A multi-user operating system allows multiple users to access a computer system concurrently. Time-sharing system can be classified as multi-user systems as they enable a multiple user access to a computer through the sharing of time. Single-user operating systems, as opposed to a multi-user operating system, are usable by a single user at a time.

Windows OS
Administrator Student DB2Admin

UNIX OS
User 1 ID & PSWD User 2 ID & PSWD User 3 ID & PSWD

Single User System

Multi User System

Fig.1.11: Single User & Multi User OS Being able to have multiple accounts on a Windows operating system does not make it a multiuser system. Rather, only the network administrator is the real user. But for a Unix-like

1. 13 |Opera ting S ys tem Concepts operating system, it is possible for two users to login at a time and this capability of the OS makes it a multi-user operating system.

5. Multi-Tasking v/s Single Tasking Operating System:


When a single program is allowed to run at a time, the system is grouped under a single-tasking system, while in case the operating system allows the execution of multiple tasks at one time, it is classified as a multi-tasking operating system. Multi-tasking can be of two types namely, pre-emptive or co-operative. In pre-emptive multitasking, the operating system slices the CPU time and dedicates one slot to each of the programs. Unix-like operating systems such as Solaris and Linux support pre-emptive multitasking. Cooperative multitasking is achieved by relying on each process to give time to the other processes in a defined manner. MS Windows prior to Windows 95 used to support cooperative multitasking.

Time Slice

Fig.1.12: Time Slicing in Multi-Tasking System

Introduc tion to Opera ting s ys tem, Process and threads | 1.14

6. Network Operating System:


A networking operating system (NOS), also referred to as the Dialogue, is the software that runs on a server and enables the server to manage data, users, groups, security, applications, and other networking functions. The network operating system is designed to allow shared file and printer access among multiple computers in a network, typically a local area network (LAN), and a private network or to other networks. The most popular network operating systems are Microsoft Windows Server 2003, Microsoft Windows Server 2008, UNIX, Linux, Mac OS X, and Novell NetWare.

Characteristics:
Network Operating Systems are based on a client/server architecture in which a server enables multiple clients to share resources. Centrally manage network resources, such as programs, data and devices. Secure access to a network. Allow remote users to connect to a network. Allow users to connect to other networks like the internet. Back up data and make sure it's always available. Allow for simple additions of clients and resources. Monitor the status and functionality of network elements. Distribute programs and software updates to clients. Ensure efficient use if a server's capabilities.

Peer-to-Peer NOS:
In a Peer-to-peer network operating system users are allowed to share resources and files located on their computers and access shared resources from others. This system is not based with having a file server or centralized management source. A peer-to-peer network sets all connected computers equal; they all share the same abilities to utilize resources available on the network. Examples: AppleShare used for networking connecting Apple products. Windows for Workstations used for networking peer-to-peer windows computers. Advantages
Ease of setup Less hardware needed, no server needs to be purchased.

Disadvantages
No central location for storage. Lack of security that a client/server type offers.

Client/Server NOS:
Client/server network operating systems allow the network to centralize functions and applications in one or more dedicated file servers. The server is the center of the system, allowing access to resources and instituting security. The network operating system provides the mechanism to integrate all the components on a network to allow multiple users to simultaneously share the same resources regardless of physical location. Examples: Novell Netware, Windows Server

1. 15 |Opera ting S ys tem Concepts

Advantages
Centralized servers are more stable. Security is provided through the server. New technology and hardware can be easily integrated into the system. Servers are able to be accessed remotely from different locations and types of systems.

Disadvantages
Cost of buying and running a server are high. Dependence on a central location for operation.

Requires regular maintenance and updates.

7. Parallel Processing Operating System:


These systems are also known as Parallel Systems. In such systems there are several processors that are connected to each other via an interconnection network. Various resources like memory, I/O devices are shared among these processors. Any processor can access content of each others local memory directly. A control unit serves the purpose of controlling the parallelism by controlling access to global memory (made up of combination of all processors local memories) and controlling the interconnection network.

Global Memory Control Unit Control Memory

P1 LM1

P2 LM2

P3 LM3

P4 LM4

Host Interconnection Network

Fig.1.13: Shared memory based parallel processing System

Control Unit Control Memory

P1 LM1

P2 LM2

P3 LM3

P4 LM4

Host Message Passing Interface

Fig.1.14: Distributed memory based parallel processing System

Introduc tion to Opera ting s ys tem, Process and threads | 1.16

8. Multiprocessor Operating System:


Multiprocessing system can be modeled in two ways a) Symmetric Multiprocessing: In this system, each process runs identical copy of the operating system and these copies communicate with each other, as needed. The benefits of symmetric multiprocessing are that many processes can run at once, without decreasing performance. However we must be careful to control Input/Output to ensure that data reach the appropriate processor. Since the CPU are separate, on may be sitting idle while another is overloaded, resulting in inefficiency. To avoid this inefficiency the processor may share some data structures. A multiprocessor system of this form will allow jobs and resources to be shared dynamically among the various processors and can lower the variance among the system. b) Asymmetric Multiprocessing: Each processor is assigned a specific task. This scheme defines a master-slave relationship. A master processor controls the system while other processors either look to the master for instruction or have predefined tasks. The master processor schedules and allocates work to the slave processors. Asymmetric multiprocessing is more common in extremely large systems where one of the most time consuming activities is simply processing i/o. In older batch systems small processors located at some distance from the main CPU were used to run card readers and line printers and to transfer these jobs o and from the main computer. These locations are called Remote Job Entry (RJE), If the main CPU is interrupted for every character by every terminal it may spend all its time simply processing characters. So to avoid this situation, most systems have a separate fist-end processor that handles all the i/o. PE1 PE2 Master Processor Slave processor s PE1 PE1

PE3

PE4

PE1

PE1

(a)Symmetric multiprocessor system

(b) Asymmetric multiprocessor system

Fig.1.15: Symmetric and asymmetric multiprocessor system Advantages


Performance and computing power Fault tolerance Flexibility Functional specialization

Disadvantages
Design is complex Limitations of master-slave architecture Decreased reliability Cost performance low

1. 17 |Opera ting S ys tem Concepts

1.2.6

Operating System as Resource Manager and Virtual Machine

There are two different view of an operating system namely OS as a Resource Manager and OS as a Virtual Machine.

OS as Resource Manager:
Operating system controls various resources of system like Memory, I/O device, Processor, Secondary Storage etc. these resources are required by a program during its execution. If multiple programs are in execution then resource requirements can cause conflicts resulting into decreasing system utilization. Thus in order to avoid such conflicts a control program is needed which can monitor and manage all the resource allocation activities; here the Operating system comes into play. The job of the operating system is to provide for an orderly and controlled allocation of the processors, memories, and I/O devices among the various programs competing for them. Example:
Imagine what would happen if three programs running on some computer all tried to print their output simultaneously on the same printer. The first few lines of printout might be from program 1, the next few from program 2, then some from program 3, and so forth. The result would be chaos (ambiguous). The operating system can bring order to the potential chaos by buffering all the output destined for the printer on the disk. When one program is finished, the operating system can then copy its output from the disk file where it has been stored to the printer, while at the same time the other program can continue generating more output, oblivious to the fact that the output is not really going to the printer.

When a computer (or network) has multiple users, the need for managing and protecting the memory, I/O devices, and other resource is even greater, since the users might otherwise interfere with one another. In addition, users often need to share not only hardware, but information (files, databases, etc.) as well. In short, this view of the operating system holds that its primary task is to keep track of who is using which resource, to grant resource request, to account for usage, and to mediate conflicting requests from different program and users. Resource management includes multiplexing (sharing) resources in two ways: 1. Time Multiplexing 2. Space Multiplexing

Time Multiplexing:
When a resource is time multiplexed, different programs or users take turns using it. First one of them gets to use the resource, then another, and so on. For example, with only one CPU and multiple programs that want to run on it, the operating system first allocates the CPU to one program, and then after it has run long enough, another one gets to use the CPU, then another, and then eventually the first one again. Determining how the resource is time multiplexed who goes next and for how long is the task of the operating system. Another example of time multiplexing is sharing the printer. When multiple print jobs are queued up for printing on a single printer, a decision has to be made about which one is to be printed next.

Introduc tion to Opera ting s ys tem, Process and threads | 1.18

Space Multiplexing:
The other kind of multiplexing is space multiplexing. Instead of the customers taking turns, each one gets part of the resource. For example, main memory is normally divided up among several running programs, so each one can be resident at the same time (for example, in order to take turns using the CPU). Assuming there is enough memory to hold multiple programs, it is more efficient to hold several programs in memory at once rather than give one of them all of it, especially if it only needs a small fraction of the total. Of course, this raises issues of fairness, protection, and so on, and it is up to the operating system to solve them. Another resource that is space multiplexed is the (hard) disk. In many systems a single disk can hold files from many users at the same time. Allocating disk space and keeping track of who is using which disk blocks is a typical operating system resource management task.

OS as Virtual Machine:
We refer the term virtual for something which is not real. The fundamental idea behind a virtual machine is to abstract the hardware of a single computer (the CPU, memory, disk drives, network interface cards etc) into several different execution environments, thereby creating the illusion that each separate execution environment is running its own private computer. By using CPU scheduling and virtual-memory techniques, an operating system can create the illusion that a process has its own processor with its own (virtual) memory. Normally, a process has additional features, such as system calls and a file system that are not provided by the bare hardware. The virtual-machine approach does not provide any such additional functionality but rather provides an interface that is identical to the underlying bare hardware. Each process is provided with a (virtual) copy of the underlying computer.

Processes Processes
Programming Interface

Processes Processes

Kernel Hardware (a)

Kernel VM1

Kernel VM2 Hardware (b)

Kernel VM3

Virtual-machine implementation

Fig.1.16: System models: (a) Non-virtual v/s (b) Virtual Machine

1. 19 |Opera ting S ys tem Concepts There are several reasons for creating a virtual machine, all of which are fundamentally related to being able to share the same hardware yet run several different execution environments. A major difficulty with the virtual-machine approach involves disk system. Suppose that the physical machine has three disk drives but wants to support seven virtual machines. Clearly, it cannot allocate a disk drive to each virtual machine, because the virtual-machine software itself will need substantial disk space to provide virtual memory and spooling. The solution is to provide virtual disks, which are identical in all respect except size. Such virtual disks are called minidisks; system implements these disks by allocating as many tracks on the physical disks as the minidisk needs. Obviously, the sum of the sizes of all minidisks must be smaller than the size of the physical disk space available. Example: VMware & Java Virtual Machine The Fig1.16 above shows a comparison between Non-virtual and virtual machine architectures. In a nonvirtual system processes are controlled by kernel through an interface and kernel directly interact with the bare hardware of system. In virtual machine system, an intermediately layer governs various Virtual Machines and allows multiple processes to be executed with their own kernel. Here the kernel doesnt interact directly with the hardware of system; instead it is managed by Virtual Machine implementation.

1.2.7

Operating System Services

An operating system provides an environment for the execution of programs. It provides certain services to programs and to the users of those programs. The specific services provided differ from one OS to another, but we can identify common classes.

Operating System Services

Service provided to user

Service provided to system itself

User Interface Program Execution I/O Operations File-system manipulation Communications Error detection

Resource Allocation Accounting Protection & Security

Fig.1.17: Operating System Services

Introduc tion to Opera ting s ys tem, Process and threads | 1.20

As shown in above fig1.17 we can categorize the services offered by OS in two classes: 1. Services provided to user 2. Services provided to system itself

Services provided to user:


Following are the set of operating-system services provides functions that are helpful to the user:

1. User Interface: User interface is the way of communication between a machine and its user. User
accesses the machine through one of the User Interface available, to provide input and seek output from the machine. Almost all operating systems have a user-interface (UI). Various forms of UI are as below. Some systems provide two or all three of these variations. a) Command line interface (CLI): it uses text commands and a method for entering them. A special program is used to enter and edit these commands under certain privileges. i.e. The UNIX command to delete a file rm file.txt

b) Batch interface: in such interface to directives and commands are entered into files and those
files are then executed. The whole file or set of files in form of Batches are supplied to processor for command execution. c) Graphical User Interface (GUI): Here the interface is a window based system with a pointing device (such as mouse) to direct I/O, choose from menus, and make selections and a keyboard to enter text. Program Execution: The system must be able to load a program into memory and to run that program. The program must be able to end execution, either normally or abnormally (indicating error). I/O Operations: A running program may require I/O, which may involve file or an I/O device. For specific devices, special functions may be designed (such as recording to a CD or DVD drive or blanking a CRT screen). For efficiency and protection, users usually cannot control I/O devices directly. Therefore, the operating system must provide a means to do I/O. File-system manipulation: Programs need to read and write files and directories. They also need to create and delete them by name, search for a given file, and list file information. Some programs include permissions management to allow or deny access to files or directories based on file ownership. Communications: There are many circumstances in which one process needs to exchange information with another process. Such communication may occur between processes that are executing on the same computer or between processes that are executing on different computer systems tied together by a computer network. Communications may be implemented via shared memory or through message passing, in which packets of information are moved between processes by the operating system. Error detection: The operating system needs to be constantly aware of possible errors. Possible errors are:

2.

3.

4.

5.

6.

1. 21 |Opera ting S ys tem Concepts

a) Hardware Error: Errors may occur in the CPU memory hardware (such as a memory error or a power failure) I/O devices (such as a parity error on tape, a connection failure on a network, or lack of
paper in the printer)

b) User program Error: An arithmetic overflow An attempt to access an illegal memory location A too-great use of CPU time.
For each type of error, the operating system should take the appropriate action to ensure correct and consistent computing. Debugging facilities can greatly enhance the users and programmers attributes to use the system efficiently.

Services provided to system itself:


Another set of operating-system functions exist not for helping the user but rather for ensuring the efficient operation of the system itself. Systems with multiple users can gain efficiency by sharing the computer resources among the users.

1. Resource Allocation: when there are multiple users or multiple jobs running at the same time,
resources must be allocated to each of them with care. Many different types of resources are managed by the operating system such as main memory, I/O devices, file storage etc. Some of these resources require specific allocation code (such as CPU cycles, main memory, and file storage) while other may require general request/release code. 2. Accounting: We want to keep track of which users use how much and what kinds of computer resources. This records keeping may be used for accounting or simply for accumulating usage statistics. Usage statistics may be a valuable tool for researchers who wish to reconfigure the system to improve computing services. 3. Protection and Security: When several separate processes execute concurrently, it should not be possible for one process to interfere with the others or with the operating system itself. Protection involves ensuring that all access to system resources is controlled. Security of the system from outsiders is also important. Such security starts with requiring each user to authenticate himself or herself to the system, usually by means of a password, to gain access to system resources. It extends to defending external I/O devices, including modems and network adapters, from invalid access attempts and to recording all such connections for detection of break-ins. A chain is only as strong as its weakest link.

1.2.8

BIOS (Basic Input/ Output System)

The BIOS software is built into the PC, and is the first code run by a PC when powered ON. The primary function of the BIOS is to set up the hardware and load and start an operating system. When the PC

Introduc tion to Opera ting s ys tem, Process and threads | 1.22

starts up, the first job for the BIOS is to initialize and identify system devices such as the video display card, keyboard and mouse, hard disk, CD/DVD drive and other hardware. The BIOS then locates software held on a peripheral device, such as a hard disk or a CD, and loads and executes that software, giving it control of the PC. This process is known as booting, or booting up, which is short for bootstrapping. The BIOS is typically placed in a ROM chip that comes with the computer (it is often called a ROM BIOS). This ensures that the BIOS will always be available and will not be damaged by disk failures. It also makes it possible for a computer to boot itself. Because RAM is faster than ROM, though, many computer manufacturers design systems so that the BIOS is copied from ROM to RAM each time the computer is booted. This is known as shadowing. Many modern PCs have flash BIOS, which means that the BIOS have been recorded on a flash memory chip, which can be updated if necessary. BIOS also have a user interface (or UI for short). Typically this is a menu system accessed by pressing a certain key on the keyboard when the PC starts. In the BIOS UI, a user can: configure hardware set the system clock enable or disable system components select which devices are eligible to be a potential boot device Set various password prompts, such as a password for securing access to the BIOS UI functions itself and preventing malicious users from booting the system from unauthorized peripheral devices. PC BIOSes that can handle Plug-and-Play (PnP) devices are known as PnP BIOSes, or PnP-aware BIOSes. These BIOSes are always implemented with flash memory rather than ROM. The BIOS provides a small library of basic input/output functions used to operate and control the peripherals and these software library functions are callable by external software. The role of the BIOS has changed over time; today BIOS is a legacy system, superseded by the more complex Extensible Firmware Interface (EFI), but BIOS remains in widespread use, and EFI booting has only been supported in Microsoft's operating system products supporting GPT and Linux Kernels 2.6.1 and greater builds.

Fig.1.18: A Typical Phoenix - Award BIOS CMOS Setup Utility

1.2.9

System Calls/ Monitor Calls

In computing, a system call is how a program requests a service from an operating system's kernel that it does not normally have permission to run. System calls provide the interface between a process and the operating system. Most operations interacting with the system require permissions not available to

1. 23 |Opera ting S ys tem Concepts a user level process, e.g. I/O performed with a device present on the system, or any form of communication with other processes requires the use of system calls. These system calls are generally available as routines written in C and C++, although certain low-level tasks (e.g. tasks where hardware must be accessed directly), may need to be written using assembly-language instructions. Application developers design programs according to an application programming interface (API). The API specifies a set of functions that are available to application programmer, including the parameters that are passed to each function and the return values the programmer can expect. Three of the most common APIs available to application programmers are the Win32 API for Windows systems, the POSIX API for POSIX-based systems and the java API for designing programs that run on the Java virtual machine. A system call is invoked to perform certain privileged operations by switching context from user mode to kernel/supervisor mode. In user mode OS acts as a general purpose service provider and allows users to executer their programs (No privileged instructions are allowed to execute in this mode). While in supervisor mode OS allows only privileged instructions are executed.

User Application User mode Kernel mode


Open () { Implementation of open () system call

Open ()

System call interface

i
} Return

Fig.1.19: The handling of a user application invoking the open () system call In the fig1.19 above open () system call is described. The run-time support system for most programming languages provides a system-call interface that serves as a link to system calls made available by the operating system. The system-call interface intercepts function calls in the API and invokes the necessary system call within the operating system. Typically, a number is associated with each system call, and the system-call interface then invokes system call in the operating system kernel and returns the status of the system call and any return values.

Introduc tion to Opera ting s ys tem, Process and threads | 1.24

The caller needs to know nothing about how the system call is implemented or what is does during execution. Rather, it just needs to obey the API and understand what the operating system will do as a result of the execution of that system call.

Types of system calls: System calls can be divided into 5 categories.


Process control
End, abort Load, execute Create process, terminate process Get process attributes, set process attributes Wait for time Wait event, signal event Allocate and free memory

File management Create file, delete file Open, close Read, write, reposition Get file attributes, set file attributes Device management Request device, release device Read, write, reposition Get device attributes, set device attributes Logically attach or detach devices Information Maintenance Get time or date, set time or date Get system data, set system data Get process, file or device attributes Set process, file or device attributes Communications Create, delete communication connection Send, receive messages Transfer status information Attach or detach remote devices

Fig.1.20: Types of system calls

1.2.10 Firmware
Institute of Electrical and Electronics Engineers (IEEE) defines firmware as follows: "FIRMWARE is the combination of a hardware device and computer instructions and data that reside as read-only software on that device. This term is sometimes used to refer only to the hardware device or only to the computer instructions or data, but these meanings are deprecated. The confusion surrounding this term has led some to suggest that it be avoided altogether."

1. 25 |Opera ting S ys tem Concepts Firmware is programming that's written to the read-only memory (ROM) of a computing device. It is added at the time of manufacturing, is used to run user programs on the device. There are no clear boundaries between firmware and software & firmware and Hardware. Firmware which is more similar to software is called high level firmware while the firmware which emphasizes more on hardware is called low level firmware.

High level Firmware Firmware Low level Firmware

Software Hardware

Fig.1.21: Firmware as combination of software and hardware

Boot Strap Loader:


When a computer is first powered on, it usually does not have an operating system in ROM or RAM. The computer must execute a relatively small program stored in ROM along with the bare minimum of data needed to access the nonvolatile devices from which the operating system programs and data are loaded into RAM. The small program that starts this sequence of loading into RAM is known as a bootstrap loader, bootstrap or boot loader. This small boot loader program's only job is to load other data and programs which are then executed from RAM. Reset event on CPU (power up, reboot) causes instruction register to be loaded with a predefined memory location. It contains a jump instruction that transfers execution to the location of Bootstrap program. Often, multiple-stage boot loaders are used; during which several programs of increasing complexity sequentially load one after the other in a process of chain loading. In this context there are two types of Boot Strap Loader: A boot loader (or bootstrap loader) is a simple program that has the task to load another, usually more complex, program and transfer control to it This is a staged, sequential, process (i.e. Primary Bootstrap followed by Secondary Bootstrap) To start off this needs firmware (for instance BIOS) support The last step in the chain loads and executes the kernel of the desired operating system a) Primary Boot Strap Loader b) Second-Stage Boot Strap Loader

Introduc tion to Opera ting s ys tem, Process and threads | 1.26

Primary Boot Strap Loader: Run diagnostics to determine the state of machine. If diagnostics pass, booting continues. Runs a Power-On Self Test (POST) to check the devices that the computer will rely on, are functioning. Primary Boot Strap Loader (e.g. BIOS) goes through a preconfigured list of devices until it finds one that is bootable. If it finds no such device, an error is given and the boot process stops. Initializes CPU registers, device controllers and contents of the main memory. After this, it loads the OS. On finding a bootable device, the BIOS loads and executes its boot sector. In the case of a hard drive, this is referred to as the master boot record (MBR) and is often not OS specific. The MBR code checks the partition table for an active partition. If one is found, the MBR code loads that partition's boot sector and executes it. The boot sector is often operating system specific, however in most operating systems its main function is to load and execute a kernel, which continues startup.

Second- Stage Boot Strap Loader: If there is no active partition or the active partition's boot sector is invalid, the MBR may load a secondary boot loader and pass control to it and this secondary boot loader will select a partition (often via user input) and load its boot sector. Examples of secondary boot loaders o o o GRUB GRand Unified Bootloader (Linux) LILO LInux LOader (Linux) NTLDR NT LoaDeR (Windows)

1.3

Process Management

Process management is an integral part of any modern day operating system (OS). The OS must allocate resources to processes, enable processes to share and exchange information, protect the resources of each process from other processes and enable synchronization among processes. To meet these requirements, the OS must maintain a data structure for each process, which describes the state and resource ownership of that process and which enables the OS to exert control over each process.

1.3.1 Process Model


Process is one of the fundamental concepts in modern operating systems. Why do we need this concept and how this definition is obtained? The development of computer system made it introduced naturally. As we mentioned before, multiprogramming is designed to

1. 27 |Opera ting S ys tem Concepts improve efficiency by keeping the processor and I/O devices, simultaneously busy. Due to the gap between the speed of processors and I/O devices, a program that performs I/O operations have to wait for the completion of the operations. To avoid the idle of CPU, another program may be loaded into main memory and be given the control of CPU to execute. Thus at some moment, we may have the main memory layout illustrated by fig1.22. A process is defined as a program in execution; it is the unit of work in system. It provides an abstraction of a program to be executed. The main motive of using process is to achieve Pseudo parallelism.

Fig.1.22: Main memory layout for multiprogramming The programs that reside in main memory are absolutely different from their counter-parts, the program files on hard disks or tapes. The former are dynamic in the sense that they are running, while the latter are static. Thus we call processes these entities that have been loaded into main memory and are able to run or already in execution. If this is not convincing enough, lets consider an interesting case, suppose Process B and Process C in fig1.22 are different executions of a same program, say DEMO. Then obviously, to distinguish the two DEMOS, we definitely need a new term, instead of program, so the term process comes. Process model consist of: Program code Data on which Program code operates Resource required (i.e. files, I/O devices etc) Status of process execution (i.e. ready or running or blocked etc) A separate program counter value Process Stack to keep record of local variables, return addresses, temporary and subroutine parameters.

Introduc tion to Opera ting s ys tem, Process and threads | 1.28

Max

Stack

Heap DATA (Global) 0 Program Code

Fig.1.23: A typical process structure NOTE: Stack grows downwards when an element is pushed onto stack and it shrinks upwards when an element is popped from the stack. Heap grows upwards starting from the lowest possible address (0000). Both memory areas (i.e. stack & heap) are maintained carefully to avoid overlapping.

1.3.2 Process Creation


In general-purpose systems, some way is needed to create and terminate processes as needed during operation. There are four principal events that cause a process to be created:

System initialization. Execution of process creation system call by running a process. A user request to create a new process. Initiation of a batch job

When an operating system is booted, typically several processes are created. Some of these are foreground processes, that interacts with a (human) user and perform work for them. Others are background processes, which are not associated with particular users, but instead have some specific function. For example, one background process may be designed to accept incoming e-mails, sleeping most of the day but suddenly springing to life when an incoming e-mail arrives. Another background process may be designed to accept an incoming request for web pages hosted on the machine, waking up when a request arrives to service that request. Process creation in UNIX and Linux are done through fork() or clone() system calls. There are several steps involved in process creation. The first step is the validation of whether the parent process has sufficient authorization to create a process. Upon successful validation, the parent process is copied almost entirely, with changes only to the unique process id, parent process, and user-space. Each new process gets its own user space. After a process is created, both the parent and child have their own

1. 29 |Opera ting S ys tem Concepts distinct address space. If either process changes a word in its address space, the change is not visible to the other process. The fork() system call: It creates a new process as a child process of calling process( parent) Both child and parent process have similar code segments The child gets a copy of the parent data segment at the time of forking.

The process creation using fork() system call can be implemented in C/C++. The algorithm is given below:
ALGORITHM:

Step 1 2 3 4 5 6 7 8 9

Operation to be performed Start program. Assign fork() system call to pid. if pid is equal to -1, child process not created. if pid is equal to 0, child process will be created. Print the id of parent process and child process. Create another one child process in same loop Print id of parent process and the child process Print grandparent id. Stop the program.

fork() child

parent

resumes wait

exec()

exit()

Fig.1.24: A typical process creation

1.3.3 Process Termination


A process terminates when it finishes executing its last statement. Its resources are returned to the system, it is purged from any system lists or tables, and its process control block (PCB) is erased i.e., the PCB's memory space is returned to a free memory pool. 1. Normal termination o o o Process terminates when it executes its last statement Upon termination, the OS deletes the process Process may return data (output) to its parent

Introduc tion to Opera ting s ys tem, Process and threads | 1.30

2. Termination by another process o o o o Termination by the system call abort Usually terminated only by the parent of the process because Child may exceed the usage of its allocated resources Task assigned to the child is no longer required

3. Cascading termination o o Upon termination of parent process Initiated by the OS

1.3.4 Process States and Transitions


A process goes through its life cycle in which first it is created and then gets ready for execution, then finally executed and terminated successfully. During its life cycle a process goes through certain states. On the basis of number of states, there are three kinds of models (State-Transition model) for a given process. (a) Two State Model (b) Five State Model a) Two State Model: A process may be in one of the two states: Running or Not Running. When the operating system creates a new process, it enters that process into the system in the Not Running state.
Pause

New Process

Enter

Not Running

Running

Exit

Process Terminated

Dispatch (b) Two State Model for Process Execution

New Process

Enter

Queue

Dispatch
Processor

Exit

Process Terminated

Pause (a) Queuing Diagram

Fig.1.25: Two-State model for process execution and Queuing Diagram

1. 31 |Opera ting S ys tem Concepts b) Five State Model: Although the two-state process management model is a perfectly valid design for an operating system but using a single queue, the dispatcher could not just select the process at the oldest end of the queue. Rather, the dispatcher would have to scan the list looking for the process that is not blocked and that has been in the queue the longest. A more natural way to handle this situation is to split the Not Running state into two states: READY and BLOCKED. The absence of a BLOCKED state means that the processor lies idle when the active process changes from CPU cycles to I/O cycles. This design does not make efficient use of the processor. The three-state process management model is designed to overcome this problem, by introducing a new state called the BLOCKED state. This state describes any process which is waiting for an I/O event to take place. In this case, an I/O event can mean the use of some device or a signal from another process. The three states in this model are: 1. RUNNING: The process that is currently being executed. For this chapter we will assume a computer with a single processor, so at most one process at a time can be in this state. 2. READY: A process that is queuing and prepared to execute when given the opportunity. 3. BLOCKED: A process that cannot execute until some event occurs, such as the completion of an I/O operation. Apart from these three states there are two more states of a process: 4. NEW: A process that has just been created but has not yet been admitted to the pool of executable processes by the OS. Typically, a new process has not yet been loaded into main memory, although its process control block has been created. 5. EXIT: A process that has been released from the pool of executable processes by the OS, either because it halted or because it aborted for some reason. The NEW and EXIT states are useful constructs for process management. The NEW state corresponds to a process that has just been defined. For example, if a new user attempts to log onto a time-sharing system or a new batch job is submitted for execution, the OS can define a new process in two stages. First, the OS performs the necessary housekeeping chores. An identifier is associated with the process. Any tables that will be needed to manage the process are allocated and built. At this point, the process is in the NEW state. This means that the OS has performed the necessary actions to create the process but has not committed itself to the execution of the process. Similarly, a process exits a system in two stages. First, a process is terminated when it reaches a natural completion point, when it aborts due to an unrecoverable error, or when another process with the appropriate authority causes the process to abort. Termination moves the process to the EXIT state. At this point, the process is no longer eligible for execution. At any instant, a process is in one and only one of the five states. For a single processor computer, only one process can be in the RUNNING state at any one instant. There can be many processes in the READY and BLOCKED states, and each of these states will have an associated queue for processes. Processes entering the system must go initially into the READY state; processes can only enter the RUNNING state via the READY state. Processes normally leave the system from the RUNNING state. For each of the three states, the process occupies space in main memory.

Introduc tion to Opera ting s ys tem, Process and threads | 1.32

New

Admit

Dispatch Ready Timeout/ Interrupt I/O or Event Completion Blocked Running

Release

Terminated

I/O or Event Wait

Fig.1.26: Five-State model for process execution (State Transition Diagram) Fig 1.26 indicates the types of events that lead to each state transition for a process; the possible transitions are as follows: Null NEW A new process is created to execute a program. This event occurs for any of the reasons listed in PROCESS CREATION NEWREADY The OS will move a process from the NEW state to the READY state when it is prepared to take on an additional process. Most systems set some limit based on the number of existing processes or the amount of virtual memory committed to existing processes. This limit assures that there are not so many active processes as to degrade performance. READY RUNNING When it is time to select a process to run, the OS chooses one of the processes in the Ready state. This is the job of the scheduler or dispatcher using some predefined algorithm, such as FCFS, SJN, priority scheduling, SRT or round robin, to determine which process will get the CPU, when, and for how long. RUNNING READY The most common reason for this transition is that the running process has reached the maximum allowable time for uninterrupted execution; i.e. time-out occurs. Other reasons can be the imposition of priority levels as determined by the scheduling policy used for the Low Level Scheduler, and the arrival of a higher priority process into the READY state. RUNNING BLOCKED A process is put into the BLOCKED state if it requests something for which it must wait. A request to the OS is usually in the form of a system call, (i.e. a call from the running process to a function that is part of the OS code). For example, requesting a file from disk or a saving a section of code or data from memory to a file on disk. BLOCKED READY It is handled by the Process Scheduler and is initiated by signal from I/O device manager that I/O request has been satisfied and job can continue.

1. 33 |Opera ting S ys tem Concepts RUNNING EXIT The currently running process is terminated by the OS if the process indicates that it has completed, or if it aborts. READY EXIT or BLOCKED EXIT For clarity, this transition is not shown on the state diagram. In some systems, a parent may terminate a child process at any time. Also, if a parent terminates, all child processes associated with that parent may be terminated.

Fig 1.27 shows the transition of each process among the states. Fig 1.28 suggests the way in which a queuing discipline might be implemented with two queues: a Ready queue and a Blocked queue. As each process is admitted to the system, it is placed in the Ready queue. When it is time for the OS to choose another process to run, it selects one from the Ready queue. In the absence of any priority scheme, this can be a simple first-in-first-out queue. When a running process is removed from execution, it is either terminated or placed in the Ready or Blocked queue, depending on the circumstances. Finally, when an event occurs, any process in the Blocked queue that has been waiting on that event only is moved to the Ready queue. This latter arrangement means that, when an event occurs, the OS must scan the entire blocked queue, searching for those processes waiting on that event. In a large OS, there could be hundreds or even thousands of processes in that queue. Therefore, it would be more efficient to have a number of queues, one for each event. Then, when the event occurs, the entire list of processes in the appropriate queue can be moved to the Ready state (fig 1.29). One final refinement: If the dispatching of processes is dictated by a priority scheme, then it would be convenient to have a number of Ready queues, one for each priority level. The OS could then readily determine the highest-priority ready process that has been waiting the longest.
Process A Process B Process C Dispatcher

10

15

20

25

30

35

40

45

Ready

Running

Blocked

Fig.1.27: Process Sequencing & Dispatcher (No two processes can be in the same state at the same time)

Introduc tion to Opera ting s ys tem, Process and threads | 1.34

Time At t=0 At t=5 At t=10 At t=14 At t=19 At t=25 At t=30 At t=35 At t=40

Process A Running Ready Ready Ready Ready Ready Running Ready Ready

Process B Ready Ready Running Blocked Ready Blocked Ready Blocked Ready

Process C Ready Ready Ready Ready Running Ready Ready Ready Running

Dispatcher Empty (allotted to A) Full (No process is running) Empty (allotted to B) Full (No process is running) Empty (allotted to C) Full (No process is running) Empty (allotted to A) Full (No process is running) Empty (allotted to C)
Release

Admit

Ready Queue

Dispatch
Processor

Timeout Event Occurs Event Wait

Blocked Queue

Fig.1.28: Single Blocked Queue


Release Admit Ready Queue

Dispatch
Processor

Timeout Event1 Occurs Event1 Wait


Event1 Queue

Event2 Occurs

Event2 Wait
Even2 Queue


Event N Occurs


Event N Queue


Event N Wait

Fig.1.29: Multiple Blocked Queues

1. 35 |Opera ting S ys tem Concepts c) Modified Five State Model (with Two extra Suspend States): While the five state model is sufficient to describe the behavior of processes with the given events, we have to extend the model to allow for other possible events, and for more sophisticated design. In particular, the use of a portion of the hard disk to emulate main memory (so called virtual memory) requires additional states to describe the state of processes which are suspended from main memory, and placed in virtual memory (on disk). Of course, such processes can, at a future time, be resumed by being transferred back into main memory. The Medium Level Scheduler controls these events. A process can be suspended from the RUNNING, READY or BLOCKED state, giving rise to two other states, namely, READY SUSPEND and BLOCKED SUSPEND. A RUNNING process that is suspended becomes READY SUSPEND, and a BLOCKED process that is suspended becomes BLOCKED SUSPEND. A process can be suspended for a number of reasons; the most significant of which arises from the process being swapped out of memory by the memory management system in order to free memory for other processes. Other common reasons for a process being suspended are when one suspends execution while debugging a program, or when the system is monitoring processes. Various transactions in the modified model are: BLOCKED BLOCKED SUSPEND If a process in the RUNNING state requires more memory, then at least one BLOCKED process can be swapped out of memory onto disk. The transition can also be made for the BLOCKED process if there are READY processes available, and the OS determines that the READY process that it would like to dispatch requires more main memory to maintain adequate performance. BLOCKED SUSPEND READY SUSPEND A process in the BLOCKED SUSPEND state is moved to the READY SUSPEND state when the event for which it has been waiting occurs. READY SUSPEND READY When there are no READY processes in main memory, the OS will need to bring one in to continue execution. In addition, it might be the case that a process in the READY SUSPEND state has higher priority than any of the processes in the READY state. In that case, the OS designer may dictate that it is more important to get at the higher priority process than to minimize swapping. READY READY SUSPENDS Normally, the OS would be designed so that the preference would be to suspend a BLOCKED process rather than a READY one. This is because the READY process can be executed as soon as the CPU becomes available for it, whereas the BLOCKED process is taking up main memory space and cannot be executed since it is waiting on some other event to occur. However, it may be necessary to suspend a READY process if that is the only way to free a sufficiently large block of main memory. Finally, the OS may choose to suspend a lower-priority READY process rather than a higher-priority BLOCKED process if it believes that the BLOCKED process will be ready soon. RUNNING READY SUSPENDS If, however, the OS is preempting the process because a higher-priority process on the Blocked/Suspend queue has just become unblocked, the OS could move the RUNNING process directly to the READY SUSPEND queue and free some main memory.

Introduc tion to Opera ting s ys tem, Process and threads | 1.36

Fig.1.30: Process model with two suspend states

1.3.5 Process Hierarchy


The operating system can run many processes at the same time; in fact it only ever directly starts one process called the init (short for initial) process. This isn't a particularly special process except that its PID is always 0 and it will always be running. All other processes can be considered children of this initial process. Processes have a family tree just like any other; each process has a parent and can have many siblings, which are processes create by the same parent. Certainly children can create more children and so on and so forth. The term spawn is often used when talking about parent processes creating children; as in "the process spawned a child".
Main Process PID=0 (Parent) Child Process PID=1 Fork ( ) Child Process PID=3 (Grand Child) (Child) Child Process PID=2 Fork ( ) Child Process PID=5

Fork ( )

Fork ( )

Fork ( ) Child Process PID=4 Fork ( ) Child Process PID=6

Fig.1.31: Process Hierarchy

1. 37 |Opera ting S ys tem Concepts

1.3.6 Context Switching


A context switch is the computing process of storing and restoring state (context) of a CPU so that execution can be resumed from the same point at a later time. This enables multiple processes to share a single CPU. The context switch is an essential feature of a multitasking operating system. Context switches are usually computationally intensive and much of the design of operating systems is to optimize the use of context switches. A context switch can mean a register context switch, a task context switch, a thread context switch, or a process context switch. What constitutes the context is determined by the processor and the operating system. Switching from one process to another requires a certain amount of time for doing the administration - saving and loading registers and memory maps, updating various tables and list etc.

Context Switch Step When to switch?


In a switch, the state of the first process must be saved somehow, so that, when the scheduler to the execution of the first process, it can restore this state and continue. The state of the gets back Multitasking Most registers that the some scheduling scheme, one process needs to be switched process includes all the commonly, withinprocess may be using, especially the program counter, plus any out of system so another that may be necessary. This data is multitasking operating other operating the CPUspecific data process can run. Within a preemptive usually stored in a data system, the scheduler block every task to run for structure called a process controlallows(PCB), or switchframe. some certain amount of time, called its Intime slice.switch processes, the PCB for the first process must be created and saved. The PCBs order to If a process does notper-process yield the kernel memory (as by performing an I/O operation), are sometimes stored upon a voluntarily stack in CPU (for example, opposed to the user-mode stack), a timer interrupt fires, and the operating system structure another process for or there may be some specific operating system defined data schedules for this information. execution instead. This ensures that the CPU cannot the execution of by first process, it can now load Since the operating system has effectively suspended be monopolized the any one processor-intensive application. the PCB and context of the second process. In doing so, the program counter from the PCB is loaded, and thus execution can continue in the new process. New processes are chosen from a queue or Interrupt handling queues. Process and thread priority can influence which process continues execution, with processes of the highest priority checked first for ready threads to execute. Modern architectures are interrupt driven. This means that if the CPU requests data The act offrom a disk, for example, it does not needsomewhat machine-dependent. A generalissue the switching from one process to another is to busy-wait until the read is over; it can outline is: The OS gets control (either because othertimer interrupt or because the over, the CPU a system request and continue with some of a execution. When the read is process made can be call. interrupted and presented with the read. For interrupts, a program called an interrupt Operatingis installed, and it isinfo is updated (pointer to the current PCB, etc.) from the disk. handler system processing the interrupt handler that handles the interrupt Processor The kernel services the memory map and floating point state, etc) state is saved (registers, interrupts in the context of the interrupted process even This process may not have caused thequeue and the next process selected may have been though it is replaced on the ready interrupt. The interrupted process by the scheduling algorithm in user mode or in kernel mode. The kernel saves enough information so that it executing The new processs operating system and processorprocess and services the interrupt in kernel can later resume execution of the interrupted state is restored The new The kernel does not spawn process it looks like a process to handle interrupts.or as if an mode. process continues (to this or schedule a special block call has just returned interrupt service routine (not a signal handler) has just returned User and kernel mode safe and fast Context switches must be made as switching as possible. Safe because isolation must be maintained When a spent doing them is stolen from processes mode is required and fast because any time transition between user mode and kernel doing useful work.in an operating system, a context switch is not necessary; a mode transition is not by itself a context switch. However, depending on the operating system, a context switch may also take place at this time.

Introduc tion to Opera ting s ys tem, Process and threads | 1.38

1.3.7 Process Control Block


At any given point in time, while the program is executing, this process can be uniquely characterized by a number of elements, including the following:
Process Identifier: A unique identifier associated with this process, to distinguish it from all

other processes.
Process State: If the process is currently executing, it is in the running state. Program counter: The address of the next instruction in the program to be executed. Memory pointers: Includes pointers to the program code and data associated with this

process, plus any memory blocks shared with other processes. Context data/ CPU Registers: These are data that are present in registers in the processor while the process is executing. I/O status information: Includes outstanding I/O requests, I/O devices (e.g., tape drives) assigned to this process, a list of open files in use by the process, and so on. Accounting information: May include the amount of processor time and clock time used, time limits, account numbers, and so on. CPU Scheduling Information: it includes process priority, pointers to scheduling queues etc.

The information in the preceding list is stored in a data structure, typically called a process control block (fig 1.32), that is created and managed by the OS. The significant point about the process control block is that it contains sufficient information so that it is possible to interrupt a running process and later resume execution as if the interruption had not occurred. The process control block is the key tool that enables the OS to support multiple processes and to provide for multiprocessing. When a process is interrupted, the current values of the program counter and the processor registers (context data) are saved in the appropriate fields of the corresponding process control block, and the state of the process is changed to some other value, such as blocked or ready The OS is now free to put some other process in the running state. The program counter and context data for this process are loaded into the processor registers and this process now begins to execute. Thus, we can say that a process consists of program code and associated data plus a process control block. For a single-processor computer, at any given time, at most one process is executing and that process is in the running state.
Process ID Process State Program counter CPU Registers/Context Data Memory pointers List of open files I/O Status info Accounting info

Fig.1.32: Process Control Block

1. 39 |Opera ting S ys tem Concepts

Process Identification
Identifiers
Numeric identifiers that may be stored with the process control block include Identifier of this process Identifier of the process that created this process (parent process) User identifier

Processor State Information


User-Visible Registers
A user-visible register is one that may be referenced by means of the machine language that the processor executes while in user mode. Typically, there are from 8 to 32 of these registers, although some RISC implementations have over 100. Control and Status Registers These are a variety of processor registers that are employed to control the operation of the processor. These include Program counter: Contains the address of the next instruction to be fetched Condition codes: Result of the most recent arithmetic or logical operation (e.g., sign, zero, carry, equal, overflow) Status information: Includes interrupt enabled/disabled flags, execution mode Stack Pointers Each process has one or more last-in-first-out (LIFO) system stacks associated with it. A stack is used to store parameters and calling addresses for procedure and system calls. The stack pointer points to the top of the stack.

Process Control Information


Scheduling and State Information This is information that is needed by the operating system to perform its scheduling function. Typical items of information: Process state: Defines the readiness of the process to be scheduled for execution (e.g., running, ready, waiting, halted). Priority: One or more fields may be used to describe the scheduling priority of the process. In some systems, several values are required (e.g., default, current, highest-allowable). Scheduling-related information: This will depend on the scheduling algorithm used. Examples are the amount of time that the process has been waiting and the amount of time that the process executed the last time it was running. Event: Identity of event the process is awaiting before it can be resumed. Data Structuring A process may be linked to other process in a queue, ring, or some other structure. For example, all processes in a waiting state for a particular priority level may be linked in a queue. A process may exhibit a parent-child (creator-created) relationship with another process. The process control block may contain pointers to other processes to support these structures. Interprocess Communication Various flags, signals, and messages may be associated with communication between two independent processes. Some or all of this information may be maintained in the process control block. Process Privileges Processes are granted privileges in terms of the memory that may be accessed and the types of instructions that may be executed. In addition, privileges may apply to the use of system utilities and services. Memory Management This section may include pointers to segment and/or page tables that describe the virtual memory assigned to this process. Resource Ownership and Utilization Resources controlled by the process may be indicated, such as opened files. A history of utilization of the processor or other resources may also be included; this information may be needed by the scheduler.

Fig.1.33: Elements of Process Control Block

Introduc tion to Opera ting s ys tem, Process and threads | 1.40

1.3.8 Process implementation


The OS controls events within the computer system. It schedules and dispatches processes for execution by the processor, allocates resources to processes, and responds to requests by user processes for basic services. Fundamentally, we can think of the OS as that entity that manages the use of system resources by processes. If the OS is to manage processes and resources, it must have information about the current status of each process and resource. The universal approach to providing this information is straightforward: The OS constructs and maintains tables of information about each entity that it is managing. A general idea of the scope of this effort is indicated in Fig1.33, which shows four different types of tables maintained by the OS: memory, I/O, file, and process. Although the details will differ from one OS to another, fundamentally, all operating systems maintain information in these four categories.

Fig.1.34: General Structure of Operating System Control Tables 1) Memory tables are used to keep track of both main (real) and secondary (virtual) memory. Some of main memory is reserved for use by the OS; the remainder is available for use by processes. Processes are maintained on secondary memory using some sort of virtual memory or simple swapping mechanism.

1. 41 |Opera ting S ys tem Concepts The memory tables must include the following information: The allocation of main memory to processes The allocation of secondary memory to processes Any protection attributes of blocks of main or virtual memory, such as which processes may access certain shared memory regions Any information needed to manage virtual memory 2) I/O tables are used by the OS to manage the I/O devices and channels of the computer system. At any given time, an I/O device may be available or assigned to a particular process. If an I/O operation is in progress, the OS needs to know the status of the I/O operation and the location in main memory being used as the source or destination of the I/O transfer. 3) File tables: These tables provide information about the existence of files, their location on secondary memory, their current status, and other attributes. Much, if not all, of this information may be maintained and used by a file management system, in which case the OS has little or no knowledge of files. In other operating systems, much of the detail of file management is managed by the OS itself. 4) Process tables: it is used to manage processes.

Process Creation & PCB Elements: When a process is created, it is allocated a PCB that includes CPU Registers Pointer to Text (program code) Pointer to uninitialized data Stack Pointer Program Counter Pointer to Data Root directory Default File Permissions Working directory Process State Exit Status File Descriptors Process Identifier (pid) User Identifier (uid) Pending Signals Signal Maps Other OS-dependent information

These are some of the major elements that make up the process context. Although not all of them are directly manipulated on a context switch.

Introduc tion to Opera ting s ys tem, Process and threads | 1.42

1.3.9 Basic System Calls: Linux & Windows


System Calls In Linux:
System call is the services provided by Linux kernel. In C programming, it often uses functions defined in libc which provides a wrapper for many system calls. Manual page section 2 provides more information about system calls. To get an overview, use man 2 intro in a command shell. It is also possible to invoke syscall() function directly. Each system call has a function number defined in <syscall.h> or <unistd.h>. Internally, system call is invoked by software interrupt 0x80 to transfer control to the kernel. System call table is defined in Linux kernel source file arch/i386/kernel/entry.S .

Sr. No. 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20.

Function Name exit fork vfork read write open close waitpid execve getpid pause nice pipe acct idle clone mlockall munlockall capset capget

Description terminate the current process create a child process create a child process and block parent read from a file descriptor write to a file descriptor open a file or device close a file descriptor wait for process termination execute program get process identification suspend process until signal change process priority create an inter-process channel enable or disable process accounting make process 0 idle create a child process disable paging for calling process Re-enable paging for calling process set process capabilities get process capabilities

Source kernel/exit.c arch/i386/kernel/process.c arch/i386/kernel/process.c fs/read_write.c fs/read_write.c fs/open.c fs/open.c kernel/exit.c arch/i386/kernel/process.c kernel/sched.c arch/i386/kernel/sys_i386.c kernel/sched.c arch/i386/kernel/sys_i386.c kernel/acct.c arch/i386/kernel/process.c arch/i386/kernel/process.c mm/mlock.c mm/mlock.c kernel/capability.c kernel/capability.c

Fig.1.35: Basic Linux System calls in C Language

System Calls In Windows:


The Win32 API is the fundamental interface to the capabilities of NT. This section describes five main aspects of the Win32 API. The following table shows the system calls:

1. 43 |Opera ting S ys tem Concepts

Sr. no.

Function name

Description

Access to Kernel Objects


1 2 1 2 3 4 1 2 1 2 3 CreateXXX() CloseHandle() A process gains access to a kernel object named XXX by calling the CreateXXX function to open a handle to XXX. This handle is unique to that process. A process can close any handle by calling the CloseHandle function.

Process Management
CreateThread() SetPriorityClass() WaitForSingleObject() Or WaitForMultipleObjects() InitializeCriticalSection() PostMessage() SendMessage() VirtualAlloc() VirtualFree() VirtualLock() HeapCreate(), HeapAlloc(), HeapRealloc(), HeapSize(), HeapFree(),HeapDestroy(), HeapUnlock(), HeapLock(). Creates a thread To change the priority class (e.g. highpriority, low priority, realtimepriority etc) Provide thread synchronization Initializes critical section for thread synchronization It posts a message (asynchronously) It sends a message (synchronously)

Inter-Process Communication

Memory Management
Reserve or commit virtual memory De-commit or release the memory Lock pages into physical memory

Various heap management calls.

Fig.1.36: Windows Basic System calls

1.4

Threads
Thread is a single independent flow of execution within a process

A thread, sometimes called a lightweight process (LWP), is a basic unit of CPU utilization, and consists of a program counter, a register set, and a stack space. It shares with peer threads its code section, data section, and operating-system resources such as open files and signals, collectively known as a task.

1.4.1 Process v/s Thread


There are two main characteristics associated with process execution. Resource Allocation Process Execution/Dispatching

Introduc tion to Opera ting s ys tem, Process and threads | 1.44

An operating system can distinguish between a process and a thread based upon these characteristics. Process is referred to a single unit of Resource allocation, while Thread is referred to a single unit of process execution/dispatching. Both threads and processes are methods of parallelizing an application. However, processes are independent execution units that contain their own state information, use their own address spaces, and only interact with each other via inter-process communication mechanisms (generally managed by the operating system). Applications are typically divided into processes during the design phase, and a master process explicitly spawns sub-processes when it makes sense to logically separate significant application functionality. Processes, in other words, are an architectural construct. By contrast, a thread is a coding construct that doesn't affect the architecture of an application. A single process might contains multiple threads; all threads within a process share the same state and same memory space, and can communicate with each other directly, because they share the same variables. Threads typically are spawned for a short-term benefit that is usually visualized as a serial task, but which doesn't have to be performed in a linear manner (such as performing a complex mathematical computation using parallelism, or initializing a large matrix), and then are absorbed when no longer required. A process is the integration of many parallel executing threads which enables us to split a program into logically separate pieces. All these independent pieces do not interact with each other until required.

Per Process item


Address space Global variables Open files Child processes Pending alarms Signals and signal handlers o Accounting information o o o o o o o o o o

Per Thread item


Program counter Registers Stack State

NOTE: Threads are created only when the process is in running state.
Fig.1.37: Process and Thread items
Difference between process and threads are shown in following table:

SRN
1) 2) 3) 4) 5)

Process
Program in execution Heavy weight Each process has one or more thread Each process has its separate data and address space. In computing, a process is an instance of a computer program that is being sequentially executed by a computer system that has the ability to run several computer programs concurrently.

Thread
flow of control within process Light weight Each thread can belongs to only one process share global data and address space with other threads running in the same process A thread is an independent part of a process and acts as the entity scheduled for execution on the CPU.

1. 45 |Opera ting S ys tem Concepts


6) 7) 8) 9) 10) 11) 12) 13) 14) 15) 16) 17) 18)
A process, in the simplest terms, is an executing program, One or more threads run in the context of the process A process is a collection of code, memory, data and other resources A process is a subset of the program. Multiple process does not always insure true parallelism on multiprocessor system Processes have their own copy of the data segment of the parent process. Processes must use inter-process communication to communicate with sibling processes. Processes have considerable overhead (context switching). New processes require duplication of the parent process. Processes can only exercise control over child processes. Changes to the parent process do not affect child processes. Processes are expensive in terms of time & space Each process is started with a single thread, often called the primary thread, but can create additional threads from any of its threads. A process has o a virtual address space, o executable code, o open handles to system objects, o a security context, o a unique process identifier, o environment variables, o a priority class, o minimum and maximum working set sizes, o At least one thread of execution. A thread is the basic unit to which the operating system allocates processor time A thread is a sequence of code that is executed within the scope of the process A thread is a subset of the process. Multiple threads of control can exploit the true parallelism possible on multiprocessor systems Threads have direct access to the data segment of its proces Threads can directly communicate with other threads of its process Threads have almost no overhead New threads are easily created Threads can exercise considerable control over threads of the same process Changes to the main thread (cancellation, priority change, etc.) may affect the behavior of the other threads of the process Cheaper than a process Threads are interdependent because they share same address space within a process. All threads of a process share its virtual address space and system resources. In addition, each thread maintains o exception handlers, o a scheduling priority, o thread local storage, o a unique thread identifier, o A set of structures the system will use to save the thread context until it is scheduled. The thread context includes o the thread's set of machine registers, o the kernel stack, o a thread environment block, o A user stack in the address space of the thread's process. Threads can also have their own security context, which can be used for impersonating clients.

Fig.1.38: Difference between process & thread

1.4.2 Threading
Threading means introducing thread concept to execute processes in multiprocessor or uniprocessor system. When multiple threads in a single process exhibit process execution then this is called Multithreading. When there is one to one correspondence between a thread and a process (i.e. a process contains single thread) then this is called single threading or simply threading.

Introduc tion to Opera ting s ys tem, Process and threads | 1.46

Code Registers

Data

Files Stack

Code Registers

Data Registers

Files Registers

Stack

Stack

Stack

Thread Thread (a) Single-threaded process (b) Multithreaded process

Fig.1.39: (a) Single-threaded Process and (b) Multithreaded Process In the fig1.39 above two processes are shown, the process in Fig1.39 (a) is a single threaded process having a single thread of execution. This thread shares the address space allocated to the process and various other parameters like code, data and files. Thread also has some private data of its own like registers and a separate stack. Whereas the process shown in fig1.39 (b) is a process having multiple threads (3 threads) running independently. These threads share the address space, code, data and files. Also, each thread has its separate register and stack.

1.4.3 Thread Model


Thread is a flow of execution in a process. Process deals with resource grouping while thread deals with execution of instructions. The thread has a program counter that keeps track of which instruction to execute next. It has registers, which hold its current working variables. It has a stack, which contains the execution history, with one frame for each procedure called but not yet returned from. Although a thread must execute in some process, the thread and its process are different concepts and can be treated separately. Processes are used to group resources together; threads are the entities scheduled for execution on the CPU.

Fig.1.40: (a) Multiple processes each with one thread and (b) Multithreaded Process

1. 47 |Opera ting S ys tem Concepts Having multiple threads running in parallel in one process is analogous to having multiple processes running in parallel in one computer. In the former case, the threads share an address space, open files, and other resources. In the latter case, processes share physical memory, disks, printers, and other resources. Because threads have some of the properties of processes, they are sometimes called lightweight processes. The term multithreading is also used to describe the situation of allowing multiple threads in the same process. The fig1.40 gives a comparative idea about traditional process (having a single thread) and the multithreaded process. Thread Life Cycle: Like process, threads also have a life cycle in which they changes states during execution. There are two main components of thread life cycle: (a) Thread States and (b) Thread Transitions Thread states: A thread can be in one of the four states. 1. Ready thread is in ready queue and ready to be scheduled by dispatcher in order of priority. 2. Running thread is in execution continues execution until it is preempted by a higher priority thread, exhausts its time slice, blocks, or terminates. In the first two cases, it goes back to the ready state. 3. Block/Waiting thread is waiting on a I/O operation or any other event and placed in waiting queue. 4. Terminate thread has leaved the execution either successfully or abruptly with an error. Thread Transitions: Changing from one state to another is termed as transition. There are 4 main operations that are responsible for thread transitions.

Create Thread

Dispatch Ready Timeout/ Interrupt I/O or Event Completion Blocked Running

Release

Terminated

I/O or Event Wait

Fig.1.41: Thread State Transition Diagram (Thread Life Cycle) 1. Spawn Typically, when a new process is spawned, a thread for that process is also spawned. Subsequently, a thread within a process may spawn another thread within the same process, providing an instruction pointer and arguments for the new thread. The new thread is provided with its own register context and stack space and placed on the ready queue. 2. Block When a thread needs to wait for an event, it will block (saving its user registers, program counter, and stack pointers).The processor may now turn to the execution of another ready thread in the same or a different process.

Introduc tion to Opera ting s ys tem, Process and threads | 1.48

3. Unblock When the event for which a thread is blocked occurs, the thread is moved to the Ready queue. 4. Finish When a thread completes, its register context and stacks are de-allocated.

1.4.4 Some Other Thread Concepts


There are some more concepts related to thread as follows: 1. Thread Library: a thread library provides the programmer an API for creating and managing threads. There are two primary ways of implementing a thread library. The first approach is to provide a library entirely in user space with no kernel support. All code and data structures for the library exist in user space. This means that invoking a function in the library results in a local function call in user space and not a system call. The second approach is to implement a kernel-level library supported directly by the operating system. In this case, code and data structures for the library exist in kernel space. Invoking a function in the API for the library typically results in a system call to the kernel. Three main thread libraries are in use today: (a) POSIX-Pthreads: can implements one of the two approaches (b) Win32: implement kernel-level library support (c) Java: implement library support provided by Host system. 2. The fork ( ) and exec ( ) System Calls: As we have discussed in process section, fork () is used to create a separate, duplicate process (child process). The semantics of the fork () and exec () system calls change in a multithreaded program. When one thread in a program calls fork (), does the new process duplicates all threads, or is the new process single-threaded? Some UNIX systems have chosen to have two versions of fork. When a thread invokes the exec() system call, the program specified in the parameter to exec() will replace the entire process- including all threads. 3. Thread Cancellation: It is the task of terminating a thread before it has completed. A thread that is to be canceled is often referred to as the target thread. Cancellation of a thread may occur in two different scenarios: o Asynchronous Cancellation: One thread immediately terminates the target thread. o Deferred Cancellation: the target thread periodically checks whether it should terminate, allowing it an opportunity to terminate itself in an orderly fashion. 4. Thread Pools: Sometimes creating and executing a thread results into further creation of new thread, these newly created threads create further more threads, in this way number of threads are increased with time and could exhaust system resources, such as CPU time or memory. One solution to this problem is thread pool. The general idea behind a thread pool is to create a number of threads at process startup and place them into a pool, where they sit and wait for work. When a server receives a request, it awakens a thread from this pool-if one is available and passes it the request to service. Once

1. 49 |Opera ting S ys tem Concepts the thread completes its service, it returns to the pool and awaits more work. If the pool contains no available thread, the sever waits until one becomes free. The number of threads in the pool can be set based on factors such as the number of CPUs in the system, the amount of physical memory, and the expected number of concurrent client requests. Benefits of thread pool: o Servicing a request with an existing thread is usually faster than waiting to create a thread. o A thread pool limits the number of threads that exist at any one point. This is particularly important on systems that cannot support a large number of concurrent threads. 5. Thread- Specific Data: In some circumstances, each thread of a process might need its own copy of certain shared data. Such data is called thread-specific data. For example, in a transaction-processing system, we might service each transaction in a separate thread. Also, each transaction may be assigned a unique identifier. Here we can use thread-specific data to associate each thread with its unique identifier. Most thread libraries including Win32 and Pthreads provide some form of support for thread-specific data. Java provides support as well. 6. Light Weight Process: Many systems implementing the many-to-many or two-level model place an intermediate data structure between the user and kernel threads. This data structure is known as a lightweight process or LWP. To the user-library the LWP appears to be a virtual processor on which the application can schedule a user thread to run. Each LWP is attached to a kernel thread, and it is kernel thread that the operating system schedules to run on physical processors. If a kernel thread blocks, the LWP blocks as well.
User thread

LWP

Lightweight process

Kernel thread

Fig.1.42: Lightweight process (LWP)

1.4.5 User-level Threads and Kernel-level Threads


There are two broad categories of thread implementation: user-level threads (ULTs) and kernel-level threads (KLTs). KTLs are also referred as kernel-supported threads or lightweight processes. The choice is moderately controversial, and a hybrid implementation is also possible. We will now describe these methods, along with their advantages and disadvantages.

Introduc tion to Opera ting s ys tem, Process and threads | 1.50

User-level Thread: The first method is to put the threads package entirely in user space. The kernel knows nothing about them. As far as the kernel is concerned, it is managing ordinary, single-threaded processes. As shown in fig1.43 the threads run on top of a run-time system, which is a collection of procedures that manage threads. When threads are managed in user space, each process needs its own private thread table to keep track of the threads in that process. This table is analogous to the kernels process table, except that it keeps track only of the per-thread properties such the each threads program counter, stack pointer, registers, state, etc. The thread table is managed by the run-time system. When a thread is moved to ready state or blocked state, the information needed to restart it is stored in the thread table, exactly the same way as the kernel stores information about processes in the process table. When a thread does something that may cause it to become blocked locally, for example, waiting for another thread in its process to complete some work, it calls a run-time system procedure. This procedure checks to see if the thread must be put into blocked state. If so, it stores the threads registers (i.e., its own) in the thread table, looks in the table for a ready thread to run, and reloads the machine registers with the new threads saved values. As soon as the stack pointer and program counter have been switched, the new thread comes to life again automatically

Fig.1.43: (a) Pure user-level (b) Pure Kernel-level (c) Combined approaches Advantages: The first, and most obvious, advantage is that a user-level threads package can be implemented on an operating system that does not support threads. They allow each process to have its own customized scheduling algorithm. They also scale better, since kernel threads invariably require some table space and stack space in the kernel, which can be a problem if there are a very large number of threads. The procedure that saves the threads state and the scheduler are just local procedures, so invoking them is much more efficient than making a kernel call. No trap is needed, no context switch is needed and the memory cache need not be flushed. This makes thread scheduling very fast. Disadvantages: Blocking system calls are difficult to implement. The user level threads run precisely on existing operating system.

1. 51 |Opera ting S ys tem Concepts If a thread starts running, no other thread in that process will ever run unless the first thread voluntarily gives up the CPU. Within a single process, there are no clock interrupts making it impossible to schedule processes by using round-robin scheduling.

Kernel-level Thread: Now let us consider having the kernel know about and manage the threads. No run-time system is needed in each, as shown in Fig1.43 Also, there is no thread table in each process. Instead, the kernel has a thread table that keeps track of all the threads in the system. When a thread wants to create a new thread or destroy an existing thread, it makes a kernel call, which then does the creation or destruction by updating the kernel thread table. The kernels thread table holds each threads registers, state, and other information. The information is the same as with user-level threads, but it is now in the kernel instead of in user space (inside the runtime system). This information is a subset of the information that traditional kernels maintain about each of their single-threaded processes, that is, the process state. In addition, the kernel also maintains the traditional process table to keep track of processes. All calls that might block a thread are implemented as system calls, at considerably greater cost than a call to a run-time system procedure. When a thread blocks, the kernel can run either another thread from the same process (if one is ready), or a thread from a different process. Kernel threads do not require any new, nonblocking system calls. In addition, if one thread in a process causes a page fault, the kernel can easily check to see if the process has any other runnable threads, and if so, run one of them while waiting for the required page to be brought in from the disk. Thread Recycling: Due to the relatively greater cost of creating and destroying threads in the kernel, some systems take an environmentally correct approach and recycle their threads. When a thread is destroyed, it is marked as not runnable or passive, but its kernel data structures are not otherwise affected. Later, when a new thread must be created, an old thread is reactivated or active, saving some overhead. Thread recycling is also possible for user-level threads, but since the thread management overhead is much smaller, there is less incentive to do this. Advantages: The main advantage is that the kernel can simultaneously schedule multiple threads from the same process on multiple processors. If one thread in a process is blocked, the kernel can schedule another thread of the same process. Kernel routines themselves can be multithreaded.

Disadvantages:

The cost of a system call is substantial, so if thread operations (creation, termination, etc.) are common, much more overhead will be incurred. The transfer of control from one thread to another within the same process requires a mode switch to the kernel.

Introduc tion to Opera ting s ys tem, Process and threads | 1.52

Hybrid implementation: Various ways have been investigated to try to combine the advantages of user-level threads with kernellevel threads. One way is use kernel-level threads and then multiplexes user-level threads onto some or all of the kernel threads, as shown in Fig1.44(c).
Thread Process Thread Process

User Space

User Space

Kernel Space Runtime System

Kernel Thread Table Process Table

Kernel Space

Kernel Thread Table (b) Kernel-level Threads Process Table

(a) User-level Threads

User Space

Multiple user threads on a Kernel thread

Kernel Space

Kernel (c) Combined approach

Kernel thread

Fig.1.44: Detailed view of thread implementation approaches In this design, the kernel is aware of only the kernel-level threads and schedules those. Some of those threads may have multiple user-level threads multiplexed on top of them. These user-level threads are created, destroyed, and scheduled just like user-level threads in a process that runs on an operating system without multithreading capability. In this model, each kernel-level thread has some set of userlevel threads that take turns using it. In a combined system, thread creation is done completely in user space, as is the bulk of the scheduling and synchronization of threads within an application. The multiple ULTs from a single application are mapped onto some (smaller or equal) number of KLTs. The programmer may adjust the number of KLTs for a particular application and processor to achieve the best overall results. In a combined approach, multiple threads within the same application can run in parallel on multiple processors, and a blocking system call need not block the entire process Solaris is a good example of an OS using this combined approach. The current Solaris version limits the ULT/KLT relationship to be one-to-one.

1. 53 |Opera ting S ys tem Concepts

1.4.6 Benefits of Threads


1) Responsiveness: Multithreading an interactive application may allow a program to continue running even if part of it is blocked or is performing a lengthy operation, thereby increasing responsiveness to the user. For example, a multithreaded web browser could still allow user interaction in one thread while an image was being loaded in another thread. 2) Resource Sharing: By default, threads share the memory and the resources of the process to which they belong. The benefit of sharing code and data is that it allows an application to have several different threads of activity within the same address space. 3) Economy: Allocating memory and resources for process creation is costly, but threads share resources of the process to which they belong, it is more economical to create and context-switch threads. In general it is much more time consuming to create and manage processes than threads. For example, in Solaris, creating a process is about thirty times slower than is creating a thread, and context switching is about five times slower. 4) Utilization of multiprocessor architecture: The benefits of multithreading can be greatly increased in a multiprocessor architecture, where threads may be running in parallel on different processors. A single threaded process can only run on one CPU, no matter how many are available. Multithreading on a multi-CPU machine increases concurrency.

1.4.7 Thread Usage


Before discussing usage of threads let us know why we require threads? There are three main reasons:
(a) Only threads have the ability for the parallel entities to share an address space and all of its

data among themselves, multiple processes cannot perform this. (b) Threads do not have any resources attached to them, they are easier to create and destroy than processes. In many systems, creating a thread goes 100 times faster than creating a process. (c) Threads yield no performance gain when all of them are CPU bound, but when there is substantial computing and also substantial I/O, having threads allows these activities to overlap, thus speeding up the application. It is probably easiest to see why threads are useful by giving some concrete examples. Example 1: Web Server Consider a Web Server. One way to organize the Web server is shown in Fig1.46. Here one thread, the dispatcher, reads incoming requests for work from the network. After examining the request, it chooses an idle (i.e., blocked) worker thread and hands it the request, possibly by writing a pointer to the message into a special word associated with each thread. The dispatcher then wakes up the sleeping worker, moving it from blocked state to ready state. When the worker wakes up, it checks to see if the request can be satisfied from the Web page cache, to

Introduc tion to Opera ting s ys tem, Process and threads | 1.54

which all threads have access. If not, it starts a read operation to get the page from the disk and blocks until the disk operation completes. When the thread blocks on the disk operation, another thread is chosen to run, possibly the dispatcher, in order to acquire more work, or possibly another worker that is now ready to run. This model allows the server to be written as a collection of sequential threads. The dispatchers program consists of an infinite loop for getting a work request and handing it off to a worker. Each workers code consists of an infinite loop consisting of accepting a request from the dispatcher and checking the Web cache to see if the page is present. If so, it is returned to the client and the worker blocks waiting for a new request. If not, it gets the page from the disk, returns it to the client, and blocks waiting for a new request.

Fig.1.46: A Multithreaded Web Server Example 2: Word Processor Another example, consider a word processor. Most word processors display the document being created on the screen formatted exactly as it will appear on the printed page. In particular, all the line breaks and page breaks are in their correct and final position so the user can inspect them and change the document.
Suppose that the user is writing a book, now there can be two possible strategies for writing book. First, either write the whole book in a single file or second, write each section/subsection/chapter in separate files. If a change has to be made just before printing the book then First strategy will be better because it will be easy to search the particular word and replace it with new word by using a single search operation in the file. In this case, the Second strategy will require a search operation for each separate chapter file to replace the word. Now consider what happens when the user suddenly deletes one sentence from page 1 of an 800-page document. After checking the changed page to make sure it is correct, the user now wants to make another change on page 600 and types in a command telling the word processor to go to that page. The word processor is now forced to reformat the entire book up to page 600 on the spot because it does not know what the first line of page 600 will be until it has processed all the previous pages. There may be a substantial delay before page 600 can be displayed, leading to an unhappy user.

Threads can help here. Suppose that the word processor is written as a two threaded program. One thread interacts with the user and the other handles reformatting in the background. As soon as the sentence is deleted from page 1, the interactive thread tells the reformatting thread to reformat the whole book. Meanwhile, the interactive thread continues to listen to the keyboard and mouse and responds to simple commands like scrolling page 1 while the other thread is computing madly in the

1. 55 |Opera ting S ys tem Concepts background. With a little luck, the reformatting will be completed before the user asks to see page 600, so it can be displayed instantly. While we are at it, why not add a third thread?

Fig.1.45: A word processor with three threads Many word processors have a feature of automatically saving the entire file to disk every few minutes to protect the user against losing a days work in the event of a program crash, system crash, or power failure. The third thread can handle the disk backups without interfering with the other two. The situation with three threads is shown in Fig. 1.45. If the program were single-threaded, then whenever a disk backup started, commands from the keyboard and mouse would be ignored until the backup was finished. The user would perceive this as sluggish performance. Alternatively, keyboard and mouse events could interrupt the disk backup, allowing good performance but leading to a complex interrupt-driven programming model. With three threads, the programming model is much simpler. The first thread just interacts with the user. The second thread reformats the document when told to. The third thread writes the contents of RAM to disk periodically. It should be clear that having three separate processes would not work here because all three threads need to operate on the document. By having three threads instead of three processes, they share a common memory and thus all have access to the document being edited.

1.4.8 Multithreading Models


Support for threads may be provided either at the user level, for user threads, or by the kernel, for kernel threads. User threads are supported above the kernel and managed without kernel support, whereas kernel threads are supported and managed directly by the operating system. Most of the operating system supports the kernel level threads, as it provide the basis for system call implementation and helps in achieving machine level parallelism. The relationship between user threads and kernel threads can be depicted as following three models called multithreading models: 1) Many-to-One Model: This model maps many user-level threads to one kernel thread. Thread management is done by the thread library in user space, so it is efficient; but the entire process will block if a thread makes a blocking system call. Also, multiple threads are unable to run in parallel on multiprocessors because of only one thread can access the kernel at a time. Example: Green threads a thread library available for Solaris uses this model.

Introduc tion to Opera ting s ys tem, Process and threads | 1.56

User thread

Kernel thread

Fig.1.47: Many-to-one model 2) One-to-One Model: This model maps one user-level threads to one kernel thread. It provides more concurrency than the many-to-one model by allowing another thread to run when a thread makes a blocking system call; it also allow multiple threads to run in parallel on multiprocessors. The only drawback to this model is that creating a user thread requires creating the corresponding kernel thread. Most implements of this model restrict the number of threads supported by the system because of the overhead of creating kernel thread can burden the performance of an application. Example: Linux, Windows 95, 98, NT, 2000 and XP.

User thread

Kernel thread

Fig.1.48: One-to-One model 3) Many-to-Many Model: This model multiplexes many user-level threads to a smaller or equal number of kernel threads. This model does not suffer from the shortcomings of above two models. Here the developer can create as many user threads as necessary, and the corresponding kernel thread can run in parallel on a multiprocessor. Also, when a thread performs a blocking system call, the kernel can schedule another thread for execution. Two level model: one popular variation on the many-to-many model still multiplexes many user-level threads to smaller or equal number of kernel threads but also allows a user-level thread to be bound to a kernel thread. This variation sometimes referred to as the two-level model. Example: IRIX, HP-UX and Tru64 UNIX.

1. 57 |Opera ting S ys tem Concepts

User thread

User thread

Kernel thread

Kernel thread

(a) Many-to-many model

(a) Two level model

Fig.1.49: (a) Many-to-Many model and (b) Two level model

Summary
Operating System: An Operating System acts as an intermediary between the user of a computer and the computer hardware. It is software that manages the computer hardware. Operating system is like an interface between the user and machine hardware, which is responsible for managing various resources, handling system calls, executing privileged instructions and various other functions. OS as User View: The users view of the computer varies according to the interface being used. OS as System View: From the computers point of view, the OS is the program most closely involved with the hardware. OS is the most important part of system as it ensures the reliability, efficiency and effectiveness of program execution. Responsibilities of OS: Basic operations and monitoring, Ensure protection while concurrent execution and provide software platform. A real-time operating system is a multitasking operating system that aims at executing real-time applications. An Embedded system is a special purpose computer system designed to perform one or a few dedicated functions and usually embedded as a part of a complete device. A distributed operating system manages a group of independent computers and makes them appear to be a single computer. A multi-user operating system allows multiple users to access a computer system concurrently while Single-user operating systems, as opposed to a multi-user operating system, are usable by a single user at a time. When a single program is allowed to run at a time, the system is grouped under a single-tasking system, while in case the operating system allows the execution of multiple tasks at one time, it is classified as a multi-tasking operating system.

Introduc tion to Opera ting s ys tem, Process and threads | 1.58

A networking operating system (NOS), also referred to as the Dialogue, is the software that runs on a server and enables the server to manage data, users, groups, security, applications, and other networking functions. Parallel Systems: systems there are several processors that are connected to each other via an interconnection network. Various resources like memory, I/O devices are shared among these processors. Symmetric Multiprocessing: In this system, each process runs identical copy of the operating system and these copies communicate with each other, as needed. Asymmetric Multiprocessing: Each processor is assigned a specific task. This scheme defines a master-slave relationship. Operating systems provide two kinds of services: User specific and System specific. The BIOS software is built into the PC, and is the first code run by a PC when powered ON. The primary function of the BIOS is to set up the hardware and load and start an operating system. In computing, a system call is how a program requests a service from an operating system's kernel that it does not normally have permission to run. System calls provide the interface between a process and the operating system. "FIRMWARE is the combination of a hardware device and computer instructions and data that reside as read-only software on that device. The small program that starts this sequence of loading into RAM is known as a bootstrap loader. Process is one of the fundamental concepts in modern operating systems. A process is defined as a program in execution; it is the unit of work in system. It provides an abstraction of a program to be executed. The main motive of using process is to achieve Pseudo parallelism. Process creation in UNIX and Linux are done through fork() or clone() system calls. There are several steps involved in process creation. A process terminates when it finishes executing its last statement. Its resources are returned to the system, it is purged from any system lists or tables, and its process control block (PCB) is erased There are 5 states of a process: NEW, READY, RUNNING, WAITING and TERMINATED A context switch is the computing process of storing and restoring state (context) of a CPU so that execution can be resumed from the same point at a later time. Thread is a single independent flow of execution within a process when multiple threads in a single process exhibit process execution then this is called Multithreading. The thread has a program counter that keeps track of which instruction to execute next. Thread Library: a thread library provides the programmer an API for creating and managing threads. The general idea behind a thread pool is to create a number of threads at process startup and place them into a pool, where they sit and wait for work.

1. 59 |Opera ting S ys tem Concepts

Previous Years Questions


1. Enumerate the advantages and disadvantages of supporting multi-threaded applications with kernel level threads. (Refer topic 1.4.5)
(RTU 2010; Raj. Univ. 2008)

2. Attempt the following questions: a) Explain the five state process models with the help of a diagram. (Refer topic 1.3.4) b) Explain the use of various fields of process control block. (Refer topic 1.3.7)
(RTU 2010, 2009; Raj. Univ. 2008, 2007)

3. List the services provided by an operating system. (Refer topic 1.2.7)


(Raj. Univ. 2001,2002,2003)

4. What is meant by a system call? How does an application program use these calls during execution? Also explain the purpose of system calls. (Refer topic 1.2.9)
(RTU 2010; Raj. Univ. 2003)

5. Explain following with example (1) Multiprogramming (2) Multitasking and (3) Multiuser.
(RTU 2010; Raj. Univ. 2006)

6. Explain various types of multithreading models with the help of example of operating system which implements them. (Refer topic 1.4.8)
(RTU 2009)

7. Differentiate between a) Time sharing and real-time operating system (Refer topic 1.2.5) b) Network and Distributed operating system (Refer topic 1.2.5)
(Raj. Univ. 2002)

8. What is operating system? How does it interact with device? What are the main agents of operating system? (Refer topic 1.2.1 and 1.2.3)
(Raj. Univ. 2007)

9. Explain how threads approach improves performance of operating system. Give appropriate examples. (Refer topic 1.4.7)
(Raj. Univ. 2004)

10. Give three different views of operating system and compare: a) Layered model and virtual machine model of O.S (Refer topic 1.2.3) b) OS for a PC and an embedded system. (Refer topic 1.2.5)
(Raj. Univ. 2003)

11. What are the main purposes of Operating system? Explain. (Refer topic 1.2.4)
(Raj. Univ. 2003)

12. Write short notes on Parallel vs. Distributed Systems. (Refer topic 1.2.5)
(Raj. Univ. 2000)

13. Name the major activities of an operating system and briefly describe why each is required? (Refer topic 1.2.7)
(Raj. Univ. 2004, 2000, 1995, 1993)

Introduc tion to Opera ting s ys tem, Process and threads | 1.60

Review Questions
1. 2. 3. 4. What is multiprocessing? How it is different from parallel processing? What is multitasking? How it is different from Time-sharing system? What is Operating System? Explain Layered architecture of OS. Write short notes on following: a) BIOS b) Firmware c) Boot Strap Loader d) System Calls 5. Write short notes on Operating system as resource manager. 6. Write short notes on Operating system as virtual machine. 7. Write short notes on following: a) Process Hierarchy b) Context Switching c) Process States & Transitions 8. What is a Process? Discuss the term Process Creation and Process Termination. 9. Describe the concept of a Process and Process Model. Also list the fields of Process Control Block. 10. Write short notes on Basic system calls-Linux and Windows. 11. Explain the concept of threads. Differentiate threads from process. 12. Describe thread usage and thread benefits.

Anda mungkin juga menyukai