UNIX The Kernel 1 UNIX Internals Motivations Knowledge

  • Slides: 23
Download presentation
UNIX – The Kernel 1

UNIX – The Kernel 1

UNIX Internals: Motivations • Knowledge of UNIX Internals helps in: – understanding similar systems

UNIX Internals: Motivations • Knowledge of UNIX Internals helps in: – understanding similar systems (for example, NT, LINUX) – designing high performance UNIX applications 2

WHAT IS THE KERNEL? • Part of UNIX OS that contains code manage and

WHAT IS THE KERNEL? • Part of UNIX OS that contains code manage and control os , Function : – controlling execution of processes (creation, termination, suspension, communication) – scheduling processes fairly for execution on the CPU. – allocating main memory for exec of processes. – allocating secondary memory for efficient storage and retrieval of user data. – Handling peripherals such as terminals, tape drives, disk drives and network devices. 3

Kernel Characteristics: • Kernel loaded into memory and runs until the system is turned

Kernel Characteristics: • Kernel loaded into memory and runs until the system is turned off or crashes. • Mostly written in C with some assembly language written for efficiency reasons. • User programs make use of kernel services via the system call interface. • Provides its services transparently. 4

Kernel Subsystems • File system – Directory hierarchy, regular files, peripherals – Multiple file

Kernel Subsystems • File system – Directory hierarchy, regular files, peripherals – Multiple file systems • Process management – How processes share CPU, memory and signals • Input/Output – How processes access files, terminal I/O • Interprocess Communication • Memory management System V and BSD have different implementations of different subsystems. 5

TALKING TO THE KERNEL • Processes accesses kernel facilities via system calls • Peripherals

TALKING TO THE KERNEL • Processes accesses kernel facilities via system calls • Peripherals communicate with the kernel via hardware interrupts. 6

EXECUTION IN USER MODE AND KERNEL MODE • Kernel contains several data structures needed

EXECUTION IN USER MODE AND KERNEL MODE • Kernel contains several data structures needed for implementing kernel services. These structures include: • Process table: contains an entry for every process in the system • Open-file table, contains at least one entry for every open file in the system. 7

Execution in kernel mode and user mode • When a process executes a system

Execution in kernel mode and user mode • When a process executes a system call, the execution mode of the process changes from user mode to kernel mode. • Different between user mode and kernel mode – Processes in user mode can access their own instructions and data but not kernel instructions and data structures. – In kernel mode, a process can access system data structures, such as the process table. 8

Flow of Control during a System call • User process invokes a system call

Flow of Control during a System call • User process invokes a system call (for example open( )) • Every system call is allocated a code number at system initialization. – C runtime library version of the system call places the system call parameter and the system call code number into machine registers and then executes a trap machine instruction switching to kernel code and kernel mode. 9

Flow of control of a system call • trap instruction uses the system call

Flow of control of a system call • trap instruction uses the system call number as in index into a system call vector table (located in kernel memory) which is an array of pointers to the kernel code for each system call. • Code corresponding to system call executes in kernel mode, modifying kernel data structures if necessary. • Performs special "return" instruction that flips machine back into user mode and returns to the user process's code 10

SYNCHRONOUS VS ASYNCHRONOUS PROCESSING • Usually, processes performing system calls cannot be preempted. •

SYNCHRONOUS VS ASYNCHRONOUS PROCESSING • Usually, processes performing system calls cannot be preempted. • Processes must relinquish voluntarily the CPU for example while waiting for I/O to complete. • Kernel sends a process to sleep and will wake it up when I/O is completed. • The scheduler does not allocate sleeping process any CPU time and will allocate the CPU to other processes while the hardware device is servicing the I/O request. 11

INTERRUPTS AND EXCEPTIONS • UNIX system allows devices such as I/O peripherals and clock

INTERRUPTS AND EXCEPTIONS • UNIX system allows devices such as I/O peripherals and clock to interrupt CPU asynchronously. • On receipt of the interrupt, kernel saves its current context (frozen image of what the process was doing), determines cause of interrupt and services the interrupt. • Devices are allocated an interrupt priority based in their relative importance. • When the kernel services an interrupt, it blocks out lower priority interrupts but services higher priority interrupts 12

PROCESSOR EXECUTION LEVELS • Kernel must sometimes prevent the occurrence of interrupts during critical

PROCESSOR EXECUTION LEVELS • Kernel must sometimes prevent the occurrence of interrupts during critical activity to avoid corruption of data. • Typical Interrupt Levels • Machine Errors • Clock • Higher priority • Disk • Network Devices • Terminals • Software Interrupts Lower priority 13

Interrupts • Interrupts are serviced by kernel interrupt handlers which must be very fast

Interrupts • Interrupts are serviced by kernel interrupt handlers which must be very fast to avoid loosing any interrupts. • If an interrupt of higher priority occurs while a lower interrupt is services, nesting will occur and higher interrupt is serviced. 14

THE PROCESS SUBSYSTEM: process states • Every process on the system can be in

THE PROCESS SUBSYSTEM: process states • Every process on the system can be in one of 6 states: – running: process is currently using the CPU – runnable: ready to run, will run depending on priority – sleeping: waiting for an event – suspended: (e. g. , as a result of ctrl Z) – idle: being created by fork( ), not yet runnable – zombie: terminated but parent has not accept its 15 return value

Example of process state • For example, when process issues an I/O command, it

Example of process state • For example, when process issues an I/O command, it becomes suspended, then becomes runnable again when I/O completes and will run depending on priority. 16

PROCESS COMPOSITION • code area: executable (text) portion of the process • data area:

PROCESS COMPOSITION • code area: executable (text) portion of the process • data area: used by the process to contain static data • stack area: used by the process to store temporary data • user area: holds housekeeping info • page tables: used for memory management 17

PROCESS TABLE • The process table is a kernel data structure that contains one

PROCESS TABLE • The process table is a kernel data structure that contains one entry for every process in the system. • The process table contains fields that must always be accessible to the kernel. 18

Process entry info – state: (running, runnable, sleeping, suspended, idle or zombified) – process

Process entry info – state: (running, runnable, sleeping, suspended, idle or zombified) – process ID and Parent PID – its real and effective user ID and group ID (GID) – location of its code, data, stack and user areas – a list of all pending signals – various timers give process execution time and kernel resource utilization 19

THE SCHEDULER • The scheduler is responsible for sharing CPU time between competing processes.

THE SCHEDULER • The scheduler is responsible for sharing CPU time between competing processes. • The scheduler maintains a multilevel priority queue that allows it to schedule processes efficiently and follows a specific algorithm for selecting which process should be running. 20

Scheduling Rules • The kernel allocates the CPU to a process for a time

Scheduling Rules • The kernel allocates the CPU to a process for a time quantum, preempts a process that exceeds its time quantum and feeds it back into one of the several priority queues. • During every second, processes in the nonempty queue of the highest priority queue are allocated the CPU is a round-robin fashion. 21

Scheduler (cont’) • To support real-time processes, scheduler needs to be changed so •

Scheduler (cont’) • To support real-time processes, scheduler needs to be changed so • that scheduling is based on priority inheritance rather than time quanta. Also, more preemption points in the kernel are needed. 22

Context Switch • To switch from one process to another, the kernel saves the

Context Switch • To switch from one process to another, the kernel saves the process's program counter, stack pointer and other important info in the process's user area. • When the process is ready to run, the kernel will get this info from the process's user area. 23