OS Concepts - Course Material

Embed Size (px)

Citation preview

  • 8/14/2019 OS Concepts - Course Material

    1/60

    OS Concepts

    1. Introduction to operating system

    Components of an Operating System

    Operating System Executive: -

    Types of operating system

    Batch Systems: -

    Interactive systems: -

    Objectives of an operating system.

    2. About Operating system and Kernels

    O.S vs Kernels

    Batching processing

    Multi user, Multi- processing O.S

    Layered Processing

    Distributed system

    Real Time O.S

    3. Operating System components

    Into to Process management sub system

    Into to Memory management sub system

    Intro to File sub system Into to Device drivers

    Signals and System calls

    4. Process management sub system

    Creating process

    Process Id, process descriptor

    Process contexts

    Process state

    Process address space

    Process scheduling policies Threads vs process

    Multi threading and Hyper threading

    5. Memory management sub system

    Segmentation

    Paging

    Page allocation strategies

  • 8/14/2019 OS Concepts - Course Material

    2/60

    Memory mapping and unmapping

    6. File sub system

    Importance of File system File representation in the system

    File creation and maintenance

    Block devices and file system

    7. Resource sharing in OS

    File locking

    Record locking

    Dead locks

    Advisory locking and mandatory locking

    Concept of Semaphores and mutex

  • 8/14/2019 OS Concepts - Course Material

    3/60

    Chapter 1

    Introduction To OS

    Components of an Operating System

    Operating System Executive

    Types of operating system

    Batch Systems

    Interactive systems

    Objectives of an operating system

    An operating system is a collection of programs used to control a computer

    system.

    Components of an Operating System.

    Booting Loader: -

    The function of the bootstrap loader program, which is usually contained in ROM, is to

    read the main portion of the operating system from secondary memory.

    Diagnostic Tests: -

    The operating system contains number of diagnostic programs that test the operations ofthe system components.

    Eg: Check the operations of the disk drive

    Check the operations of the RAM

    Operating System Executive: -

    The operating executive is a program that controls the activities of the system (also called

    monitor program)

    Eg: Executing programs and assigning tasks to hardware devices. This operating systemexecutive is read into RAM when computer is started. Some sophisticated operating

    system executives allow several programs to run simultaneously.

    BIOS: - Basic Input Output systems

    The program is of the low level. Some functions of the BIOS programs are:

    Reading a character from key board

  • 8/14/2019 OS Concepts - Course Material

    4/60

    Writing a character on the video display

    Writing a character to the printer

    Determining whether the printer is busy or not

    Reading a sector from the disk in a particular disc drive

    The BIOS programs are usually recorded in the ROM

    Utility Programs: -

    Every computer system needs utility programs to perform house keeping functions. The

    functions are as follows:

    Formatting a disk

    Displaying the contents of a disk

    Copying the contents of one disk to another

    Making a backup copy of the contents of a hard disk

    File Maintenance: -

    The operating system provides service routines that can be used to maintain files on the

    various secondary memories. These files maintenance routines allow programs to createread, write files

    TYPES OF OPERATING SYSTEMS

    Batch Systems: -

    A batch operating system accepts jobs, and places them in a queue to awaitexecution. This process is often called spooling. The operating system selects jobs based

    on priorities from the job queue. Batch jobs maybe executed on its serial basis

    Interactive systems: -

    An interactive operating system allows users to interact directly with a computerfrom a terminal. In effect the users can interrupt a low priority batch job and cause the

    computer to perform his high priority work. Interactive systems must be multi-

    programming systems.

    Multi Tasking: -

    Multi tasking is the capability of a CPU to execute two or more programs

    concurrently. Essentially two are more programs are stored concurrently in primary

    storage.

    Operating system as resource allocator: -

  • 8/14/2019 OS Concepts - Course Material

    5/60

    A computer system has many resources that may be required to solve a problem,

    CPU time memory space file storage space I/O devices and so on. The operating system

    acts as a manger of these resources and allocates them to specific programs and users asnecessary for tasks

    WHAT IS ON OPERATING SYSTEM?

    An operating system is software that performs the task of overall resources

    management for a computer system. In this course we shall look into the basic conceptsand methodologies used for a few important parts of an operating system.

    Objectives of an operating system.

    Co-operation:-

    The operating should enable the users to share the resources in a manner that all the jobscan get resources they need.

    Operating system components:-

    The important components of an operating system are:

    Process management

    Main memory management

    Secondary storage management

    I/O system management

    File management

    Protection system

    Process management:-

    The program executed by the computer system includes the user defined

    programs as well as system defined programs.

    A program is executed in a CPU. Besides the CPU, a process requires a memoryand other resources. The operating system must take proper decision regarding proper

    allocation of CPU. The process management unit of operating system is responsible for

    taking such decisions.Sometimes two processes may want to exchange data with one another. One

    process must wait till another process reaches a particular stage of execution. These

    communications and synchronizations are carried out by process managementcomponents.

    Policies: -

  • 8/14/2019 OS Concepts - Course Material

    6/60

    The operating system must ensure the application of a proper policy through

    which the different processes are allocated resources. The operating system must decide

    as to who should get a particular resources when and how long for resources like memoryoperating system must specify how much, main memory area should be allocated to a

    program at a time.

    Error handling: -

    The operating system must be able to react to error condition. Operating systemmust also include system diagnostic programs to check the health of the system at power

    on.

    Accounting: -

    The operating system must keep track to the initialization of the different

    resources such as, how much disk space or how much CPU time a particular program

    uses.

    Following activities are associated with process management:

    The creation and deletion of both users and system process

    Suspension and resumption of process

    Process communication and synchronization

    Dead lock handling

    Main memory management: -

    When a program is saved it is usually kept on the disk but for executing a

    program it must be brought into memory. Primary memory does not offer sufficient spaceto hold all the programs hence it is to be decided as to which set of programs should be

    brought into primary memory for execution. It is also to be decided as to which part of

    the memory should be allocated to a particular program.If memory is to be allocated for a new process, some programs residing in the

    memory need to be swapped out to the secondary memory. These decisions are taken and

    executed by the main memory management unit.

    UNIX is a layered operating system. The innermost layer is the hardware that

    provides the service for the O.S. The O.S. in UNIX is called as Kernel interacts directly

    with the hardware and provides the services to the user programs.

    User programs interact with the kernel through a set of standard system calls.These system calls request services to be provided by the Kernel. Such services would

    include accessing a file open, close, read, write, link, changing ownership of a file or adirectory, changing to a new directory, creating, suspending or killing a process and

    setting limits on system resources.

    UNIX is a multi user, multi tasking and multi terminal O.S.

  • 8/14/2019 OS Concepts - Course Material

    7/60

    UNIX caught on among programmers because it was designed with these features:

    programmers environment

    simple user interface simple utilities that can be combined to perform powerful functions

    hierarchical file system

    simple interface to devices consistent with file format multi-user, multi-process system

    architecture independent and transparent to the user.

    Chapter 2

    About Operating system and Kernels

    O.S vs Kernels

    Batching processing

    Multi user, Multi- processing O.S

    Layered Processing

    Distributed system

    Real Time O.S

    OVERVIEW

    What is an operating system and what does it do? The operating system has two basic

    functions. First, it communicates with the PCs hardware; it gives applications access tothings like the hard drive, it receives input from the keyboard, and it outputs data to the

    monitor. The second function is to provide a user interface and interpret commands so a

    user can communicate with the hardware.

    An operating system is also referred to as a command interpreter. Several types of

    operating systems perform basic functions. For the purpose of this book, we cover only

    three: MS-DOS, Windows 3.x, and Windows 95/98. We cover some basics for each

    operating system and talk about the architecture of each.

    OPERATING SYSTEM ROLE

    Must communicate with the PCs hardware

    Works with the BIOS to provide access to devices such as hard drives

    Communicates with device drivers

    Provides a user interface

  • 8/14/2019 OS Concepts - Course Material

    8/60

    To communicate with the PCs hardware, the operating system starts running

    immediately after the PC has finished its Power-On Self Test (POST), when it takes

    control of the PC. The operating system works with the BIOS to provide access todevices such as hard drives, floppy drives, keyboards, mice, and basic video. If a user

    wants to access a file on a diskette in a floppy drive, the operating system passes the

    request to the appropriate set of BIOS instructions that tells the floppy controller to sendthe requested information to RAM.

    If the BIOS is unable to talk with the hardware, the operating system talks to thehardware directly. For this capability, it needs some additional programming. Device

    drivers provide the code necessary for the operating system to communicate with specific

    hardware. Device drivers are written specifically for a particular operating system,

    usually by the hardware manufacturer.

    In addition to communicating with hardware, the operating system provides for some

    type of error handling and error notification. If a piece of hardware is not functioning

    properly, the operating system tries to fix the problem or tries to communicate with thedevice a few more times. If it is still unable to communicate with the device, it provides

    an error screen notifying the user of the problem.

    Providing a user interface is the second function of an operating system. Basically, the

    operating system organizes applications so users can easily access them, use them, and

    store application data. When an application is opened, the operating system lets theapplication provide the majority of the user interface. That is, the operating system

    disappears or moves away from the center of the screen. The operating system still has

    the responsibility of providing access to the hardware for whatever the application needs.If the program cannot function properly, the operating system again takes control, stops

    the application, and provides some sort of error message.

    Kernel Definition

    The kernelis a program that constitutes the central core of a computer operating system.

    It has complete control over everything that occurs in the system.

    A kernel can be contrasted with ashell(such as bash, csh orksh in Unix-like operatingsystems), which is the outermost part of an operating system and a program that interacts

    with user commands. The kernel itself does not interact directly with the user, but rather

    interacts with the shell and other programs as well as with the hardware devices on thesystem, including the processor (also called the central processing unit or CPU), memory

    and disk drives.

    The kernel is the first part of the operating system to load into memory during booting

    (i.e., system startup), and it remains there for the entire duration of the computer sessionbecause its services are required continuously. Thus it is important for it to be as small as

  • 8/14/2019 OS Concepts - Course Material

    9/60

    possible while still providing all the essential services needed by the other parts of the

    operating system and by the various application programs.

    Because of its critical nature, the kernel code is usually loaded into a protectedarea ofmemory, which prevents it from being overwritten by other, less frequently used parts of

    the operating system or by application programs. The kernel performs its tasks, such asexecutingprocesses and handling interrupts, in kernel space, whereas everything a user

    normally does, such as writing text in a text editor or running programs in a GUI(graphical user interface), is done in user space. This separation is made in order to

    prevent user data and kernel data from interfering with each other and thereby

    diminishing performance or causing the system to become unstable (and possiblycrashing).

    When a computercrashes, it actually means the kernel has crashed. If only a single

    program has crashed but the rest of the system remains in operation, then the kernel itself

    has not crashed. A crash is the situation in which a program, either a user application or a

    part of the operating system, stops performing its expected function(s) and responding toother parts of the system. The program might appear to the user tofreeze. If such program

    is a critical to the operation of the kernel, the entire computer could stall or shut down.

    The kernel provides basic services for all other parts of the operating system, typicallyincluding memory management, process management, file management and I/O

    (input/output) management (i.e., accessing the peripheral devices). These services are

    requested by other parts of the operating system or by application programs through aspecified set of program interfaces referred to assystem calls.

    Process management, possibly the most obvious aspect of a kernel to the user, is the part

    of the kernel that ensures that each process obtains its turn to run on the processor andthat the individual processes do not interfere with each other by writing to their areas ofmemory. A process, also referred to as a task, can be defined as an executing (i.e.,

    running) instance of a program.

    The contents of a kernel vary considerably according to the operating system, but they

    typically include (1) a scheduler, which determines how the various processes share thekernel's processing time (including in what order), (2) a supervisor, which grants use of

    the computer to each process when it is scheduled, (3) an interrupt handler, which

    handles all requests from the various hardware devices (such as disk drives and thekeyboard) that compete for the kernel's services and (4) a memory manager, which

    allocates the system's address spaces (i.e., locations in memory) among all users of the

    kernel's services.

    The kernel should not be confused with theBIOS(Basic Input/Output System). TheBIOS is an independent program stored in a chip on the motherboard(the main circuit

    board of a computer) that is used during the booting process for such tasks as initializing

    the hardware and loading the kernel into memory. Whereas the BIOS always remains inthe computer and is specific to its particular hardware, the kernel can be easily replaced

  • 8/14/2019 OS Concepts - Course Material

    10/60

    or upgraded by changing or upgrading the operating system or, in the case of Linux, by

    adding a newer kernel or modifying an existing kernel.

    Most kernels have been developed for a specific operating system, and there is usuallyonly one version available for each operating system. For example, the Microsoft

    Windows 2000 kernel is the only kernel for Microsoft Windows 2000 and the MicrosoftWindows 98 kernel is the only kernel for Microsoft Windows 98. Linux is far more

    flexible in that there are numerous versions of the Linux kernel, and each of these can bemodified in innumerable ways by an informed user.

    A few kernels have been designed with the goal of being suitable for use with any

    operating system. The best known of these is the Mach kernel, which was developed at

    Carnegie-Mellon University and is used in the Macintosh OS X operating system.

    It is not necessary for a computer to have a kernel in order for it to be usable, the reason

    being that it is not necessary for it to have an operating system. That is, it is possible to

    load and run programs directly on bare metal machines (i.e., computers without anyoperating system installed), although this is usually not very practical.

    In fact, the first generations of computers used bare metal operation. However, it was

    eventually realized that convenience and efficiency could be increased by retaining small

    utility programs, such as program loaders and debuggers, in memory betweenapplications. These programs gradually evolved into operating system kernels.

    The term kernelis frequently used in books and discussions about Linux, whereas it is

    used less often when discussing some other operating systems, such as the Microsoft

    Windows systems. The reasons are that the kernel is highly configurable in the case of

    Linux and users are encouraged to learn about and modify it and to download and installupdated versions. With the Microsoft Windows operating systems, in contrast, there is

    relatively little point in discussing kernels because they cannot be modified or replaced.

    Categories of Kernels

    Kernels can be classified into four broad categories: monolithic kernels, microkernels,hybrid kernels and exokernels. Each has its own advocates and detractors.

    Monolithic kernels, which have traditionally been used by Unix-like operating systems,

    contain all the operating system core functions and the device drivers (small programs

    that allow the operating system to interact with hardware devices, such as disk drives,video cards and printers). Modern monolithic kernels, such as those of Linux and

    FreeBSD, both of which fall into the category of Unix-like operating systems, feature the

    ability to load modules at runtime, thereby allowing easy extension of the kernel'scapabilities as required, while helping to minimize the amount of code running in kernel

    space.

  • 8/14/2019 OS Concepts - Course Material

    11/60

    A microkernel usually provides only minimal services, such as defining memory address

    spaces, interprocess communication (IPC) and process management. All other functions,

    such as hardware management, are implemented as processes running independently ofthe kernel. Examples of microkernel operating systems are AIX, BeOS, Hurd, Mach, Mac

    OS X, MINIX and QNX.

    Hybrid kernels are similar to microkernels, except that they include additional code in

    kernel space so that such code can run more swiftly than it would were it in user space.These kernels represent a compromise that was implemented by some developers before

    it was demonstrated that pure microkernels can provide high performance. Hybrid kernels

    should not be confused with monolithic kernels that can load modules after booting (suchas Linux).

    Most modern operating systems use hybrid kernels, including Microsoft Windows NT,

    2000 and XP. DragonFly BSD, a recentfork(i.e., variant) of FreeBSD, is the first non-

    Mach based BSD operating system to employ a hybrid kernel architecture.

    Exokernels are a still experimental approach to operating system design. They differ from

    the other types of kernels in that their functionality is limited to the protection and

    multiplexing of the raw hardware, and they provide no hardware abstractions on top of

    which applications can be constructed. This separation of hardware protection fromhardware management enables application developers to determine how to make the most

    efficient use of the available hardware for each specific program.

    Exokernels in themselves they are extremely small. However, they are accompanied bylibrary operating systems, which provide application developers with the conventionalfunctionalities of a complete operating system. A major advantage of exokernel-based

    systems is that they can incorporate multiple library operating systems, each exporting adifferent API (application programming interface), such as one for Linux and one forMicrosoft Windows, thus making it possible to simultaneously run both Linux and

    Windows applications.

    The Monolithic Versus Micro Controversy

    In the early 1990s, many computer scientists considered monolithic kernels to beobsolete, and they predicted that microkernel would revolutionize operating system

    design. In fact, the development of Linux as a monolithic kernel rather than a microkernel

    led to a famousflame war(i.e., a war of words on the Internet) between Andrew

    Tanenbaum, the developer of the MINIX operating system, and Linus Torvalds, whooriginally developed Linux based largely on MINIX.

    Proponents of microkernels point out that monolithic kernels have the disadvantage that

    an error in the kernel can cause the entire system to crash. However, with a microkernel,if a kernel process crashes, it is still possible to prevent a crash of the system as a whole

    by merely restarting the service that caused the error. Although this sounds sensible, it is

    questionable how important it is in reality, because operating systems with monolithic

    http://www.bellevuelinux.org/minix.htmlhttp://www.bellevuelinux.org/linus.htmlhttp://www.bellevuelinux.org/minix.htmlhttp://www.bellevuelinux.org/linus.html
  • 8/14/2019 OS Concepts - Course Material

    12/60

    kernels such as Linux have become extremely stable and can run for years without

    crashing.

    Another disadvantage cited for monolithic kernels is that they are notportable; that is,they must be rewritten for each new architecture (i.e., processor type) that the operating

    system is to be used on. However, in practice, this has not appeared to be a majordisadvantage, and it has not prevented Linux from being ported to numerous processors.

    Monolithic kernels also appear to have the disadvantage that theirsource code canbecome extremely large. Source code is the version ofsoftware as it is originally written

    (i.e., typed into a computer) by a human inplain text(i.e., human readable alphanumeric

    characters) and before it is converted by a compiler into object code that a computer's

    processor can directly read and execute.

    For example, the source code for the Linux kernel version 2.4.0 is approximately 100MB

    and contains nearly 3.38 million lines, and that for version 2.6.0 is 212MB and contains

    5.93 million lines. This adds to the complexity of maintaining the kernel, and it alsomakes it difficult for new generations of computer science students to study and

    comprehend the kernel. However, the advocates of monolithic kernels claim that in spite

    of their size such kernels are easier to design correctly, and thus they can be improved

    more quickly than can microkernel-based systems.

    Moreover, the size of the compiled kernel is only a tiny fraction of that of the source

    code, for example roughly 1.1MB in the case of Linux version 2.4 on a typical Red Hat

    Linux 9 desktop installation. Contributing to the small size of the compiled Linux kernel

    is its ability to dynamically load modules at runtime, so that the basic kernel containsonly those components that are necessary for the system to start itself and to load

    modules.

    The monolithic Linux kernel can be made extremely small not only because of its abilityto dynamically load modules but also because of its ease of customization. In fact, there

    are some versions that are small enough to fit together with a large number of utilities and

    other programs on a single floppy disk and still provide a fully functional operating

    system (one of the most popular of which is muLinux). This ability to miniaturize itskernel has also led to a rapid growth in the use of Linux in embedded systems (i.e.,

    computer circuitry built into other products).

    Although microkernels are very small by themselves, in combination with all their

    required auxiliary code they are, in fact, often larger than monolithic kernels. Advocatesof monolithic kernels also point out that the two-tiered structure of microkernel systems,

    in which most of the operating system does not interact directly with the hardware,

    creates a not-insignificant cost in terms of system efficiency.

    http://www.bellevuelinux.org/mulinux.htmlhttp://www.bellevuelinux.org/mulinux.html
  • 8/14/2019 OS Concepts - Course Material

    13/60

    Kernel basic responsibilities

    The kernel's primary purpose is to manage the computer's resources and allow other

    programs to run and use these resources. Typically, the resources consist of:

    The CPU (frequently called the processor). This is the most central part of acomputer system, responsible forrunningorexecutingprograms on it. The kerneltakes responsibility for deciding at any time which of the many running programs

    should be allocated to the processor or processors (each of which can usually run

    only one program at once)

    The computer's memory. Memory is used to store both program instructions and

    data. Typically, both need to be present in memory in order for a program to

    execute. Often multiple programs will want access to memory, frequently

    demanding more memory than the computer has available. The kernel isresponsible for deciding which memory each process can use, and determining

    what to do when not enough is available.

    Any Input/Output (I/O) devices present in the computer, such as disk drives,printers, displays, etc. The kernel allocates requests from applications to perform

    I/O to an appropriate device (or subsection of a device, in the case of files on a

    disk or windows on a display) and provides convenient methods for using the

    device (typically abstracted to the point where the application does not need toknow implementation details of the device)

    http://en.wikipedia.org/wiki/Image:Computer_abstraction_layers.svg
  • 8/14/2019 OS Concepts - Course Material

    14/60

    Kernels also usually provide methods for synchronization and communication between

    processes (called inter-process communication or IPC).

    A kernel may implement these features itself, or rely on some of the processes it runs toprovide the facilities to other processes, although in this case it must provide some means

    of IPC to allow processes to access the facilities provided by each other.

    Finally, a kernel must provide running programs with a method to make requests to

    access these facilities.

    Process Management

    The main task of a kernel is to allow the execution of applications and support them with

    features such as hardware abstractions.

    Memory management

    The kernel has full access to the system's memory and must allow processes to access thismemory safely as they require it.

    Device Management

    To perform useful functions, processes need access to the peripherals connected to the

    computer, which are controlled by the kernel through device drivers

    Kernel design decisions

    Fault tolerance

    An important consideration in the design of a kernel is fault tolerance; specifically, in

    cases where multiple programs are running on a single computer, it is usually important

    to prevent a fault in one of the programs from negatively affecting the other. Extended to

    malicious design rather than a fault, this also applies to security, and is necessary toprevent processes from accessing information without being granted permission.

    Two main approaches to the protection of sensitive information are assigning privileges

    to hierarchical protection domains, for example by using a processor's supervisor mode,or distributing privileges differently for each process and resource, for example by using

    capabilities or access control lists.

    Hierarchical protection domains are much less flexible, as it is not possible to assign

    different privileges to processes that are at the same privileged level, and can't thereforesatisfy Denning's four principles for fault tolerance (particularly the Principle of least

    privilege). Hierarchical protection domains also have a major performance drawback,

    http://en.wikipedia.org/wiki/Peripheralhttp://en.wikipedia.org/wiki/Device_driverhttp://en.wikipedia.org/wiki/Peripheralhttp://en.wikipedia.org/wiki/Device_driver
  • 8/14/2019 OS Concepts - Course Material

    15/60

    since interaction between different levels of protection, when a process has to manipulate

    a data structure both in 'user mode' and 'supervisor mode', always requires message

    copying (transmission by value). A kernel based on capabilities, however, is moreflexible in assigning privileges, can satisfy Denning's fault tolerance principles, and

    typically doesn't suffer from the performance issues of copy by value.

    Both approaches typically require some hardware or firmware support to be operable and

    efficient. The hardware support for hierarchical protection domains is typically that of"CPU modes." An efficient and simple way to provide hardware support of capabilities is

    to delegate the MMU the responsibility of checking access-rights for every memory

    access, a mechanism called capability-based addressing. Most commercial computerarchitectures lack MMU support for capabilities. An alternative approach is to simulate

    capabilities using commonly-support hierarchical domains; in this approach, each

    protected object must reside in an address space that the application does not have accessto; the kernel also maintains a list of capabilities in such memory. When an application

    needs to access an object protected by a capability, it performs a system call and the

    kernel performs the access for it. The performance cost of address space switching limitsthe practicality of this approach in systems with complex interactions between objects,but it is used in current operating systems for objects that are not accessed frequently or

    which are not expected to perform quickly.Approaches where protection mechanism are

    not firmware supported but are instead simulated at higher levels (e.g. simulatingcapabilities by manipulating page tables on hardware that does not have direct support),

    are possible, but there are performance implications. Lack of hardware support may not

    be an issue, however, for systems that choose to use language-based protection.

    Security

    An important kernel design decision is the choice of the abstraction levels where thesecurity mechanisms and policies should be implemented. One approach is to use

    firmware and kernel support for fault tolerance (see above), and build the security policyfor malicious behavior on top of that (adding features such as cryptography mechanisms

    where necessary), delegating some responsibility to the compiler. Approaches that

    delegate enforcement of security policy to the compiler and/or the application level areoften called language-based security.

    Hardware-based protection or language-based protection

    Typical computer systems today use hardware-enforced rules about what programs are

    allowed to access what data. The processor monitors the execution and stops a programthat violates a rule (e.g., a user process that is about to read or write to kernel memory,

    and so on). In systems that lack support for capabilities, processes are isolated from each

    other by using separate address spaces.Calls from user processes into the kernel are

    regulated by requiring them to use one of the above-described system call methods.

    An alternative approach is to use language-based protection. In a language-based

    protection system, the kernel will only allow code to execute that has been produced by a

  • 8/14/2019 OS Concepts - Course Material

    16/60

    trusted language compiler. The language may then be designed such that it is impossible

    for the programmer to instruct it to do something that will violate a security requirement.

    Advantages of this approach include:

    Lack of need for separate address spaces. Switching between address spaces is aslow operation that causes a great deal of overhead, and a lot of optimisation work

    is currently performed in order to prevent unnecessary switches in current

    operating systems. Switching is completely unnecessary in a language-basedprotection system, as all code can safely operate in the same address space.

    Flexibility. Any protection scheme that can be designed to be expressed via a

    programming language can be implemented using this method. Changes to the

    protection scheme (e.g. from a hierarchical system to a capability-based one) do

    not require new hardware.

    Disadvantages include:

    Longer application starts up time. Applications must be verified when they are

    started to ensure they have been compiled by the correct compiler, or may needrecompiling either from source code or from byte code.

    Inflexible type systems. On traditional systems, applications frequently perform

    operations that are not type safe. Such operations cannot be permitted in alanguage-based protection system, which means that applications may need to be

    rewritten and may, in some cases, lose performance.

    Examples of systems with language-based protection include JX and Microsoft's

    Singularity.

    Process cooperation

    Edsger Dijkstra proved that from a logical point of view, atomic lock and unlockoperations operating on binary semaphores are sufficient primitives to express any

    functionality of process cooperation. However this approach is generally held to belacking in terms of safety and efficiency, whereas a message passing approach is moreflexible.

    I/O devices management

    The idea of a kernel where I/O devices are handled uniformly with other processes, as

    parallel co-operating processes, was first proposed and implemented by Brinch Hansen

    http://en.wikipedia.org/wiki/JX_(operating_system)http://en.wikipedia.org/wiki/Edsger_Dijkstrahttp://en.wikipedia.org/wiki/JX_(operating_system)http://en.wikipedia.org/wiki/Edsger_Dijkstra
  • 8/14/2019 OS Concepts - Course Material

    17/60

    (although similar ideas were suggested in 1967). In Hansen's description of this, the

    "common" processes are called internal processes, while the I/O devices are calledexternal processes.

    Kernel-wide design approaches

    Naturally, the above listed tasks and features can be provided in many ways that differ

    from each other in design and implementation. While monolithic kernels execute all of

    their code in the same address space (kernel space) to increase the performance of thesystem, micro kernels try to run most of their services in user space, aiming to improve

    maintainability and modularity of the code base. Most kernels do not fit exactly into one

    of these categories, but are rather found in between these two designs. These are calledhybrid kernels. More exotic designs such as nanokernels and exokernels are available, but

    are seldom used for production systems. The Xen hypervisor, for example, is an

    exokernel.

    The principle ofseparation of mechanism and policy is the substantial differencebetween the philosophy of micro and monolithic kernels. Here a mechanism is the

    support that allows to implement many different policies, while a policy is a particular

    "mode of operation". In minimal microkernel just some very basic policies are included,

    and its mechanisms allows what is running on top of the kernel (the remaining part of theoperating system and the other applications) to decide which policies to adopt (as

    memory management, high level process scheduling, file system management, ecc.). A

    monolithic kernel instead tends to include many policies, therefore restricting the rest ofthe system to rely on them.

    Monolithic kernels

    http://en.wikipedia.org/wiki/Nanokernelhttp://en.wikipedia.org/wiki/Exokernelhttp://en.wikipedia.org/wiki/Xenhttp://en.wikipedia.org/wiki/Image:Kernel-simple.pnghttp://en.wikipedia.org/wiki/Nanokernelhttp://en.wikipedia.org/wiki/Exokernelhttp://en.wikipedia.org/wiki/Xen
  • 8/14/2019 OS Concepts - Course Material

    18/60

    Diagram of Monolithic kernels

    In a monolithic kernel, all OS services run along with the main kernel thread, thus alsoresiding in the same memory area. This approach provides rich and powerful hardware

    access. Some developers maintain that monolithic systems are easier to design andimplement than other solutions, and are extremely efficient if well-written. The main

    disadvantages of monolithic kernels are the dependencies between system components - abug in a device driver might crash the entire system - and the fact that large kernels can

    become very difficult to maintain.

    Microkernels

    In the microkernel approach, the kernel itself only provides basic functionality that

    allows the execution of servers, separate programs that assume former kernel functions,

    such as device drivers, GUI servers, etc.

    The microkernel approach consists of defining a simple abstraction over the hardware,

    with a set of primitives or system calls to implement minimal OS services such as

    memory management, multitasking, and inter-process communication. Other services,

    including those normally provided by the kernel such as networking, are implemented inuser-space programs, referred to asservers. Microkernels are easier to maintain than

    monolithic kernels, but the large number of system calls and context switches might slow

    down the system because they typically generate more overhead than plain function calls.

    Microkernels generally underperform traditional designs, sometimes dramatically. This is

    due in large part to the overhead of moving in and out of the kernel, a context switch, to

    move data between the various applications and servers. By the mid-1990s, mostresearchers had abandoned the belief that careful tuning could reduce this overheaddramatically, but recently, newer microkernels, optimized for performance, such as L4

    and K42 have addressed these problems.

    A microkernel allows the implementation of the remaining part of the operating system asa normal application program written in a high-level language, and the use of different

    operating systems on top of the same unchanged kernel. It is also possible to dynamically

    switch among operating systems and to have more than one active simultaneously.

    Monolithic kernels vs microkernels

    As the computer kernel grows, a number of problems become evident. One of the most

    obvious is that the memory footprint increases. This is mitigated to some degree by

    perfecting the virtual memory system, but not all computer architectures have virtualmemory support. To reduce the kernel's footprint, extensive editing has to be performed

    to carefully remove unneeded code, which can be very difficult with non-obvious

    interdependencies between parts of a kernel with millions of lines of code.

    http://en.wikipedia.org/wiki/L4_microkernel_familyhttp://en.wikipedia.org/wiki/K42http://en.wikipedia.org/wiki/L4_microkernel_familyhttp://en.wikipedia.org/wiki/K42
  • 8/14/2019 OS Concepts - Course Material

    19/60

    Due to the problems that monolithic kernels pose, they were considered obsolete by the

    early 1990s. As a result, the design of Linux using a monolithic kernel rather than a

    microkernel was the topic of a famous flame war between Linus Torvalds and AndrewTanenbaum. There is merit on both sides of the argument presented in the

    Tanenbaum/Torvalds debate.

    Some, including early UNIX developer Ken Thompson, argued that while microkernel

    designs were more aesthetically appealing, monolithic kernels were easier to implement.However, a bug in a monolithic system usually crashes the entire system, while this

    doesn't happen in a microkernel with servers running apart from the main thread.

    Monolithic kernel proponents reason that incorrect code doesn't belong in a kernel, andthat microkernels offer little advantage over correct code. Microkernels are often used in

    embedded robotic or medical computers where crash tolerance is important and most of

    the OS components reside in their own private, protected memory space. This isimpossible with monolithic kernels, even with modern module-loading ones. However,

    the monolithic model tends to be more efficient through the use of shared kernel memory,

    rather than the slower IPC system of microkernel designs, which is typically based onmessage passing.

    Hybrid kernels

    The hybrid kernel approach tries to combine the speed and simpler design of a monolithickernel with the modularity and execution safety of a microkernel.

    Hybrid kernels are essentially a compromise between the monolithic kernel approach and

    the microkernel system. This implies running some services (such as the network stack or

    the filesystem) in kernel space to reduce the performance overhead of a traditional

    microkernel, but still running kernel code (such as device drivers) as servers in userspace.

    Nanokernels

    A nanokernel delegates virtually all services including even the most basic ones likeinterrupt controllers or the timer to device drivers to make the kernel memory

    requirement even smaller than a traditional microkernel.

    Exokernels

    An exokernel is a type of kernel that does not abstract hardware into theoretical models.Instead it allocates physical hardware resources, such as processor time, memory pages,

    and disk blocks, to different programs. A program running on an exokernel can link to alibrary operating system that uses the exokernel to simulate the abstractions of a well-known OS, or it can develop application-specific abstractions for better performance. [28]

    http://en.wikipedia.org/wiki/Linus_Torvaldshttp://en.wikipedia.org/wiki/Andrew_S._Tanenbaumhttp://en.wikipedia.org/wiki/Andrew_S._Tanenbaumhttp://en.wikipedia.org/wiki/Filesystemhttp://en.wikipedia.org/wiki/Kernel_(computer_science)#_note-6http://en.wikipedia.org/wiki/Linus_Torvaldshttp://en.wikipedia.org/wiki/Andrew_S._Tanenbaumhttp://en.wikipedia.org/wiki/Andrew_S._Tanenbaumhttp://en.wikipedia.org/wiki/Filesystemhttp://en.wikipedia.org/wiki/Kernel_(computer_science)#_note-6
  • 8/14/2019 OS Concepts - Course Material

    20/60

    Batch processing

    Batch processing is the execution of a series of programs ("jobs") on a computer withouthuman interaction, when possible.

    Batch jobs are set up so they can be run to completion without human interaction, so all

    input data is preselected through scripts or commandline parameters. This is in contrast to

    interactive programs which prompt the user for such input.

    Batch processing has these benefits:

    It allows sharing of computer resources among many users

    It shifts the time of job processing to when the computing resources are less busy

    It avoids idling the computing resources without minute-by-minute human

    interaction and supervision

    It is used on expensive classes of computers to help amortize the cost by keepinghigh rates of utilization of those expensive resources.

    Batch processing has historically been synonymous with mainframe computers. Since

    this class computer was so expensive, batch processing was used for the reasons listed

    above. Also, in the early days of electronic computing, interactive sessions with computerterminal interfaces and later Graphical user interfaces) were not yet widespread.

    Batch processing has grown beyond its mainframe origins, and is now frequently used in

    UNIX environments, where the corn and at facilities allow for scheduling of complex jobscripts. Similarly, Microsoft DOS and Windows systems refer to their command-scripting

    language as batch files and Windows has a job scheduler.

    A popular computerized batch processing procedure is printing. This normally involves

    the operator selecting the documents they need printed and indicating to the batchprinting software when and where they should be output. Batch processing is also used

    for efficient bulk database updates and automated transaction processing, as contrasted to

    interactive online transaction processing (OLTP) applications.

    Multiprocessing

    Multiprocessing is a generic term for the use of two or more central processing units(CPUs) within a single computer system. It also refers to the ability of a system to

    support more than one processor and/or the ability to allocate tasks between them.[1]

    There are many variations on this basic theme, and the definition of multiprocessing can

    vary with context, mostly as a function of how CPUs are defined (multiple cores on one

    die, multiple chips in one package, multiple packages in one system unit, etc.).

    http://en.wikipedia.org/wiki/Job_(software)http://en.wikipedia.org/wiki/Multi-processing#_note-0http://en.wikipedia.org/wiki/Job_(software)http://en.wikipedia.org/wiki/Multi-processing#_note-0
  • 8/14/2019 OS Concepts - Course Material

    21/60

    Multiprocessingsometimes refers to the execution of multiple concurrent software

    processes in a system as opposed to a single process at any one instant. However, the

    term multiprogramming is more appropriate to describe this concept, which isimplemented mostly in software, whereas multiprocessing is more appropriate to describe

    the use of multiple hardware CPUs. A system can be both multiprocessing and

    multiprogramming, only one of the two, or neither of the two.

    Multi-user

    Multi-user is a term that defines an operating system or application software that allows

    concurrent access by multiple users of a computer. Time-sharing systems are multi-user

    systems. Most batch processing systems for mainframe computers may also beconsidered "multi-user", to avoid leaving the CPU idle while it waits for I/O operations to

    complete. However, the term "multitasking" is more common in this context.

    An example is a UNIX server where multiple remote users have access (via Telnet orSSH) to the UNIX shell prompt at the same time. Another example uses multiple X

    sessions spread across multiple monitors powered by a single machine.

    Management systems are implicitly designed to be used by multiple users, typically one

    system administrator or more and an End-user (computer science) community.

    The opposite term, single-user, is most commonly used when talking about an operatingsystem being usable only by one person at a time, or in reference to a single-user

    software license agreement.

    Distributed computing is a method of computer processing in which different parts of aprogram run simultaneously on two or more computers that are communicating with each

    other over a network. Distributed computing is a type of parallel computing. But the latter

    term is most commonly used to refer to processing in which different parts of a program

    run simultaneously on two or more processors that are part of the same computer. Whileboth types of processing require that a program be parallelizeddivided into sections

    that can run simultaneously, distributed computing also requires that the division of the

    program take into account the different environments on which the different sections ofthe program will be running. For example, two computers are likely to have different file

    systems and different hardware components.

    An example of distributed computing is BOINC, a framework in which large problems

    can be divided into many small problems which are distributed to many computers. Later,the small results are reassembled into a larger solution.

    Distributed computing is a natural result of the use of networks to allow computers to

    efficiently communicate. But distributed computing is distinct from networking. The

  • 8/14/2019 OS Concepts - Course Material

    22/60

    latter refers to two or more computers interacting with each other, but not, typically,

    sharing the processing of a single program. The World Wide Web is an example of a

    network, but not an example of distributed computing.

    There are numerous technologies and standards used to construct distributed

    computations, including some which are specially designed and optimized for thatpurpose, such as Remote Procedure Calls

    Real-time operating system

    A real-time operating system (RTOS) is a multitasking operating system intended for

    real-time applications. Such applications include embedded systems (programmable

    thermostats, household appliance controllers, and mobile telephones), industrial robots,spacecraft, industrial control and scientific research equipment.

    An RTOS facilitates the creation of a real-time system, but does not guarantee the finalresult will be real-time; this requires correct development of the software. An RTOS does

    not necessarily have high throughput; rather, an RTOS provides facilities which, if usedproperly, guarantee deadlines can be met generally (soft real-time) or deterministically

    (hard real-time). An RTOS will typically use specialized scheduling algorithms in order

    to provide the real-time developer with the tools necessary to produce deterministicbehavior in the final system. An RTOS is valued more for how quickly and/or predictably

    it can respond to a particular event than for the given amount of work it can perform over

    time. Key factors in an RTOS are therefore a minimal interrupt latency and a minimal

    thread switching latency.

    An early example of a large-scale real-time operating system was the so-called "controlprogram" developed by American Airlines and IBM for the Sabre Airline Reservations

    System.

  • 8/14/2019 OS Concepts - Course Material

    23/60

    Chapter 3

    Operating System components

    Into to Process management sub system

    Into to Memory management sub system

    Intro to File sub system

    Into to Device drivers

    Signals and System calls

    Process management

    The main task of a kernel is to allow the execution of applications and support them withfeatures such as hardware abstractions. To run an application, a kernel typically sets up an

    address space for the application, loads the file containing the application's code into

    memory (perhaps via demand paging), sets up a stack for the program and branches to agiven location inside the program, thus starting its execution.

    Multi-tasking kernels are able to give the user the illusion that the number of processes

    being run simultaneously on the computer is higher than the maximum number of

    processes the computer is physically able to run simultaneously. Typically, the number ofprocesses a system may run simultaneously is equal to the number of CPUs installed

    (however this may not be the case if the processors support simultaneous multithreading).

    In a pre-emptive multitasking system, the kernel will give every program a slice of time

    and switch from process to process so quickly that it will appear to the user as if theseprocesses were being executed simultaneously. The kernel uses scheduling algorithms to

    determine which process is running next and how much time it will be given. The

    algorithm chosen may allow for some processes to have higher priority than others. Thekernel generally also provides these processes a way to communicate; this is known as

    inter-process communication (IPC) and the main approaches are shared memory, message

    passing and remote procedure calls (see concurrent computing).

    Other systems (particularly on smaller, less powerful computers) may provide co-operative multitasking, where each process is allowed to run uninterrupted until it makes

    a special request that tells the kernel it may switch to another process. Such requests are

    known as "yielding", and typically occur in response to requests for interprocess

    communication, or for waiting for an event to occur. Older versions of Windows and MacOS both used co-operative multitasking but switched to pre-emptive schemes as the

    power of the computers to which they were targeted grew.

  • 8/14/2019 OS Concepts - Course Material

    24/60

    The operating system might also support multiprocessing (SMP or Non-Uniform

    Memory Access); in that case, different programs and threads may run on different

    processors. A kernel for such a system must be designed to be re-entrant, meaning that itmay safely run two different parts of its code simultaneously. This typically means

    providing synchronization mechanisms (such as spin locks) to ensure that no two

    processors attempt to modify the same data at the same time.

    Memory management

    The kernel has full access to the system's memory and must allow processes to access this

    memory safely as they require it. Often the first step in doing this is virtual addressing,

    usually achieved by paging and/or segmentation. Virtual addressing allows the kernel tomake a given physical address appear to be another address, the virtual address. Virtual

    address spaces may be different for different processes; the memory that one process

    accesses at a particular (virtual) address may be different memory from what anotherprocess accesses at the same address. This allows every program to behave as if it is the

    only one (apart from the kernel) running and thus prevents applications from crashingeach other.

    On many systems, a program's virtual address may refer to data which is not currently inmemory. The layer of indirection provided by virtual addressing allows the operating

    system to use other data stores, like a hard drive, to store what would otherwise have to

    remain in main memory (RAM). As a result, operating systems can allow programs to usemore memory than the system has physically available. When a program needs data

    which is not currently in RAM, the CPU signals to the kernel that this has happened, and

    the kernel responds by writing the contents of an inactive memory block to disk (if

    necessary) and replacing it with the data requested by the program. The program can then

    be resumed from the point where it was stopped. This scheme is generally known asdemand paging.

    Virtual addressing also allows creation of virtual partitions of memory in two disjointed

    areas, one being reserved for the kernel (kernel space) and the other for the applications(user space). The applications are not permitted by the processor to address kernel

    memory, thus preventing an application from damaging the running kernel. This

    fundamental partition of memory space has contributed much to current designs of actualgeneral-purpose kernels and is almost universal in such systems, although some research

    kernels (e.g. Singularity) take other approaches.

    Device management

    To perform useful functions, processes need access to the peripherals connected to thecomputer, which are controlled by the kernel through device drivers. For example, to

    show the user something on the screen, an application would make a request to the

    kernel, which would forward the request to its display driver, which is then responsiblefor actually plotting the character/pixel.

  • 8/14/2019 OS Concepts - Course Material

    25/60

    A kernel must maintain a list of available devices. This list may be known in advance

    (e.g. on an embedded system where the kernel will be rewritten if the available hardware

    changes), configured by the user (typical on older PCs and on systems that are notdesigned for personal use) or detected by the operating system at run time (normally

    called plug and play).

    In a plug and play system, a device manager first performs a scan on different hardware

    buses, such as Peripheral Component Interconnect (PCI) or Universal Serial Bus (USB),to detect installed devices, then searches for the appropriate drivers.

    As device management is a very OS-specific topic, these drivers are handled differently

    by each kind of kernel design, but in every case, the kernel has to provide the I/O to allow

    drivers to physically access their devices through some port or memory location. Veryimportant decisions have to be made when designing the device management system, as

    in some designs accesses may involve context switches, making the operation very CPU-

    intensive and easily causing a significant performance overhead.

    File sub system

    Computers can store information on several different storage media, such as magnetic

    disks, magnetic tapes and optical disks. So that computer system will be convenient to

    use, the operating system provides a uniform logical view of information storage. Filesare mapped by the operating system on to physical devices. These devices usually

    nonvolatile so contents are persistent.

    Signals

    A signal is a limited form of inter-process communication used in UNIX, Unix-like, andother POSIX-compliant operating systems. Essentially it is an asynchronous notification

    sent to a process in order to notify it of an event that occurred. When a signal is sent to a

    process, the operating system interrupts the processes' normal flow of execution.Execution can be interrupted during any non-atomic instruction. If the process has

    previously registered a signal handler, that routine is executed. Otherwise the default

    signal handler is executing.

    Sending signals

    Typing certain combinations at the controlling terminal of a running process

    causes the system to send it certain signals:o Ctrl-C (in older Unixes, DEL) sends an INT signal (SIGINT); by default,

    this causes the process to terminate.o Ctrl-Z sends a TSTP signal (SIGTSTP); by default, this causes the process

    to suspend execution.o Ctrl-\ sends a QUIT signal (SIGQUIT); by default, this causes the process

    to terminate and dump core.

    http://en.wikipedia.org/wiki/Ctrl-Chttp://en.wikipedia.org/wiki/SIGINT_(POSIX)http://en.wikipedia.org/wiki/Ctrl-Zhttp://en.wikipedia.org/wiki/SIGTSTPhttp://en.wikipedia.org/wiki/Ctrl-/http://en.wikipedia.org/wiki/SIGQUIThttp://en.wikipedia.org/wiki/Core_dumphttp://en.wikipedia.org/wiki/Ctrl-Chttp://en.wikipedia.org/wiki/SIGINT_(POSIX)http://en.wikipedia.org/wiki/Ctrl-Zhttp://en.wikipedia.org/wiki/SIGTSTPhttp://en.wikipedia.org/wiki/Ctrl-/http://en.wikipedia.org/wiki/SIGQUIThttp://en.wikipedia.org/wiki/Core_dump
  • 8/14/2019 OS Concepts - Course Material

    26/60

    Handling signals

    Signal handlers can be installed with the signal () system call. If a signal handler is not

    installed for a particular signal, the default handler is used. Otherwise the signal isintercepted and the signal handler is invoked. The process can also specify two default

    behaviors, without creating a handler: ignore the signal (SIG_IGN) and use the defaultsignal handler (SIG_DFL). There are two signals which cannot be intercepted andhandled: SIGKILL and SIGSTOP.

    Signal handling is vulnerable to race conditions. Because signals are asynchronous,

    another signal (even of the same type) can be delivered to the process during execution of

    the signal handling routine. The sigprocmask() call can be used to block and unblock

    delivery of signals.

    Signals can cause the interruption of a system call in progress, leaving it to the

    application to manage a non-transparent restart.

    System calls

    To actually perform useful work, a process must be able to access the services provided

    by the kernel. This is implemented differently by each kernel, but most provide a C

    library or an API, which in turn invoke the related kernel functions.

    The method of invoking the kernel function varies from kernel to kernel. If memoryisolation is in use, it is impossible for a user process to call the kernel directly, because

    that would be a violation of the processor's access control rules. A few possibilities are:

    Using a software-simulated interrupt. This method is available on most hardware,

    and is therefore very common. Using a call gate. A call gate is a special address which the kernel has added to a

    list stored in kernel memory and which the processor knows the location of. When

    the processor detects a call to that location, it instead redirects to the target

    location without causing an access violation. Requires hardware support, but thehardware for it is quite common.

    Using a special system call instruction. This technique requires special hardware

    support, which common architectures (notably, x86) may lack. System call

    instructions have been added to recent models of x86 processors, however, andsome (but not all) operating systems for PCs make use of them when available.

    Using a memory-based queue. An application that makes large numbers of

    requests but does not need to wait for the result of each may add details ofrequests to an area of memory that the kernel periodically scans to find requests.

    http://en.wikipedia.org/wiki/Sigprocmask_(Unix)http://en.wikipedia.org/wiki/X86http://en.wikipedia.org/wiki/Sigprocmask_(Unix)http://en.wikipedia.org/wiki/X86
  • 8/14/2019 OS Concepts - Course Material

    27/60

    Chapter 4

    Process management sub system

    Creating process

    Process Id, process descriptor

    Process contexts

    Process state

    Process address space

    Process scheduling policies

    Threads vs process

    Multi threading and Hyper threading

    The concept of aprocess is fundamental to any multiprogramming operatingsystem.A process is usually defined as an instance of a program in execution; thus, if 16

    users are running v iat once, there are 16 separate processes (although they can share the

    same executable code). Processes are often called "tasks".

    In this chapter, we will first discuss static properties of processes and then describe how

    process switching is performed by the kernel. The last two sections investigate dynamic

    properties of processes, namely, how processes can be created and destroyed. Thischapter also describes how multithreaded applications are executed.

    Process Descriptor

    In order to manage processes, the kernel must have a clear picture of what each process is

    doing. It must know, for instance, the process's priority, whether it is running on theCPUor blocked on some event, what address space has been assigned to it, which files it

    is allowed to address, and so on. This is the role of theprocess descriptor, that is, of atask_structtype structure whose fields contain all the information related to a singleprocess. As the repository of so much information, the process descriptor is rather

    complex. Not only does it contain many fields itself, but some contain pointers to other

    data structures that, in turn, contain pointers to other structures.

    The Process/Kernel Model

    As already mentioned, a CPU can run either in User Mode or in Kernel Mode. Actually,some CPUs can have more than two execution states. For instance, the Intel 80x86

  • 8/14/2019 OS Concepts - Course Material

    28/60

    microprocessors have four different execution states. But all standard Unix kernels make

    use of only Kernel Mode and User Mode.

    When a program is executed in User Mode, it cannot directly access the kernel data

    structures or the kernel programs. When an application executes in Kernel Mode,

    however, these restrictions no longer apply. Each CPU model provides specialinstructions to switch from User Mode to Kernel Mode and vice versa. A program

    executes most of the time in User Mode and switches to Kernel Mode only when

    requesting a service provided by the kernel.

    When the kernel has satisfied the program's request, it puts the program back in User

    Mode. Processes are dynamic entities that usually have a limited life span within the

    system. The task of creating, eliminating, and synchronizing the existing processes isdelegated to a group of routines in the kernel.

    The kernel itself is not a process but a process manager. The process/kernel model

    assumes that processes that require a kernel service make use of specific programming

    constructs calledsystem calls. Each system call sets up the group of parameters thatidentifies the process request and then executes the hardware-dependent CPU instruction

    to switch from User Mode to Kernel Mode. Besides user processes, Unix systems includea few privileged processes called kernel threads with the following characteristics:

    They run in Kernel Mode in the kernel address space. They do not interact with users, and thus do not require terminal devices. They are usually created during system startup and remain alive until the system isshut down.

    Notice how the process/ kernel model is somewhat orthogonal to the CPU state: on a

    uniprocessor system, only one process is running at any time and it may run either inUser or in Kernel Mode. If it runs in Kernel Mode, the processor is executing some

    kernel routine.

    Figure 1-3 illustrates examples of transitions between User and Kernel Mode. Process 1in User Mode issues a system call, after which the process switches to Kernel Mode and

    the system call is serviced. Process 1 then resumes execution in User Mode until a timer

    interrupt occurs and the scheduler is activated in Kernel Mode. A process switch takesplace, and Process 2 starts its execution in User Mode until a hardware device raises an

    interrupt. As a consequence of the interrupt, Process 2 switches to Kernel Mode and

    services the interrupt.

  • 8/14/2019 OS Concepts - Course Material

    29/60

    Unix kernels do much more than handle system calls; in fact, kernel routines can be

    activated in several ways:

    A process invokes a system call. The CPU executing the process signals an exception, which is some unusual conditionsuch as an invalid instruction. The kernel handles the exception on behalf of the

    process that caused it. A peripheral device issues an interrupt signalto the CPU to notify it of an event such

    as a request for attention, a status change, or the completion of an I/O operation. Eachinterrupt signal is dealt by a kernel program called an interrupt handler. Since

    peripheral devices operate asynchronously with respect to the CPU, interrupts occur at

    unpredictable times.

    A kernel thread is executed; since it runs in Kernel Mode, the corresponding programmust be considered part of the kernel, albeit encapsulated in a process.

    Process Implementation

    To let the kernel manage processes, each process is represented by aprocess descriptor

    that includes information about the current state of the process.

    When the kernel stops the execution of a process, it saves the current contents of several

    processor registers in the process descriptor. These include:

    The program counter (PC) and stack pointer (SP) registers The general-purpose registers

    The floating point registers The processor control registers (Processor Status Word) containing information about

    the CPU state The memory management registers used to keep track of the RAM accessed by the

    process When the kernel decides to resume executing a process, it uses the proper process

    descriptor fields to load the CPU registers. Since the stored value of the program counter

  • 8/14/2019 OS Concepts - Course Material

    30/60

    points to the instruction following the last instruction executed, the process resumes

    execution from where it was stopped.

    When a process is not executing on the CPU, it is waiting for some event. Unix kernels

    distinguish many wait states, which are usually implemented by queues of process

    descriptors; each (possibly empty) queue corresponds to the set of processes waiting for aspecific event.

    Process Address Space

    Each process runs in its private address space. A process running in User Mode refers to

    private stack, data, and code areas. When running in Kernel Mode, the process addresses

    the kernel data and code area and makes use of another stack.Since the kernel is reentrant, several kernel control pathseach related to a different

    processmay be executed in turn. In this case, each kernel control path refers to its own

    private kernel stack.

    While it appears to each process that it has access to a private address space, there aretimes when part of the address space is shared among processes. In some cases this

    sharing is explicitly requested by processes; in others it is done automatically by thekernel to reduce memory usage.

    If the same program, say an editor, is needed simultaneously by several users, the

    program will be loaded into memory only once, and its instructions can be shared by all

    of the users who need it. Its data, of course, must not be shared, because each user willhave separate data. This kind of shared address space is done automatically by the kernel

    to save memory. Processes can also share parts of their address space as a kind of

    interprocess communication, using the "shared memory" technique introduced in SystemV

    Creating a new Task using forkand exec

    The DOS and Windows API contain the spawn family of functions.These functions take as an argument the name of a program to runand create a new process instance of that program. Linux doesntcontain a single function that does all this in one step. Instead, Linuxprovides one function, fork, that makes a child process that is an exactcopy of its parent process.

    In general we never know that child starts executing before or vice versa. This depends

    upon the scheduling algorithm used by the kernel.

    The process state

    As its name implies, the statefield of the process descriptor describes what is currently

    happening to the process. It consists of an array of flags, each of which describes apossible process state. In the current Linux version these states are mutually exclusive,

  • 8/14/2019 OS Concepts - Course Material

    31/60

    and hence exactly one flag ofstateis set; the remaining flags are cleared. The following

    are the possible process states:

    TASK_RUNNING

    The process is either executing on the CPU or waiting to be executed.

    TASK_INTERRUPTIBLE

    The process is suspended (sleeping) until some condition becomes true. Raising a

    hardware interrupt, releasing a system resource the process is waiting for, or

    delivering a signal are examples of conditions that might wake up the process, that is,put its state back toTASK_RUNNING .

    TASK_UNINTERRUPTIBLE

    Like the previous state, except that delivering a signal to the sleeping process leaves

    its state unchanged. This process state is seldom used. It is valuable, however, undercertain specific conditions in which a process must wait until a given event occurs

    without being interrupted. For instance, this state may be used when a process opens a

    device file and the corresponding device driver starts probing for a correspondinghardware device. The device driver must not be interrupted until the probing is

    complete, or the hardware device could be left in an unpredictable state.

    TASK_STOPPED

    Process execution has been stopped: the process enters this state after receiving aSIGSTOP, SIGTSTP, SIGTTIN, orSIGTTOU signal. When a process is being monitoredby another (such as when a debugger executes a ptrace( ) system call to monitor a

    test program), any signal may put the process in theTASK_STOPPED state.

    TASK_ZOMBIE

    Process execution is terminated, but the parent process has not yet issued a wait( )-like system call (wait( ), wait3( ), wait4( ), orwaitpid( )) to return

    information about the dead process. Before the wait( )-like call is issued, the kernel

    cannot discard the data contained in the dead process descriptor because the parent

    could need it.

  • 8/14/2019 OS Concepts - Course Material

    32/60

    The fork(2) system call creates a new process, which begins life in the initial state.

    When the process is fully created fork(2) moves it to ready to run state, where it mustwait to be scheduled eventually the kernel selects it for execution and initiates to a

    context switch. This invokes a kernel routine (switches) that loads the hardware

    context of the process into the system registers and transfer control to the process.From this point the new process behaves like any other process.

    A process running in a user mode enters kernel mode as a result of a system call or an

    interrupts and returns to a user mode when that completes. While executing a system

    call, the process may need to waitfor an event or for a resource that is currentlyunavailable, it does by callingsleep(2) system call which puts process into a queue of

    sleeping process and changes its state to asleep. When the event occurs or the

    resource becomes available, the kernel wakes up the process which now becomes

    ready to run and waits to be scheduled.

    When a process is scheduled to run, it initially runs in kernel mode (kernel runningstate) where it completes the context switch. Initially process exits by calling exit(2)

    system call or because of a signal. In either case, the kernel releases all the resources

    of the process, except for the exit status and the resource usage information and

    leaves process inZombie state. The process remains in this state until its parent callswait(2) system call.

    User

    Running

    Zombi

    Kernel

    Runnin

    Asleep

    Stoppe

    d +

    Initial

    idle

    Readyto run

    Stoppe

    Fork()

    Fork()

    sto c

    ontinue

    System callinterrupt

    Return from Systemcall interrupt

    exit

    wait

    switchwakeu

    wakeu

    slee

    stopsto c

    ontinue

  • 8/14/2019 OS Concepts - Course Material

    33/60

    Process Context

    Each process has a well defined context. This context has several components:

    User address space: This is usually divided in to several components. The

    program text executable code, data, user stack, shared memory regionsand so on.

    Control information: The kernel uses two main data structures to maintain

    control information about the process.

    Credentials: The credentials of the process include the user and group IPs

    associated with it.

    Environment variables: Which are the inherited from the parent.

    Hardware context: This includes the context of the general purpose

    registers and of a set of special system registers. The system registerincludes

    The program counter (PC): Which holds the address of the nextinstruction to execute.

    The stack pointer which contains the address of the upper most

    element of the stack. The processor status word (PSW) which has several status bits

    containing information about system state, such as current and

    previous execution modes, current and interrupt priority levels, andoverflow and carry bits.

    Memory management registers: Which map the address translation tables

    of the process.

    Floating point unit (FPU) registers

    Machine registers contain the hardware context of the currently running process.When context switch occurs is registers are saved in task struct structure.

    Identifying a Process

    Although Linux processes can share a large portion of their kernel data structuresan

    efficiency measure known as lightweight processeseach process has its own processdescriptor. Each execution context that can be independently scheduled must have its

    own process descriptor.

    Lightweight processes should not be confused with user-mode threads, which are

    different execution flows handled by a user-level library. For instance, older Linux

    systems implementedPOSIX threads entirely in user space by means of thepthread

    library; therefore, a multithreaded program was executed as a single Linux process.Currently, the pthread library, which has been merged into the standard C library, takes

    advantage of lightweight processes.

  • 8/14/2019 OS Concepts - Course Material

    34/60

    The very strict one-to-one correspondence between the process and process descriptor

    makes the 32-bit process descriptor address[1] a convenient tool to identify processes.

    These addresses are referred to asprocess descriptor pointers. Most of the references toprocesses that the kernel makes are through process descriptor pointers.

    Any Unix-like operating system, on the other hand, allows users to identify processes bymeans of a number called theProcess ID (orPID). The PID is a 32-bit unsigned integer

    stored in the pid field of the process descriptor. PIDs are numbered sequentially: the PID

    of a newly created process is normally the PID of the previously created processincremented by one. However, for compatibility with traditional Unix systems developed

    for 16-bit hardware platforms, the maximum PID number allowed on Linux is 32767.

    When the kernel creates the 32768th process in the system, it must start recycling the

    lower unused PIDs.

    At the end of this section, we'll show you how it is possible to derive a process descriptor

    pointer efficiently from its respective PID. Efficiency is important because many system

    calls like k i l l ( )use the PID to denote the affected process.

    Process Switching

    In order to control the execution of processes, the kernel must be able to suspend the

    execution of the process running on the CPU and resume the execution of some otherprocess previously suspended. This activity is calledprocess switching, task switching,

    orcontext switching. The following sections describe the elements of process switching

    in Linux:

    Hardware context Hardware support Linux code

    Saving the floating point registers

    Hardware Context

    While each process can have its own address space, all processes have to share the CPU

    registers. So before resuming the execution of a process, the kernel must ensure that eachsuch register is loaded with the value it had when the process was suspended.

    The set of data that must be loaded into the registers before the process resumes its

    execution on the CPU is called the hardware context. The hardware context is a subset ofthe process execution context, which includes all information needed for the process

    execution. In Linux, part of the hardware context of a process is stored in the TSS

    segment, while the remaining part is saved in the Kernel Mode stack. As we learned in ,

    the TSS segment coincides with the tssfield of the process descriptor.

  • 8/14/2019 OS Concepts - Course Material

    35/60

    Swap processes

    If the most deserving process to run is not the current process, then the current

    process must be suspended and the new one made to run. When a process isrunning it is using the registers and physical memory of the CPU and of the

    system. Each time it calls a routine it passes its arguments in registers and may

    stack saved values such as the address to return to in the calling routine. So, whenthe scheduler is running it is running in the context of the current process. It will

    be in a privileged mode, kernel mode, but it is still the current process that is

    running. When that process comes to be suspended, all of its machine state,

    including the program counter (PC) and all of the processor's registers, must besaved in the processes task_struct data structure. Then, all of the machine state

    for the new process must be loaded. This is a system dependent operation, no

    CPUs do this in quite the same way but there is usually some hardware assistance

    for this act.

    This swapping of process context takes place at the end of the scheduler. The

    saved context for the previous process is, therefore, a snapshot of the hardware

    context of the system as it was for this process at the end of the scheduler.Equally, when the context of the new process is loaded, it too will be a snapshot

    of the way things were at the end of the scheduler, including this processes

    program counter and register contents.

    If the previous process or the new current process uses virtual memory then thesystem's page table entries may need to be updated. Again, this action is

    architecture specific. Processors like the Alpha AXP, which use Translation Look-aside Tables or cached Page Table Entries, must flush those cached table entriesthat belonged to the previous process.

    Process scheduling

    Scheduling is a fundamental operating system function. All computer resources are

    scheduled before use. Since CPU is one of the primary computer resources, its scheduling

    is central to the operating system design.

    Scheduling refers to a set of policies and mechanisms supported by operating system that

    controls the order in which the work to be done is completed. A scheduler is an operatingsystem program that selects the next job to be admitted for execution. The main objectiveof scheduling is to increase CPU utilization and the higher output. In multi processing

    environment CPU scheduling is one of the fundamental requirement. This mechanism

    improves the overall efficiency of the computer system by getting more work done in lesstime.

  • 8/14/2019 OS Concepts - Course Material

    36/60

    Types of scheduler

    There are three types of schedulers: Long term, medium term and short term schedulers.

    Long term schedulers sometimes it is also job scheduling. This determines which job

    shall be admitted for immediate processing. There are always more processes that can beexecuted by CPU as a batch processing. These processes are kept in the large storage

    devices like disk for later processing. The long term schedulers select processes from this

    pool and loads into the memory. In memory these processes belong to a ready queue.

    Medium term scheduler: Most of the processes require some I/O operations in that case itmay become suspended for I/O operation after running a while. It is beneficial to remove

    these processes from the main memory to hard disk to make room for other processes. At

    some time later this processes can be reloaded into memory and continue from where itwas left earlier. Saving of suspended processes is said to be swapped out or rolled out.

    The process is swapped in and swaps out by the medium term scheduler.

    End

    Ready Queue CPU

    Suspended

    Queue

    Long term

    scheduler

    Short term

    scheduler

    End of a program

    Ready Queue CPU

    Suspended Queue

    Suspended andswapped out queue

    ng termheduler

    Job

  • 8/14/2019 OS Concepts - Course Material

    37/60

    Medium term scheduler

    Short term scheduler: It allocates processes belonging to ready queue to CPU for

    immediate processing. Its main objective is to maximize CPU requirement. Compared to

    the other two scheduler it is more frequent. It must select a new process for executionquite often because a CPU executes a process only for a few milliseconds before it goes

    for I/O operation. So the short term scheduler is very fast.

    Scheduling Algorithms

    CPU scheduling deals with the problem of deciding which of the processes is in the ready

    queue. In this session several scheduling algorithms.

    Basically there are two types of scheduling

    Pre- emptiveNon- pre emptive

    Non pre-emptive scheduling: If once a process has been given the CPU, CPU cannot

    taken from that process till it completes is called Non preemptive scheduling. In this type

    of scheduling, the jobs are made to wait by longer jobs, but the treatment of processes isfairer.

    Preemptive: In preemptive scheduling the CPU can be taken away by the allocated

    process. It is more useful in high priority, which requires immediate response .

    FCFS

    This is the simplest scheduling, the processes in this scheduling are served in the order

    they arrive. Its implementation is done using FIFO queue. Once the process yields CPU,

    it runs and completes its job, then it releases the CPU. The FCFS is Non Preemptivescheduling which results in poor performance. In this scheduling there is low rate of CPU

    utilization and system throughput. The short jobs have to wait for long time. Consider

    two processes

    Process Execution

    P1 30

    P2 6

    If these processes arrive in the order p1 then p2 the turnout time is 30 and 36units of time

    respectively. Thus giving an average time of (30 + 36)/2 =33 units of time. Theircorresponding waiting time is 15 units of time. However, if the processes arrives in

    reverse order then the turn around time 6 and 36 units with the average 21 units and the

    average waiting time is 0+6/2 = 3.

  • 8/14/2019 OS Concepts - Course Material

    38/60

    Shortest job first scheduling

    In the shortest job scheduling of a job or process is done on the basis its having shortest

    execution time. If the two processes have the same execution time then, FCFS is used for

    example consider the process given below:

    Process CPU time

    P1 4P2 10

    P3 6

    P4 3

    According to the shortest job scheduling the processes are served in the order P4-P1-P3-

    P2 So the waiting time is

    (0+3+7+13)/4=23/4= 5.75units of time.