Document Sample
ppt Powered By Docstoc

              Operating System Structure

CS433-W-2-1                                Tarun Soni
The plan

   The story so far
   •Computer organization
      •I/O and Interrupts
      •Memory hierarchy
   •Operating system organization
      •User view
      •Hardware support
      •Dual mode operation

   This module:
   •Operating system organization

   Reading: Chapter 4 & 5
CS433-W-2-2                         Tarun Soni
OS Structure

     •        System Components
     •        Operating System Services
     •        System Calls
     •        System Programs
     •        System Structure
     •        Virtual Machines
     •        System Design and Implementation
     •        System Generation

CS433-W-2-3                                      Tarun Soni
Common system components

     •        Process Management
     •        Main Memory Management
     •        Secondary-Storage Management
     •        I/O System Management
     •        File Management
     •        Protection System
     •        Networking
     •        Command-Interpreter System

CS433-W-2-4                                  Tarun Soni
Process Management

     • A process is a program in execution. A process
       needs certain resources, including CPU time,
       memory, files, and I/O devices, to accomplish its task.
     • The operating system is responsible for the following
       activities in connection with process management.
        – Process creation and deletion.
        – process suspension and resumption.
        – Provision of mechanisms for:
            • process synchronization
            • process communication

CS433-W-2-5                                            Tarun Soni
Memory Management

    • Memory is a large array of words or bytes, each with
      its own address. It is a repository of quickly
      accessible data shared by the CPU and I/O devices.
    • Main memory is a volatile storage device. It loses its
      contents in the case of system failure.
    • The operating system is responsible for the following
      activities in connections with memory management:
        – Keep track of which parts of memory are currently
          being used and by whom.
        – Decide which processes to load when memory
          space becomes available.
        – Allocate and deallocate memory space as needed.

CS433-W-2-6                                           Tarun Soni
Storage Management

      • Since main memory (primary storage) is volatile and
        too small to accommodate all data and programs
        permanently, the computer system must provide
        secondary storage to back up main memory.
      • Most modern computer systems use disks as the
        principle on-line storage medium, for both programs
        and data.
      • The operating system is responsible for the following
        activities in connection with disk management:
         – Free space management
         – Storage allocation
         – Disk scheduling

CS433-W-2-7                                            Tarun Soni
I/O & File Management

    • The I/O system consists of:
       – A buffer-caching system
       – A general device-driver interface
       – Drivers for specific hardware devices

   • A file is a collection of related information defined by
     its creator. Commonly, files represent programs
     (both source and object forms) and data.
   • The operating system is responsible for the following
     activities in connections with file management:
       – File creation and deletion.
       – Directory creation and deletion.
       – Support of primitives for manipulating files and
       – Mapping files onto secondary storage.
       – File backup on stable (nonvolatile) storage media.
CS433-W-2-8                                            Tarun Soni

     • Protection refers to a mechanism for controlling
       access by programs, processes, or users to both
       system and user resources.
     • The protection mechanism must:
        – distinguish between authorized and unauthorized
        – specify the controls to be imposed.
        – provide a means of enforcement.

CS433-W-2-9                                        Tarun Soni
Distributed systems

     • A distributed system is a collection processors that
       do not share memory or a clock. Each processor has
       its own local memory.
     • The processors in the system are connected through
       a communication network.
     • A distributed system provides user access to various
       system resources.
     • Access to a shared resource allows:
         – Computation speed-up
         – Increased data availability
         – Enhanced reliability

CS433-W-2-10                                        Tarun Soni
Command Interpreter: The human interface

    • Many commands are given to the operating system
      by control statements which deal with:
       – process creation and management
       – I/O handling
       – secondary-storage management
       – main-memory management
       – file-system access
       – protection
       – networking
     • The program that reads and interprets control
       statements is called variously:
        – control-card interpreter
        – command-line interpreter
        – shell (in UNIX)

