Kernel : Activities - PowerPoint PPT Presentation

1 / 6
About This Presentation
Title:

Kernel : Activities

Description:

page table entries etc. can be tagged to indicate they can only be changed if ... to handle interrupts - these will be tagged as only modifiable in kernel mode. ... – PowerPoint PPT presentation

Number of Views:46
Avg rating:3.0/5.0
Slides: 7
Provided by: michae64
Category:

less

Transcript and Presenter's Notes

Title: Kernel : Activities


1
Kernel Activities
  • What it does
  • Deals directly with hardware
  • Handles interrupts
  • Switches between threads/processes
  • Sets up/manages memory page tables for processes
  • Ensures protection of processes from each other
  • Provides operating system functions that can be
    called by users
  • Problems
  • Hardware - must control the use of hardware,
    but
  • Switching between threads - remember deadlock,
    synchronisation, mutual exclusion, etc. Which
    thread to schedule next, priority ?
  • Memory management got to set up page
    tables, handle page faults, etc.
  • Protection but kernel is just
    software anything it can do user software can
    do ? Hopefully no!
  • Hardware to the rescue modern processors
    provides special modes

2
Kernel Modes
  • Using virtual memory, each process can only
    access the physical page frames in RAM that are
    given in its page tables, so if these are set up
    correctly, a process cannot affect another
    process memory.
  • Looks good, but whats to stop a process from
    changing its own page tables now it could mess
    up any other process, or the OS itself?
  • Also, a threads must be able to do input/output
    to hardware devices. But if threads can issue I/O
    instructions whenever they want, result is likely
    to be a mess.
  • Must ensure that only the kernel can modify
    memory management tables etc., and that only the
    kernel can directly access hardware.
  • Hardware support is needed to distinguish between
    when the kernel is executing code, and when it is
    just a user thread.
  • Done by processor having at least two modes, and
    adding access permissions to resources such as
    page tables and the TLB
  • kernel mode all machine instructions can be
    used
  • user mode I/O instructions and mode change
    instructions not allowed
  • page table entries etc. can be tagged to
    indicate they can only be changed if processor is
    in kernel mode

3
Kernel Entering
  • So how to get into kernel mode from user mode
    ? Cant be done by a user mode instruction (it
    would be pointless if it could)
  • Done automatically by hardware when a hardware or
    software interrupt occurs, before entering the
    interrupt handling routine.
  • The interrupt handling routine in the kernel can
    now deal with the interrupt, then transfer
    control to maybe schedule a different thread, set
    up new page tables, pass data to an input/output
    device, or whatever. It can use any resource on
    the machine, and change anything.
  • Lastly, or thereabouts, the previous mode is
    restored.
  • If this is user mode, the thread wont be able to
    change anything not belonging to itself,
    particularly page table entries and the code used
    to handle interrupts - these will be tagged as
    only modifiable in kernel mode. Even if it goes
    crazy, it will only be able to access the memory
    allocated to it in its page tables, and the OS
    will still be able to break in on it (timer
    interrupt) and schedule another thread.
  • Software interrupts happen as part of the
    execution of a program and are often referred to
    as exceptions or traps. They may occur due to
    an error such as e.g. divide by zero. By
    specifying which interrupt and using a trap
    instruction a user thread can access an OS kernel
    function.
  • (Hardware interrupts occur asynchronously to the
    execution of the program, i.e. can happen any
    time)

4
Kernel Thread Queues
  • Remember, each process has a PCB, giving
  • Its name (PID) and the resources allocated to it,
    including open files and memory. Each process has
    its own set of page tables.
  • The execution state of each of its threads, i.e.
    register values and status, perhaps as a list of
    TCBs. All its threads share the same memory.
  • The kernel must schedule the threads. It
    typically has at least two queues
  • Runnable a queue of TCBs for threads that are
    ready to run (there may be many such queues, one
    per priority level)
  • Blocked a queue of threads waiting to access a
    device or semaphore or other OS construct (there
    may be many such queues, one per item)
  • When a time slice expires, the current thread is
    suspended, and its updated TCB put at the end of
    its priority queue. A TCB from the highest
    priority queue is then used to set up the
    registers and continue another thread. Which TCB
    depends on the scheduling approach taken.
  • When a resource on which a thread is waiting
    becomes available and interrupts (either hardware
    or trap), one of the TCBs on that threads
    blocked queue is transferred to the runnable
    queue, and the scheduler is called to decide
    which runnable thread to run next.
  • So a thread is either running, runnable, or
    blocked

5
Kernel Scheduling
  • In some systems, a thread is allowed to keep
    running until it does an OS call the thread
    decides when the OS can take over
    nonpreemptive.
  • Various scheduling approaches are used, trying to
    achieve fast response times, good processor
    utilisation, fairness, and predicability among
    other things.
  • One trick often used is to increase priority when
    a thread is blocked on input/output (I/O bound),
    and decrease priority when a threads time slice
    runs out, so that compute bound threads move
    into the background. This can help ensure a fast
    response time for programs with a lot of user
    interaction.
  • In real time operating systems, as in cars,
    planes, and Mars explorers, it is essential that
    critical deadlines are met, and priorities are
    set with this in mind, and might not be changed
    at all. However, priority inheritance may be
    needed, where a low priority thread holding a
    resource that a high priority thread wants is
    changed to that high priority to prevent a medium
    priority thread getting in and holding up the
    high priority one indirectly.
  • On many systems, processes can be swapped out
    to disc in their entirety, freeing up RAM but
    adding more queues and further issues for the
    scheduler.
  • Some of these issues are addressed in next years
    course.

6
Kernel API
  • The kernel Application Programming Interface
    (API) allows programmers to access functions
    provided by the kernel. Typical examples
  • sem_init() return a semaphore initialised to a
    given value
  • sem_wait() do wait operation on given semaphore
  • sem_post() do signal operation on given
    semaphore
  • mq_open() open a message queue
  • mq_send() put message on message queue, wakeup
    waiting
  • mq_receive() get something off queue, wait if
    empty
  • Have a look at the Posix IPC standard to see lots
    more.
  • Essentially, the OS provides a virtual machine
    with various instructions such as the above
    the programmer should not have to worry about the
    underlying hardware.
  • A microkernel provides support just for process
    and thread switching, virtual memory, and
    interprocess communication. Other OS functions
    can be built on top of this, in particular a file
    system and support for communications and
    networking
Write a Comment
User Comments (0)
About PowerShow.com