Transcript Document

Phones OFF Please
Unix Kernel
Parminder Singh Kang
Home: www.cse.dmu.ac.uk/~pkang
Email: [email protected]
1. Kernel:
•
kernel is the central component of operating system.
•
As a basic component of an operating system,
•
kernel can provide the lowest-level abstraction layer for the
resources.
•
Applications and processes use these resources through
IPC mechanisms and System calls.
Shell
Kernel
Hardware;
CPU, I/O, Memory
and Storage Devices
2. Unix Kernel
• The Unix Kernel is monolithic in design.
• When it was first written it was fairly small - about 16K bytes.
• It has grown in size as more and more functions have been added.
• Berkeley 4.2 was about one megabytes long.
• BSD 4.3 kernel was about 48000 lines of C and 68000 lines of assembler.
• The Kernel can be split into two sections :• machine dependent
• and machine independent
User Program
Libraries
trap
trap
User level
Kernel level
System call Interface
Process Control subsystem
File Subsystem
IPC
Scheduler
Memory Management
Buffer cache
Character/block
device drivers
Hardware control
Kernel level
Hardware
Hardware level
• Machine dependent part
o main function is hardware control.
o incorporating low-level I/O, device drivers, interrupt handlers,
context switching and some memory management.
• Machine Independent Part
o The machine independent is the rest.
o Processes communicate with the system via system calls; implemented with a
trap or software interrupt instruction.
o Every process has a user part and a system part.
o The system part is activated when a system call is made.
o The Kernel has its own stack and registers.
o The user stack contains arguments, local variables etc - a fairly standard
stack frame in fact.
o Each process has unique process ID.
o Processes are organised into process groups.
o A process inherits its group from its parent, although it can change its group.
swapper
process zero
schedules transfers of processes between memory
and backing store.
init
process 1
fires off login processes and shells for
each terminal.
page daemon
process 2
deals with creating free pages in a virtual
memory system.
3. Kernel Structure
• Key structure for the kernel is the process table; contains an entry for every
process.
• It is of fixed size - an array (Most kernel structures were initially designed to be
simple and are fixed size tables.
• To make it manageable it has pointers to other tables.
o The process table contains information which must be accessible to
the kernel.
o The user area contains fields relating to the current process which can
be swapped to disk.
user area
------------------|
------>|
|
| P1
| |
|
|
-------|--------.--|
--------|
|
|--------|--|
--------|
|
| P2
| |
|
|
|
|
| .
------>|
.------> |
|
| .
|
|
|
|
|
| Pn
|
|
|
|
|
-------------------------process table
per process
memory
region table
Process table entries:Scheduling details
process priority, amount of CPU time used, recently used,
sleeping etc.
Identifiers
unique process id, parent's process id, user id.
Memory usage
how much space process is using and where it is - memory
or disk. This will be pointers to processes page tables, size of
process page tables, location of user area page tables etc.
Signals
signals sent to the process but not yet dealt with. Masks
showing which signals are being caught, ignored etc.
Sync
event process is awaiting
Resource accounting
pointer to usage, pointer to disk usage.
Timer management
amount of real-time until timer expires.
The user area :• machine registers in user state;
• system call state - parameters and results;
• file descriptor table - files process has open;
• i/o parameters - amount of data to transfer etc;
• current directory;
• permission modes for creating files;
• accounting information - user time, system time;
• kernel stack.
4. Variables
• Global variables - the process table, data buffers etc - hence are NOT re-entrant.
I.e. process switches can only take place at certain points.
• Kernel itself is not preemptable. I.e. kernel processes run to completion, or
until they block.
• For re-entrant it must not have any global variables.
• Local variables must be located on a stack frame local to that invocation of the
procedure.
• User processes - pre-emptive scheduling. I.e. CPU can be forcibly taken away
from process and new process set running. Runs on user stack in
user space. Unprivileged.
• Kernel - hardware independent - runs until completed or blocked. Can block to
wait for resources. Runs on kernel stack in kernel mode. Privileged.
• Kernel - hardware dependent - Never scheduled. Cannot block. Deals with
interrupts. Runs on kernel stack, in kernel address space.
5. Interrupts: High priority
Machine errors
Clock
Disk
Network
Terminals
Low priority
Software Interrupt
• Care has to be taken. If an interrupt occurs while top half of kernel running
race conditions can arise.
• E.g. kernel accessing a buffer cache and interrupt occurs with i/o device.
• Device could modify cache. Some data structures are shared between top and
bottom half of kernel. In this case the top half has to disable interrupts when it
is running critical code.
6. Sleep And Wakeup
• When process is executing in the kernel it may need to wait for some event.
• To do this it executes the sleep routine.
• Process state changed to blocked and scheduler selects other process.
• When the event occurs the interrupt routine executes the wakeup routine.
I.e. process state changed from blocked to runnable.
• Several processes may be waiting for the same event - all are made runnable.
• Interrupts are disabled while sleep and wakeup are executing.
• Kernel sets the priority of the sleeping process according to the reason
for sleeping.
process A
|
v
buffer locked
sleeps
process B
:
:
|
|
v
buffer locked
sleeps
process C
:
:
:
:
:
|
|
v
buffer locked, sleeps
- buffer unlocked. A, B, C woken
ready to run
:
:
:
:
Runs - buffer
locked - sleeps
ready to run
Runs - locks
buffer
:
sleeps for
some reason
ready to run
:
:
:
:
:
Runs - buffer
locked - sleeps
7. Context Switching
• Can be done :o when process puts itself to sleep.
o when process exits.
o when it is about to return from a system call when interrupt occurs
in user mode.
• Kernel makes sure the state of its data structures is consistent
before doing switch.