CS433-W-2-11                                           Tarun Soni
Operating System Services:

    • Program execution – system capability to load a
      program into memory and to run it.
    • I/O operations – since user programs cannot execute
      I/O operations directly, the operating system must
      provide some means to perform I/O.
    • File-system manipulation – program capability to
      read, write, create, and delete files.
    • Communications – exchange of information between
      processes executing either on the same computer or
      on different systems tied together by a network.
      Implemented via shared memory or message
    • Error detection – ensure correct computing by
      detecting errors in the CPU and memory hardware, in
      I/O devices, or in user programs.

CS433-W-2-12                                       Tarun Soni
Operating System Services:

     Additional functions exist not for helping the user, but
     rather for ensuring efficient system operations.
         • Resource allocation – allocating resources to
           multiple users or multiple jobs running at the
           same time.
         • Accounting – keep track of and record which
           users use how much and what kinds of computer
           resources for account billing or for accumulating
           usage statistics.
         • Protection – ensuring that all access to system
           resources is controlled.

CS433-W-2-13                                           Tarun Soni
System Calls

    • System calls provide the interface between a running
      program and the operating system.

    • Three general methods are used to pass parameters
      between a running program and the operating
       – Pass parameters in registers.
       – Store the parameters in a table in memory, and the
         table address is passed as a parameter in a
       – Push (store) the parameters onto the stack by the
         program, and pop off the stack by operating

CS433-W-2-14                                         Tarun Soni
System Programs

     • System programs provide a convenient environment
       for program development and execution. The can be
       divided into:
        – File manipulation
        – Status information
        – File modification
        – Programming language support
        – Program loading and execution
        – Communications
        – Application programs
     • Most users’ view of the operation system is defined
       by system programs, not the actual system calls.

CS433-W-2-15                                        Tarun Soni
System Structure

     • Monolithic
               – MSDOS
     • Layered
               – THE, Unix (sometimes!) : X windows
     • Virtual Machine
               – VM/370, Java, Perl, tcl ?
     • Client-Server
               – Mach, Unix: syslogd ?

     • and anything else you can dream up !

CS433-W-2-16                                          Tarun Soni
System Structure

   • MS-DOS – written to
     provide the most
     functionality in the
     least space
      – not divided into
      – Although MS-
        DOS has some
        structure, its
        interfaces and
        levels of
        functionality are
        not well

CS433-W-2-17                Tarun Soni
System Structure

    • UNIX – limited by hardware functionality, the original
      UNIX operating system had limited structuring. The
      UNIX OS consists of two separable parts.
       – Systems programs
       – The kernel
          • Consists of everything below the system-call
            interface and above the physical hardware
          • Provides the file system, CPU scheduling,
            memory management, and other operating-
            system functions; a large number of functions
            for one level.

CS433-W-2-18                                          Tarun Soni
System Structure: UNIX

CS433-W-2-19             Tarun Soni
System Structure: The layered approach

     • The operating system is divided into a number of
       layers (levels), each built on top of lower layers. The
       bottom layer (layer 0), is the hardware; the highest
       (layer N) is the user interface.
     • With modularity, layers are selected such that each
       uses functions (operations) and services of only
       lower-level layers.

CS433-W-2-20                                            Tarun Soni
System Structure: The layered approach

  • A layered design was first used in THE operating

         layer 5: user programs
         layer 4: buffering for input and output
         layer 3: operator-console device driver
         layer 2: memory management
         layer 1: CPU scheduling
         layer 0: hardware

CS433-W-2-21                                           Tarun Soni
System Structure: OS/2

CS433-W-2-22             Tarun Soni
System Structure: Virtual Machine

   • A virtual machine takes the layered approach to its
     logical conclusion. It treats hardware and the
     operating system kernel as though they were all
   • A virtual machine provides an interface identical to
     the underlying bare hardware.
   • The operating system creates the illusion of multiple
     processes, each executing on its own processor with
     its own (virtual) memory.
   • The resources of the physical computer are shared to
     create the virtual machines.
      – CPU scheduling can create the appearance that
        users have their own processor.

