OS Unit1
OS Unit1
SYLLABUS
RAJASTHAN TECHNICAL UNIVERSITY Detailed Syllabus B.Tech. (Comp. Engg.) V-VI Sem. 2010-11
Units
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
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
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
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 and logical structure of operating system are described below
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.
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.
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
Shell & Command Interpreters CPU scheduling & Virtual Memory Kernel Interface to H/W
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.
1.2.4
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
Operating System
Embedded OS
Embedded 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).
Processor
Sensor
Actuator
External Environment
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
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
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.
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.
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.
Windows OS
Administrator Student DB2Admin
UNIX OS
User 1 ID & PSWD User 2 ID & PSWD User 3 ID & PSWD
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.
Time Slice
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
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.
P1 LM1
P2 LM2
P3 LM3
P4 LM4
P1 LM1
P2 LM2
P3 LM3
P4 LM4
PE3
PE4
PE1
PE1
Disadvantages
Design is complex Limitations of master-slave architecture Decreased reliability Cost performance low
1.2.6
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.
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 VM1
Kernel VM3
Virtual-machine implementation
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
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.
User Interface Program Execution I/O Operations File-system manipulation Communications Error detection
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
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.
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.
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
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
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.
1.2.9
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.
Open ()
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.
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.
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
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.
Software Hardware
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. 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.
Max
Stack
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.
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()
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
New Process
Enter
Not Running
Running
Exit
Process Terminated
New Process
Enter
Queue
Dispatch
Processor
Exit
Process Terminated
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.
New
Admit
Release
Terminated
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)
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
Blocked Queue
Dispatch
Processor
Event2 Occurs
Event2 Wait
Even2 Queue
Event N Occurs
Event N Queue
Event N Wait
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.
Fork ( )
Fork ( )
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
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
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.
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
Sr. no.
Function name
Description
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
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.
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.
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.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.
Code Registers
Data
Files Stack
Code Registers
Data Registers
Files Registers
Stack
Stack
Stack
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.
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
Release
Terminated
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.
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. 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
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.
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
User Space
Kernel Space
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.
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
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.
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.
User thread
User thread
Kernel thread
Kernel thread
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.
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.
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)
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)
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.