Major Services Provided by an Operating System - PowerPoint by ebh18594


									 Major Services Provided by an
       Operating System
 process management and scheduling
 main-memory management
 secondary-memory management
 input/output system management,
including interrupt handling
 file management
 protection and security
 command interpretation
           CPU Interrupt

Transfers execution to service routine
associated with the interrupt

Interrupt vector: array of addresses to
service routines (stored at beginning of
Operating System Design Factors
Ease of Use
Resource Utilization

Other goals:
   Easy to extend
   Portable
   Easy to install/uninstall
      Dual-Mode Operation
In dual-mode operation, there are two separate
modes: monitor mode (also called 'system mode'
and 'kernel mode') and user mode.

In monitor mode, the CPU can use all
instructions and access all areas of memory.

In user mode, the CPU is restricted to
unprivileged instructions and a specified area of
        C Library to System Call
              #include <stdio.h>
              int main()
                     printf(“Hello World”);
                     return 0;

User mode
                     standard C library

Kernel mode

                        system call
Program code (aka text section)
Current activity:
   Program counter
   CPU register contents
   Process stack:
      Function parameters, return addresses, local variables
Data section:
   Global Variables
   Heap - Memory dynamically allocated to the process
    during the process run time
             PCB in Linux

pid_t pid; /* process identifier */
long state; /* state of the process */
unsigned int time_slice /* scheduling info */
struct files_struct *files; /* list of open files */
struct mm_struct *mm; /* address space */
 new                              terminated

 admitted        interrupt       exit

         ready               running

I/O or event                     I/O or wait
    done          waiting
    process P0             OS             process P1

executing         interrupt/system call

                  save state into PCB0

                 reload state from PCB1

                  interrupt/system call

                  save state into PCB1

                 reload state from PCB0
            Process Creation
A process can create another process:
   A parent process creates a child process

When a child process is created, the parent
process may either:
   Continue to execute concurrently with the child

   Wait until some or all of its children have terminated.
    Child processes whose parent terminates first are
    called orphan processes.
 UNIX Process Creation
                   process running

parent                        child

  wait()                     exit()

#include <sys/types.h> // for pid_t
#include <stdio.h>
#include <unistd.h>

int main()
       pid_t returned_pid = fork();

      if (returned_pid == 0)
             /* do child stuff */
      else if (returned_pid > 0)
             wait(NULL); /* wait for child */
             printf(“Child terminated!”);
The exec family of system calls is used to
replace the current process's program.
An exec system call loads a binary file into
the text section of the current process.
Now the process is running a different
An exec system call is often called (by the
child) after the fork() system call.
            Parent PCB                                             Child PCB

Program A    010001                                    Program A    010001
  Code       010001              fork()                  Code       010001

                                           Child PCB

                               Program B    010001       exec()
                                 Code       011110


#include <sys/types.h> // for pid_t
#include <stdio.h>
#include <unistd.h>

int main()
       pid_t returned_pid = fork();

      if (returned_pid == 0)
             execlp(“ls”, “ls”, (char *) NULL);
      else if (returned_pid > 0)
             wait(NULL); /* wait for child */
             printf(“Child terminated!”);
Interprocess Communication
Shared Memory:
   A region of memory is allocated and shared
    by cooperating processes.

Message Passing:
   Messages are exchanged between
    cooperating processes
        Process Scheduling
   Have a process running on the CPU at all

 The Process Scheduler is in charge of
selecting the next process to start
executing on the CPU.

 Preemptive scheduling: remove a process
from the CPU before it is finished.
        Process Scheduling

The scheduler maintains various queues
of processes:

   JOB QUEUE: As a process enters the
    system, it is placed in a "job queue”

   READY QUEUE: All processes residing in
    main memory that are ready and waiting are
    kept in a list called the ready queue.
            Scheduling Criteria
CPU Utilization:
   0 to 100%
   Number of processes completed per time unit.
Turnaround Time:
   Interval of time when a process enters the system until it exits.
Waiting Time:
   sum of time spent in the waiting queue (for a particular process).
Response Time:
   time from the submission of a request (ex. I/O request) until the
    first response to the request is produced.
       Scheduling Algorithms
First-Come, First-Served (FCFS)
   The process that requests CPU first gets the
    CPU first

Shortest-Job-First Scheduling (SJF)
   Give the CPU to the process that has the
    smallest next CPU burst
   Approximate with exponential average
      pn+1 = a * tn + (1 - a)pn
       Scheduling Algorithms
Priority Scheduling:
   Each process is assigned a priority, and the
    CPU is allocated to the process with the
    highest priority.

Round-Robin Scheduling:
   Define a time quantum to be a small unit of
    time (10-100 milliseconds) –
   The scheduler goes around the ready queue,
    allocating the CPU to each process for some
    time less than or equal to the time quantum
              Memory Hierarchy
        Address Binding
Compile time: absolute addresses are
bound at compile time; must know where
the process will reside in memory.
Load time: the absolute addresses are
determined from the relocatable
addresses when the program is loaded.
 Execution time: the absolute addresses
are generated as the program runs.
A process can be swapped temporarily out
of memory to backing store (disk), and
then subsequently brought back into
memory for continued execution

When swapping is used, the ready queue
contains all ready processes whose
images are on the backing store or in
          Memory Allocation
External Fragmentation:
   As processes are loaded and removed from
    memory, the free space is broken into pieces
    or fragments. –

External fragmentation exists when the
total free memory is large enough to fullfill
a request, but the free space is not
     Internal fragmentation
To combat external fragmentation, some
systems will allocated memory is fixed-
sized blocks.

In this scheme, the memory allocated may
be larger than the request

The wasted memory = internal
          Simple Paging
Partition physical memory into fixed-sized
blocks called frames.

Partition logical memory into same-sized
blocks called pages.

When a process is to be executed, its
pages are loaded into available memory
frames in the backing store.
               Simple Paging
A logical address is divided into two parts:
   A page number (p)
   A page offset (d)
   p is used to index the page table: f= table(p)
   The address f,d is used to index physical memory

How to calculate p and d:
   Logical address space is 2m
   Page size is 2n
   Use the (m-n) higher-order bits of a logical address
    for p, and the rest of the bits for d
                  Simple Paging
Logical Memory
   0    a
                                                Physical Memory
                            Page Table            0
   1    b
                              0   5               4    Ijkl
   2    c
                                                  8    mnop
   3    d                     1   6
   4    e                                         16
                              2   1
   5    f                                         20   abcd
                              3   2
   6    g                                         24   efgh
   7    h                                         28

   8    I        Logical Address Space = 24
   9    j
   10   k
                 Page size = 22
   11   l
                 => Use first (4-2) = 2 high-order bits
   12   m
   13   n
                    from logical address for p, the page table
   14   o            index
   15   p        => Use remaining 2 bits for d, the page
                    Simple Paging
Logical Memory
   0    a
                                                       Physical Memory
                                 Page Table              0
   1    b
                                   0     5               4    Ijkl
   2    c
                                                         8    mnop
   3    d                          1     6
   4    e                                                16
                                   2     1
   5    f                                                20   abcd
                                   3     2
   6    g                                                24   efgh
   7    h                                                28

   8    I
   9    j
                 Ex. Logical address 11
   10   k        1110 = 10112
   11   l
   12   m        p = 102 = 210         => page table 110 = 012
   13   n
   14   o        d = 112               => physical address = 01112
   15   p                                 = 710
        Simple Segmentation
An address is specified by a segment
name/number and a segment offset
Typically a compiler automatically constructs
segments from the source code
 Ex. A C compiler may create segments for:
   The code
   Global variables
   The heap
   The stacks
   Demand Paging and Virtual
Memory management technique that
allows execution of process that are not
completely in memory.

Abstracts main memory into an extremely
large array of storage
        Demand Paging
Pages are loaded when they are
demanded during the execution of a

OS program that swaps in demand-paged
virtual memory system is called pager
             Demand Paging
What happens when a process tries to access a
page that was not brought into memory by the
   Paging hardware transfers control to OS
   Check internal table in PCB to ensure the memory
    access is valid
   Find a free frame in main memory
   Schedule a disk operation to read the page into the
    allocated frame
   When page is copied into frame, update PCB and
    page table
   Restart the instruction that was interrupted by the trap
Effective Memory Access Time
Define the following:
   Let ma be the memory-access time
   Let p be the probability of a page fault
   Let f be the page-fault time

effective access = (1 - p) X ma + p X f
When a child process is created, instead
of copying the pages of the parent, the
child and parent can simply share the
pages of the parent.

When the either the parent or the child
attempts to write to a page, the page is
first copied, and that copied page is used
by the process instead
   Used in Windows XP, Linux, and Solaris
       Page Replacement
Suppose a page needs to be placed in

A frame is requested, but no free-frames
are available

In this situation, we need to remove a
page from memory to free-up a frame
   Basic Page Replacement
Find a frame whose page is not currently
in use.

Write the contents (page) of the frame to

Update page table and frame tables.
Page Replacement Algorithms
First-In First-Out (FIFO)
   Each page has a time stamp when it was
    brought into memory.
   When a page must be replaced, the oldest
    page is chosen.

Least-Recently-Used (LRU)
   Replace the page that has not been used for
    the longest period of time.
   Each page has an associated time of that
    page's last use.
Page Replacement Algorithms
   Many hardware systems do not have support
    for the LRU page replacement algorithm
Second-Chance Algorithm:
   Use FIFO selection, but if the selected page
    has reference bit equal to 1, then the
    reference bit is set to 0, and the arrival time is
    set to the current time. Then consider next
    FIFO page.
Thrashing occurs when the processes are not
allocated enough frames, and there is a large
number of page-faults

CPU utilization drops as the system spends
most of its time performing paging operations

A process is thrashing if it is spending more time
paging then executing

To top