CS433-W-2-23                                         Tarun Soni
System Structure: Virtual Machine

     • The virtual-machine concept provides complete
       protection of system resources since each virtual
       machine is isolated from all other virtual machines.
       This isolation, however, permits no direct sharing of
     • A virtual-machine system is a perfect vehicle for
       operating-systems research and development.
       System development is done on the virtual machine,
       instead of on a physical machine and so does not
       disrupt normal system operation.
     • The virtual machine concept is difficult to implement
       due to the effort required to provide an exact
       duplicate to the underlying machine.
     • if you are going to create a virtual machine, why not
       create the one you want/need rather than the one
       representing the hardware?

CS433-W-2-24                                          Tarun Soni
System Implementation

    • Traditionally written in assembly language, operating
      systems can now be written in higher-level
    • Code written in a high-level language:
       – can be written faster.
       – is more compact.
       – is easier to understand and debug.
    • An operating system is far easier to port (move to
      some other hardware) if it is written in a high-level

CS433-W-2-25                                         Tarun Soni
System Generation

     • Operating systems are designed to run on any of a
       class of machines; the system must be configured for
       each specific computer site.
     • SYSGEN program obtains information concering the
       specific configuration of the hardware system.
     • Booting – starting a computer by loading the kernel.
     • Bootstrap program – code stored in ROM that is able
       to locate the kernel, load it into memory, and start its

CS433-W-2-26                                            Tarun Soni

CS433-W-2-27               Tarun Soni
Concept: What is a process?

     • An operating system executes a variety of programs:
        – Batch system – jobs
        – Time-shared systems – user programs or tasks
     • Process – a program in execution; process execution
       must progress in sequential fashion.
     • A process includes (at least):
        – program counter
        – stack
        – data section

CS433-W-2-28                                        Tarun Soni
 Concept: What is a process?

• OS code and data are in the
  shared address space and are
  shared by all user processes
• Separate kernel stack for
  calls/returns when the process is
  in kernel mode
• Within a user process, both user
  and OS programs may execute
  (more than 1)

 CS433-W-2-29                         Tarun Soni
The state of a process

   • As a process executes, it changes state
      – new: The process is being created.
      – running: Instructions are being executed.
      – waiting: The process is waiting for some event to
      – ready: The process is waiting to be assigned to a
      – terminated: The process has finished execution.

CS433-W-2-30                                        Tarun Soni
The state of a process

CS433-W-2-31             Tarun Soni
 The process control block

 Information associated with each
 • Process state
 • Program counter
 • CPU registers
 • CPU scheduling information
 • Memory-management
 • Accounting information
 • I/O status information

  CS433-W-2-32                      Tarun Soni
UNIX: Process creation

• Every process, except process 0, is created by the fork()
  system call
   – fork() allocates entry in process table and assigns a
     unique PID to the child process
   – child gets a copy of process image of parent: both child
     and parent are executing the same code following fork()
   – but fork() returns the PID of the child to the parent
     process and returns 0 to the child process

CS433-W-2-33                                        Tarun Soni
UNIX: Processes

 • User-level context
    – Process Text (ie: code: read-only)
    – Process Data
    – User Stack (calls/returns in user mode)
    – Shared memory (for IPC)
       • only one physical copy exists but, with virtual
          memory, it appears as it is in the process’s address
 • Register context

CS433-W-2-34                                         Tarun Soni
UNIX: Processes

  • System-level context
     – Process table entry
        • the actual entry concerning this process in the Process
          Table maintained by OS
            – Process state, UID, PID, priority, event awaiting,
              signals sent, pointers to memory holding text,
     – U (user) area
        • additional process info needed by the kernel when
          executing in the context of this process
            – effective UID, timers, limit fields, files in use ...
     – Kernel stack (calls/returns in kernel mode)
     – Per Process Region Table (used by memory manager)

CS433-W-2-35                                         Tarun Soni
Context Switching

• Interrupts
   – Clock
        • process has executed for the maximum allowable time
   – I/O
• Memory fault
   – memory address is in virtual memory so it must be brought
      into main memory

• Trap
   – error occurred
   – may cause process to be moved to Exit state
• Supervisor call
   – such as file open

CS433-W-2-36                                       Tarun Soni
Process switching by the CPU

CS433-W-2-37                   Tarun Soni
Scheduling queues

     • Job queue – set of all processes in the system.
     • Ready queue – set of all processes residing in main
       ready and waiting to execute.
     • Device queues – set of processes waiting for an I/O
     • Process migration between the various queues.

CS433-W-2-38                                         Tarun Soni
Scheduling queues

CS433-W-2-39        Tarun Soni
Scheduling queues

• Long-term scheduler (or job scheduler) – selects which
  processes should be brought into the ready queue.
• Short-term scheduler (or CPU scheduler) – selects which
  process should be executed next and allocates CPU.

CS433-W-2-40                                      Tarun Soni
 Scheduling queues

• Processor is faster than I/O so all processes could be waiting
  for I/O
• Swap these processes to disk to free up more memory
• Blocked state becomes suspend state when swapped to disk
• Two new states
   – Blocked, suspend
   – Ready, suspend

 CS433-W-2-41                                         Tarun Soni
Medium term scheduling

CS433-W-2-42             Tarun Soni
Scheduling and Context switching

    • Short-term scheduler is invoked very frequently
      (milliseconds)  (must be fast).
    • Long-term scheduler is invoked very infrequently
      (seconds, minutes)  (may be slow).
    • The long-term scheduler controls the degree of
    • Processes can be described as either:
       – I/O-bound process – spends more time doing I/O
         than computations, many short CPU bursts.
       – CPU-bound process – spends more time doing
         computations; few very long CPU bursts.
    • When CPU switches to another process, the system
      must save the state of the old process and load the
      saved state for the new process.
    • Context-switch time is overhead; the system does no
      useful work while switching.
CS433-W-2-43                                       Tarun Soni
Process creation

  • Parent process creates children processes, which, in
    turn create other processes, forming a tree of
  • Resource sharing
     – Parent and children share all resources.
     – Children share subset of parent’s resources.
     – Parent and child share no resources.
  • Execution
     – Parent and children execute concurrently.
     – Parent waits until children terminate.

CS433-W-2-44                                        Tarun Soni
Process creation

   • Address space
      – Child duplicate of parent.
      – Child has a program loaded into it.
   • UNIX examples
      – fork system call creates new process
      – execve system call used after a fork to replace the
        process’ memory space with a new program.


CS433-W-2-45                                         Tarun Soni
Process termination

    • Process executes last statement and asks the
      operating system to terminiate it (exit).
       – Output data from child to parent (via wait).
       – Process’ resources are deallocated by operating
    • Parent may terminate execution of children
      processes (abort).
       – Child has exceeded allocated resources.
       – Task assigned to child is no longer required.
       – Parent is exiting.
          • Operating system does not allow child to
            continue if its parent terminates.
          • Cascading termination.

CS433-W-2-46                                        Tarun Soni
Process termination: some causes

• Normal completion                • I/O failure
• Time limit exceeded              • Invalid instruction
• Memory unavailable                   – happens when try to
• Bounds violation                       execute data
• Protection error                 • Privileged instruction
   – example write to read-        • Data misuse
      only file                    • Operating system
• Arithmetic error                   intervention
• Time overrun                         – such as when deadlock
   – process waited longer               occurs
      than a specified             • Parent terminates so child
      maximum for an event           processes terminate
                                   • Parent request

CS433-W-2-47                                        Tarun Soni
Cooperating processes

     • Independent process cannot affect or be affected by
       the execution of another process.
     • Cooperating process can affect or be affected by the
       execution of another process
     • Advantages of process cooperation
        – Information sharing
        – Computation speed-up
        – Modularity
        – Convenience

CS433-W-2-48                                         Tarun Soni
Producer consumer problem

   • Paradigm for cooperating processes, producer
     process produces information that is consumed by a
     consumer process.
      – unbounded-buffer places no practical limit on the
        size of the buffer.
      – bounded-buffer assumes that there is a fixed
        buffer size.

CS433-W-2-49                                        Tarun Soni
Producer consumer: the shared memory solution

   • Shared data
             var n;
             type item = … ;
             var buffer. array [0..n–1] of item;
                 in, out: 0..n–1;
   • Producer process
                 produce an item in nextp
                 while in+1 mod n = out do no-op;
                 buffer [in] :=nextp;
                 in :=in+1 mod n;
             until false;

CS433-W-2-50                                        Tarun Soni
Producer consumer: the shared memory solution

     • Consumer process

                     while in = out do no-op;
                     nextc := buffer [out];
                     out := out+1 mod n;
                     consume the item in nextc
                     until false;
     • Solution is correct, but can only fill up n–1 buffer.

CS433-W-2-51                                                   Tarun Soni
Problems with Processes?

        •Time to create/delete/manage
        •Process switch overhead
        •Overhead on communication between two processes
        •Block on IO => lose processor slice

CS433-W-2-52                                               Tarun Soni

• Resource ownership - process is allocated a virtual address
  space to hold the process image
• Dispatched - process is an execution path through one or more
   – execution may be interleaved with other processes
• These two characteristics are treated independently by the
  modern operating system

• Dispatching is referred to as a thread
• Resource of ownership is referred to as a process or task

 CS433-W-2-53                                        Tarun Soni

     • A thread (or lightweight process) is a basic unit of
       CPU utilization; it consists of:
        – program counter
        – register set
        – stack space
     • A thread shares with its peer threads its:
        – code section
        – data section
        – operating-system resources
        collectively know as a task.
     • A traditional or heavyweight process is equal to a
       task with one thread

CS433-W-2-54                                            Tarun Soni
Single and Multithreaded models

     Thread Control Block contains a register image, thread priority and
     thread state information
CS433-W-2-55                                               Tarun Soni

    • In a multiple threaded task, while one server thread is
          blocked and waiting, a second thread in the same task
          can run.
            – Cooperation of multiple threads in same job confers
               higher throughput and improved performance.
            – Applications that require sharing a common buffer
               (i.e., producer-consumer) benefit from thread
    • Threads provide a mechanism that allows sequential
          processes to make blocking system calls while also
          achieving parallelism.
    • Kernel-supported threads (Mach and OS/2).
    • User-level threads; supported above the kernel, via a set
          of library calls at the user level (Project Andrew from
    • Hybrid approach implements both user-level and kernel-
          supported threads (Solaris 2).
CS433-W-2-56                                                     Tarun Soni
User level Threads

     • The kernel is not aware of the
       existence of threads
     • All thread management is
       done by the application by
       using a thread library
     • Thread switching does not
       require kernel mode privileges
       (no mode switch)
     • Scheduling is application

CS433-W-2-57                            Tarun Soni
 User level Threads

• The kernel is not aware of thread activity but it is still
  managing process activity
• When a thread makes a system call, the whole process will be
• but for the thread library that thread is still in the running state
• So thread states are independent of process states

  CS433-W-2-58                                            Tarun Soni
User level Threads

• Advantages                   • Inconveniences
   – Thread switching does        – Most system calls are
     not involve the kernel:        blocking and the kernel
     no mode switching              blocks processes. So
   – Scheduling can be              all threads within the
     application specific:          process will be blocked
     choose the best              – The kernel can only
     algorithm.                     assign processes to
   – ULTs can run on any            processors. Two
     OS. Only needs a thread        threads within the same
     library                        process cannot run
                                    simultaneously on two

CS433-W-2-59                                     Tarun Soni
Kernel level Threads

 • All thread management is done
   by kernel
 • No thread library but an API to
   the kernel thread facility
 • Kernel maintains context
   information for the process and
   the threads
 • Switching between threads
   requires the kernel
 • Scheduling on a thread basis
 • Ex: Windows NT and OS/2

CS433-W-2-60                         Tarun Soni
Kernel level Threads

 • Advantages                    • Inconveniences
    – the kernel can                – thread switching within
      simultaneously schedule         the same process
      many threads of the same        involves the kernel. We
      process on many                 have 2 mode switches
      processors                      per thread switch
    – blocking is done on a         – this results in a
      thread level                    significant slow down
    – kernel routines can be

CS433-W-2-61                                     Tarun Soni
Combined Approaches

     • Thread creation done in the
       user space
     • Bulk of scheduling and
       synchronization of threads
       done in the user space
     • The programmer may
       adjust the number of KLTs
     • May combine the best of
       both approaches
     • Example is Solaris

CS433-W-2-62                         Tarun Soni

     • Process includes the user’s address space, stack, and
       process control block
     • User-level threads (threads library)
        – invisible to the OS
        – are the interface for application parallelism
     • Kernel threads
        – the unit that can be dispatched on a processor and it’s
          structures are maintain by the kernel
     • Lightweight processes (LWP)
        – each LWP supports one or more ULTs and maps to
          exactly one KLT
        – each LWP is visible to the application

CS433-W-2-63                                          Tarun Soni
Threads in Solaris

CS433-W-2-64         Tarun Soni
Interprocess Communication (IPC)

    • Mechanism for processes to communicate and to
      synchronize their actions.
    • Message system – processes communicate with each
      other without resorting to shared variables.
    • IPC facility provides two operations:
        – send(message) – message size fixed or variable
        – receive(message)
    • If P and Q wish to communicate, they need to:
        – establish a communication link between them
        – exchange messages via send/receive
    • Implementation of communication link
        – physical (e.g., shared memory, hardware bus)
        – logical (e.g., logical properties)

CS433-W-2-65                                      Tarun Soni
Direct IPC

     • Processes must name each other explicitly:
        – send (P, message) – send a message to process P
        – receive(Q, message) – receive a message from
          process Q
     • Properties of communication link
        – Links are established automatically.
        – A link is associated with exactly one pair of
          communicating processes.
        – Between each pair there exists exactly one link.
        – The link may be unidirectional, but is usually bi-

CS433-W-2-66                                          Tarun Soni
Indirect IPC

    • Messages are directed and received from mailboxes
          (also referred to as ports).
            – Each mailbox has a unique id.
            – Processes can communicate only if they share a
    • Properties of communication link
            – Link established only if processes share a
              common mailbox
            – A link may be associated with many processes.
            – Each pair of processes may share several
              communication links.
            – Link may be unidirectional or bi-directional.
    • Operations
            – create a new mailbox
            – send and receive messages through mailbox
CS433-W-2-67– destroy a mailbox                             Tarun Soni
Queue Buffering

    • Queue of messages attached to the link; implemented
      in one of three ways.
       1. Zero capacity – 0 messages
          Sender must wait for receiver (rendezvous).
       2. Bounded capacity – finite length of n messages
          Sender must wait if link full.
       3. Unbounded capacity – infinite length
          Sender never waits.

   Exceptions and errors
   • Process terminates
   • Lost messages
   • Scrambled Messages

CS433-W-2-68                                       Tarun Soni
  • Homework ideas
  1. Create and delete a process: Make sure you can “see”
     child and parent processes separately: fork(), exec()
     and the use of returned PID for distinguishing parent
     from child.
  2. Make the parent process wait() on the end of the child
     process; make the child process an infinite loop; go
     to a different shell and kill the child process;
  3. Create and delete 10K processes. Calculate average
     time to create and delete a process.
  4. Create threads inside a process. Repeat 3 for threads.
  5. Write producer-consumer example in threads with
     one thread producing and another consuming data.
     Prove to yourself that this is working as you

CS433-W-2-69                                          Tarun Soni

    •     OS Components and services
    •     OS structure: layers etc.
    •     Processes
    •     Scheduling, control blocks
    •     Process control: creation, deletion
    •     Producer-consumer model
    •     Resource allocation vs. Dispatching
    •     Threads
    •     Inter-process Communication

CS433-W-2-70                                    Tarun Soni

Shared By: