Вы находитесь на странице: 1из 42

System Call vs Interrupt

A typical processor executes instructions one by one. But there


may be occasions when the processor has to stop temporarily
and hold the current instruction and execute some other
program or code segment (residing in some other place). After
doing this, the processor returns to normal execution and
continues from where it left off. A system call and an interrupt
are such occasions. A system call is a call to a subroutine built
in to the system. An interrupt is a program control interruption
caused by external hardware events.
What is a System Call?
System calls provide programs running on the computer an
interface to talk with the operating system. When a program
needs to ask for a service (for which it does not have
permission itself) from the kernel of the operating system it
uses a system call. User level processes do not have the same
permissions as the processes directly interacting with the
operating system. For example, to communicate with and
external I/O device or to interact with any other processes, a
program has to use system calls.
What is an Interrupt?
During normal execution of a computer program, there could be
events that can cause the CPU to temporarily halt. Events like
this are called interrupts. Interrupts can be caused by either
software or hardware faults. Hardware interrupts are called
(simply) Interrupts, while software interrupts are called
Exceptions or Traps. Once an interrupt (software or hardware) is
raised, the control is transferred to a special subroutine called
ISR (Interrupt Service Routine) that can handle the conditions
that are raised by the interrupt.
As mentioned above, the term Interrupt is usually reserved for
hardware interrupts. They are program control interruptions
caused by external hardware events. Here, external means
external to the CPU. Hardware interrupts usually come from
many different sources such as timer chip, peripheral devices
(keyboards, mouse, etc.), I/O ports (serial, parallel, etc.), disk
drives, CMOS clock, expansion cards (sound card, video card,
etc). That means hardware interrupts almost never occur due
to some event related to the executing program. For example,
an event like a key press on the keyboard by the user, or an
internal hardware timer timing out can raise this kind of
interrupt and can inform the CPU that a certain device needs

OSD Page 1
some attention. In a situation like that the CPU will stop
whatever it was doing (i.e. pauses the current program),
provides the service required by the device and will get back to
the normal program.
What is the difference between System Call and
Interrupt?
System call is a call to a subroutine built in to the system, while
Interrupt is an event, w hich causes the processor to
temporarily hold the current execution. However one major
difference is that system calls are synchronous, whereas
interrupts are not. That means system calls occur at a fixed
time (usually determined by the programmer), but interrupts
can occur at any time due an unexpected event such as a key
press on the keyboard by the user. Therefore, when ever a
system call occurs the processor only has to remember where
to return to, but in the event of an interrupt, the processor has
to remember both the place to return to and the state of the
system. Unlike a system call, an interrupt usually does not have
anything to do with the current program.

System Call vs Function Call


A typical processor executes instructions one by one. But there
may be occasions where the processor has to stop current
instruction and execute some other program or code segment
(residing in some other place). After doing this the processor
returns to normal execution and continues from where it left off.
A system call and a function call are such occasions. A system
call is a call to a subroutine built in to the system. A function
call is a call to a subroutine within the program itself.
What is a System Call?
System calls provide programs running on the computer an
interface to talk with the operating system. When a program
needs to ask for a service (for which it does not have
permission to do that by itself) from the kernel of the operating
system, it uses a system call. User level processes do not have
the same permissions as the processes directly interacting with
the operating system. For example, to communicate with and
external I/O device or to interact with any other processes, a
program uses system calls.
What is a Function Call?
A function call is also called a subroutine call. A subroutine (also
known as a procedure, function, method or routine) is part of a
OSD Page 2
larger program that is responsible for carrying out a specific
task. The larger program may execute a heavy workload, and
the subroutine may be performing just a simple task, which is
also independent of the remaining program coding. A function
is coded in such a way that it may be called multiple times and
from different places (even from within other functions). When
a function is called, the processor may go to where the code for
the function is residing and execute the instructions of the
function one by one. After completing the functions, the
processor will return to exactly where it left off and continue the
execution starting from the next instruction. Functions are a
great tool for code reuse. Many modern programming
languages support functions. A collection of functions is called
a library. Libraries are often used as means of sharing and
trading software. In some cases, the whole program could be a
sequence of subroutines (e.g. threaded code compilation).

What is the difference between System Call and


Function Call?
System call is a call to a subroutine built in to the system, while
a function call is a call to a subroutine within the program.
Unlike function calls, system calls are used when a program
needs to perform some task, which it does not have privilege
for. System calls are entry points in to the operating system
kernel and are not linked to the program (like function calls).
Unlike, system calls, function calls are portable. Time overhead
of a system call is more than the overhead for a function call
because a transition between the user mode and the kernel
mode must take place. System calls are executed in kernel
address space, while function calls are executed in user address
space.

Kernel vs Operating System


Operating system is the system software that manages the
computer. Its tasks include managing the computer resources
and accommodating their communication needs. The Kernel is
the main part of an operating system that mostly caries out
direct communication with the hardware resources. Without the
kernel, an operating system cannot operate. But because the
kernel of an operating system is buried with many other
components, most users are unaware of the existence of a
kernel.

OSD Page 3
What is Operating System?
An Operating system is software that manages a computer. It is
a collection of data and programs that manages the systems
(hardware) resources. Furthermore, it accommodates the
execution of application software (such as word processors etc.)
by acting as an interface layer between the hardware and the
applications (for functions such as input/output and memory
related operations). It is the main system software running on a
computer. Because users are unable to run any other system or
application software without a properly running operating
system, an operating system can be considered the most
important system software for a computer.
Operating systems are present in all types of machines (not just
computers) that have processors such as mobile phones,
console based gaming systems, super computers and servers.
Most popular operating systems are Microsoft Windows, Mac OS
X, UNIX, Linux and BSD. Microsoft operating systems are mostly
used within commercial enterprises, while UNIX based
operating systems are more popular with the academic
professionals, because they are free and open source (unlike
Windows, which is very costly).
What is Kernel?
Kernel is the main part of a computer Operating system. It is
the actual bridge between the hardware and the application
software. The kernel is usually responsible for the management
of system resources including the hardware and software
communication. It provides a very low level abstraction layer
between processors and input/output devices. Inter-process
communication and system calls are the main mechanisms in
which these low level facilities are offered to other applications
(by the kernel). Kernels are divided in to different types based
on the design/implementation and how each operating system
task is performed. All the system code is executed in the same
address space (for performance improvement reasons) by the
monolithic kernels. But, most services are run in the user space
by the microkernels (maintainability and modularity can be
increased with this approach). There are many other
approaches between these two extremes.
What is the difference between Kernel and Operating
System?
Kernel is the core (or the lowest level) of the operating system.
All other parts that make up the operating system (graphical

OSD Page 4
user interface, file management, shell, etc.) rely on the kernel.
Kernel is responsible for the communication with the hardware,
and it is actually the part of the operating system that talks
directly with the hardware. Numerous callable routines that can
be used for accessing files, displaying graphics, getting
keyboard/mouse inputs are provided by the kernel to be used
by other software.

Differences between Deadlock, NoLock, LiveLock & Blocking


Many a times this question is asked in different aspects, so I
compiled the following note and for sure these are not my
words but of the original authors (A big thanks to all of them
and I hope they don't mind. The purpose here is information
sharing only). I just spent a lil time to put it together.

What is a deadlock?
Deadlock is a situation when two processes, each having a lock
on one piece of data, attempt to acquire a lock on the
other's piece. Each process would wait indefinitely for the other
to release the lock, unless one of the user processes is
terminated. SQL Server detects deadlocks and terminates one
user's process.
A deadlock occurs when two or more tasks permanently block
each other by each task having a lock on a resource which the
other tasks are trying to lock. For example:
Transaction A acquires a share lock on row 1.
Transaction B acquires a share lock on row 2.
Transaction A now requests an exclusive lock on row 2, and is
blocked until transaction B finishes and releases the share lock
it has on row 2.
Transaction B now requests an exclusive lock on row 1, and is
blocked until transaction A finishes and releases the share lock
it has on row 1.
Transaction A cannot complete until transaction B completes,
but transaction B is blocked by transaction A. This condition is
also called a cyclic dependency: Transaction A has a
dependency on transaction B, and transaction B closes the
circle by having a dependency on transaction A.
Both transactions in a deadlock will wait forever unless the
deadlock is broken by an external process. The Microsoft SQL
Server Database Engine deadlock monitor periodically checks

OSD Page 5
for tasks that are in a deadlock. If the monitor detects a cyclic
dependency, it chooses one of the tasks as a victim and
terminates its transaction with an error. This allows the other
task to complete its transaction. The application with the
transaction that terminated with an error can retry the
transaction, which usually completes after the other
deadlocked transaction has finished.

What is a LiveLock?
A livelock is one, where a request for an exclusive lock is
repeatedly denied because a series of overlapping shared locks
keeps interfering. SQL Server detects the situation after four
denials and refuses further shared locks. A livelock also occurs
when read transactions monopolize a table or page, forcing a
write transaction to wait indefinitely.
A Live lock is one, where a request for exclusive lock is denied
continuously because a series of overlapping shared locks
keeps on interfering each other and to adapt from each other
they keep on changing the status which further prevents them
to complete the task. In SQL Server Live Lock occurs when read
transactions are applied on table which prevents write
transaction to wait indefinitely. This is different then deadlock
as in deadlock both the processes wait on each other.
A human example of live lock would be two people who meet
face-to-face in a corridor and each moves aside to let the other
pass, but they end up moving from side to side without making
any progress because they always move the same way at the
same time and never cross each other. This is good example of
live lock.
NO LOCK
If you add WITH (NOLOCK) after a table name in the FROM
clause, then no locks will be held on that table for the duration
of the query. However, you may also get dirty results, as
updates and deletes from other processes are being ignored.
Ex. SELECT COUNT(UserID)
FROM Users WITH (NOLOCK)
WHERE Username LIKE 'foobar'

livelock is similar to a deadlock, except that the states of the


processes involved in the livelock constantly change with

OSD Page 6
regard to one another, none progressing. Livelock is a special
case of resource starvation; the general definition only states
that a specific process is not progressing.

A real-world example of livelock occurs when two people meet


in a narrow corridor, and each tries to be polite by moving aside
to let the other pass, but they end up swaying from side to side
without making any progress because they both repeatedly
move the same way at the same time.

Livelock is a risk with some algorithms that detect and recover


from deadlock. If more than one process takes action, the
deadlock detection algorithm can be repeatedly triggered. This
can be avoided by ensuring that only one process (chosen
randomly or by priority) takes action.

DEADLOCK Deadlock is a condition in which a task waits


indefinitely for conditions that can never be satisfied - task
claims exclusive control over shared resources - task holds
resources while waiting for other resources to be released -
tasks cannot be forced to relinguish resources - a circular
waiting condition exists
LIVELOCK Livelock conditions can arise when two or more
tasks depend on and use the some resource causing a circular
dependency condition where those tasks continue running
forever, thus blocking all lower priority level tasks from running
(these lower priority tasks experience a condition called
starvation)

Binary semaphore is a semaphore with the integer value


ranges over 0 and 1 whereas genera l semaphore integer
value ranges from unrestricted domain

Binary semaphore are easier to implement as comparing


with general semaphore

Binary semaphore allows only one thread to access the


resource at a time but general semaphore allows N access
at a time

OSD Page 7
The difference is as follows:
Binary semaphores are binary, they can have two values
only; one to represent that a process/thread is in the
critical section(code that access the shared resource) and
others should wait, the other indicating the critical section
is free.
On the other hand, counting semaphores take more than
two values, they can have any value you want. The max
value X they take allows X process/threads to access the
shared resource simultaneously.

Context Switch

In computing, a context switch is the process of storing and


restoring the state (context) of a process so that execution can
be resumed from the same point at a later time. This enables
multiple processes to share a single CPU and is an essential
feature of a multitasking operating system. What constitutes
the context is determined by the processor and the operating
system.

Context switches are usually computationally intensive, and


much of the design of operating systems is to optimize the use
of context switches. Switching from one process to another
requires a certain amount of time for doing the administration -
saving and loading registers and memory maps, updating
various tables and lists etc.

A context switch can mean a register context switch,


a task context switch, a stack frame switch, a thread context
switch, or a process context switch.
OSD Page 8
Multitasking

Most commonly, within some scheduling scheme, one process


needs to be switched out of the CPU so another process can
run. This context switch can be triggered by the process
making itself unrunnable, such as by waiting for
an I/O or synchronization operation to complete. On a pre-
emptive multitasking system, the scheduler may also switch
out processes which are still runnable. To prevent other
processes from being starved of CPU time, preemptive
schedulers often configure a timer interrupt to fire when a
process exceeds its time slice. This interrupt ensures that the
scheduler will gain control to perform a context switch.

Interrupt handling[edit]

Modern architectures are interrupt driven. This means that if


the CPU requests data from a disk, for example, it does not
need to busy-wait until the read is over; it can issue the request
and continue with some other execution. When the read is over,
the CPU can be interrupted and presented with the read. For
interrupts, a program called an interrupt handler is installed,
and it is the interrupt handler that handles the interrupt from
the disk.

When an interrupt occurs, the hardware automatically switches


a part of the context (at least enough to allow the handler to
return to the interrupted code). The handler may save
additional context, depending on details of the particular
hardware and software designs. Often only a minimal part of
the context is changed in order to minimize the amount of time
spent handling the interrupt. The kernel does not spawn or
schedule a special process to handle interrupts, but instead the
handler executes in the (often partial) context established at
the beginning of interrupt handling. Once interrupt servicing is
complete, the context in effect before the interrupt occurred is
restored so that the interrupted process can resume execution
in its proper state.

OSD Page 9
User and kernel mode switching[edit]

When a transition between user mode and kernel mode is


required in an operating system, a context switch is not
necessary; a mode transition is not by itself a context switch.
However, depending on the operating system, a context switch
may also take place at this time.

Steps[edit]

In a switch, the state of the first process must be saved


somehow, so that, when the scheduler gets back to the
execution of the first process, it can restore this state and
continue.

The state of the process includes all the registers that the
process may be using, especially the program counter, plus any
other operating system specific data that may be necessary.
This data is usually stored in a data structure called a process
control block (PCB), or switchframe.

In order to switch processes, the PCB for the first process must
be created and saved. The PCBs are sometimes stored upon a
per-process stack in kernel memory (as opposed to the user-
mode call stack), or there may be some specific operating
system defined data structure for this information.

Since the operating system has effectively suspended the


execution of the first process, it can now load the PCB and
context of the second process. In doing so, the program counter
from the PCB is loaded, and thus execution can continue in the
new process. New processes are chosen from a queue or
queues. Process and thread priority can influence which process
continues execution, with processes of the highest priority
checked first for ready threads to execute.

OSD Page 10
Software vs hardware context switching[edit]

Context switching can be performed primarily by software or


hardware. Some processors, like the Intel 80386 and its
successors,[1] have hardware support for context switches, by
making use of a special data segment designated the task state
segment or TSS. A task switch can be explicitly triggered with a
CALL or JMP instruction targeted at a TSS descriptor in
the global descriptor table. It can occur implicitly when an
interrupt or exception is triggered if there's a task gate in
the interrupt descriptor table. When a task switch occurs
the CPU can automatically load the new state from the TSS.

As with other tasks performed in hardware, one would expect


this to be rather fast; however, mainstream operating systems,
including Windows and Linux,[2] do not use this feature. This is
mainly due to two reasons:

Hardware context switching does not save all the registers


(only general purpose registers, not floating point registers
although the TS bit is automatically turned on in the
CR0 control register, resulting in a fault when executing
floating point instructions and giving the OS the opportunity
to save and restore the floating point state as needed).

Associated performance issues, e.g., software context


switching can be selective and store only those registers that
need storing, whereas hardware context switching stores
nearly all registers whether they are required or not.

"Device controller provides interface between device (physical


device) and adress and data bus. Device cotroller provides
interface that is uniform to all device having command
registers, status registers and data reisters. Device controller

OSD Page 11
provides interface to be used by the high level machine
software."
A little later practically all that is said for device driveres:
" Device driveres implement a similar interface, so a higher
level machine need not to be espacially concerned with the
details of the particular device. The goal is to simplify software
interfaces to devices as much as possible..."
Is that means that only device drivers provides interface that
enables application programmer sees all devices in the same
way, and not device controller. I'm confused because on a lot of
places what is said for device controller later is said for device
drivers. Can I say:
Device drivers translates request from application to a device
controller depended instructiont which then translate them to
device instruction on physical level?

Difference between Task and thread ?

In computer science terms, a Task is a future or a promise.


(Some people use those two terms synomymously, some use
them differently, nobody can agree on a precise definition.)
Basically, a Task<T> "promises" to return you a T, but not right
now honey, I'm kinda busy, why don't you come back later?
A Thread is one of many ways to fulfil that promise. But not
every Task needs a Thread. If the value you are waiting for
comes from the filesystem or a database or the network, then
there is no need for a thread. The Task might just register a
callback to receive the value when the disk is done seeking.
In particular, the Task does not say why it is that it takes such a
long time to return the value. It mightbe that it takes a long
time to compute, or it might that it takes a long time to fetch.
Only in the former case would you use a Thread to run a Task.
(In .NET, threads are freaking expensive, so you generally want
to avoid them as much as possible and really only use them if
you want to run multiple heavy computations on multiple CPUs.
For example, in Windows, a thread weighs 12KiByte (I think), in

OSD Page 12
Linux, a thread weighs as little as 4KiByte, in Erlang/BEAM even
just 400Byte. In .NET, it's 1MiByte!)

I think that what you are talking about when you say Task is
a System.Threading.Task. If that's the case then you can think
about it this way:

A program can have many threads, but a processor core


can only run one Thread at a time.
Threads are very expensive, and switching between
the threads that are running is also very expensive.
So... Having thousands of threads doing stuff is
inefficient. Imagine if your teacher gave you10,000 tasks
to do. You'd spend so much time cycling between them
that you'd never get anything done. The same thing can
happen to the CPU if you start too many threads.

To get around this, the .NET framework allows you to create


Tasks. Tasks are a bit of work bundled up into an object, and
they allow you to do interesting things like capture the output
of that work and chain pieces of work together (first go to the
store, then buy a magazine).

Tasks are scheduled on a pool of threads. The specific number


of threads depends on the scheduler used, but the default
scheduler tries to pick a number of threads that is optimal for
the number of CPU cores that you have and how much time
your tasks are spending actually using CPU time. If you want to,
you can even write your own scheduler that does something
specific like making sure that all Tasks for that scheduler always
operate on a single thread.

So think of Tasks as items in your to-do list. You might be able


to do 5 things at once, but if your boss gives you 10000, they
will pile up in your inbox until the first 5 that you are doing get
done. The difference between Tasks and the ThreadPool is that
Tasks (as I mentioned earlier) give you better control over the
relationship between different items of work (imagine to-do
OSD Page 13
items with multiple instructions stapled together), whereas the
ThreadPool just allows you to queue up a bunch of individual,
single-stage items (Functions).

OSD Page 14
What is the difference between multiprocessing,
multiprogramming,multitasking and multithreading?
Multitasking:
The ability to execute more than one task at the same time
Tasks sharing a common resource (like 1 CPU)
More than one task/program/job/process can reside into the
same CPU at one point of time.
This ability of the OS is called multitasking.
Multiprogramming:
The allocation of a computer system and its resources to more
than one concurrent application, job or user
A computer running more than one program at a time (like
running Excel and Firefox simultaneously)
More than one task/program/job/process can reside into the
main memory at one point of time.
This ability of the OS is called multiprogramming.

Multithreading:
Executing more than one thread parallely using a single
processor

Multiprocessing:
Simultaneous execution of instructions by multiple processors
within a single computer
A computer using more than one CPU at a time

For all the terminologies Multiprogramming is the core.


Multiprogramming is the process of loading more than one
program into the memory so that processor can be kept busy
by switching between any of the loaded programs. Multitasking
is derivation of multiprogramming where the operating system
treats the programs loaded in the memory as task. But
multiprocessing mainly deals with the hardware. Here more
than one processing unit (CPU) comes into the picture.
Multiprocessing leads to parallel computing.

OSD Page 15
Multiprogramming is a rudimentary form of parallel processing
in which several programs are run at the same time on a
uniprocessor. Since there is only one processor , there can be
no true simultaneous execution of different programs. Instead,
the operating system executes part of one program, then part
of another, and so on. To the user it appears that all programs
are executing at the same time.

If the machine has the capability of causing an interrupt after a


specified time interval, then the operating system will execute
each program for a given length of time, regain control, and
then execute another program for a given length of time, and
so on. In the absence of this mechanism, the operating system
has no choice but to begin to execute a program with the
expectation, but not the certainty, that the program will
eventually return control to the operating system.

Multiprocessing is the coordinated processing of programs by


more than one computer processor. Multiprocessing is a general
term that can mean the dynamic assignment of a program to
one of two or more computers working in tandem or can
involve multiple computers working on the same program at
the same time (in parallel).

With the advent of parallel processing, multiprocessing is


divided into symmetric multiprocessing (SMP) and massively
parallel processing (MPP). Multiprocessing is the coordinated
processing of programs by more than one computer processor.
Multiprocessing is a general term that can mean the dynamic
assignment of a program to one of two or more computers
working in tandem or can involve multiple computers working
on the same program at the same time (in parallel).

Multiprocessing should not be confused with

OSD Page 16
multiprogramming, or the interleaved execution of two or more
programs by a processor. Today, the term is rarely used since
all but the most specialized computer operating systems
support multiprogramming. Multiprocessing can also be
confused with multitasking, the management of programs and
the system services they request as tasks that can be
interleaved, and with multithreading, the management of
multiple execution paths through the computer or of multiple
users sharing the same copy of a program.

You have a lot to learn about the difference between these


three. I'll give the basic differences. In all these three there are
more than one program executing at a time. But...

Multiprograming: In multiprogramming systems, the running


task keeps running until it performs an operation that requires
waiting for an external event (e.g. reading from a tape) or until
the computer's scheduler forcibly swaps the running task out of
the CPU. Multiprogramming systems are designed to maximize
CPU usage.

Multitasking: In computing, multitasking is a method by which


multiple tasks, also known as processes, share common
processing resources such as a CPU. In the case of a computer
with a single CPU, only one task is said to be running at any
point in time, meaning that the CPU is actively executing
instructions for that task. Multitasking solves the problem by
scheduling which task may be the one running at any given
time, and when another waiting task gets a turn. The act of
reassigning a CPU from one task to another one is called a
context switch.

Multiprocessing: Multiprocessing is a generic term for the use of


two or more central processing units (CPUs) within a single
computer system. There are many variations on this basic

OSD Page 17
theme, and the definition of multiprocessing can vary with
context, mostly as a function of how CPUs are defined (multiple
cores on one die, multiple chips in one package, multiple
packages in one system unit, etc.).

Multiprocessing sometimes refers to the execution of multiple


concurrent software processes in a system as opposed to a
single process at any one instant.

A context switch is the computing process of storing and


restoring state (context) of a CPU so that execution can be
resumed from the same point at a later time. This enables
multiple processes to share a single CPU. The context switch is
an essential feature of a multitasking operating system.
Context switches are usually computationally intensive and
much of the design of operating systems is to optimize the use
of context switches. A context switch can mean
a register context switch, a task context switch,
a thread context switch, or a process context switch. What
constitutes the context is determined by the processor and the
operating system. Switching from one process to another
requires a certain amount of time for doing the administration -
saving and loading registers and memory maps, updating
various tables and list etc.

Contents

1 When to switch?

o 1.1 Multitasking

o 1.2 Interrupt handling

OSD Page 18
o 1.3 User and kernel mode switching

2 Context switch: steps

3 Software vs hardware context switching

4 References

5 External links

When to switch?

There are three situations where a context switch needs to


occur. They are:

Multitasking

Most commonly, within some scheduling scheme, one process


needs to be switched out of the CPU so another process can
run. Within a preemptive multitaskingoperating system, the
scheduler allows every task to run for some certain amount of
time, called its time slice.

If a process does not voluntarily yield the CPU (for example, by


performing an I/Ooperation), a timer interrupt fires, and the
operating system schedules another process for execution
instead. This ensures that the CPU cannot be monopolized by
any one processor-intensive application.

Interrupt handling

Modern architectures are interrupt driven. This means that if


the CPU requests data from a disk, for example, it does not
need to busy-wait until the read is over, it can issue the request
and continue with some other execution; when the read is over,
the CPU can be interrupted and presented with the read. For
interrupts, a program called an interrupt handler is installed,
and it is the interrupt handler that handles the interrupt from
the disk.

OSD Page 19
The kernel services the interrupts in the context of the
interrupted process even though it may not have caused the
interrupt. The interrupted process may have been executing in
user mode or in kernel mode. The kernel saves enough
information so that it can later resume execution of the
interrupted process and services the interrupt in kernel mode.
The kernel does not spawn or schedule a special process to
handle interrupts.

Portability of Linux:

Linux takes the middle road toward portability. As much as


practical, interfaces and core code are architecture-
independent C code. Where performance is critical, however,
kernel features are tuned for each architecture. For example,
much fast-path and low-level code is architecture dependent
and often written in assembly. This approach enables Linux to
remain portable without forgoing optimizations. Where
portability would hinder performance, performance generally
wins. Otherwise, code is kept portable.

Generally, exported kernel interfaces are architecture


independent. If any parts of the function need to be unique for
each supported architecture (either for performance reasons or
as a necessity), that code is implemented in separate functions
and called as needed. Each supported architecture then
implements its architecture-specific functions and links them
into the kernel image.

A good example is the scheduler. The large majority of the


scheduler is written in architecture-independent C and lives
in kernel/sched.c. A few jobs of the scheduler, such as switching
processor state or switching the address space, are very
architecture dependent. Consequently, the C
method context_switch(), which switches from one process to
another, calls the methodsswitch_to() and switch_mm(), to
switch processor state and switch address space, respectively.

OSD Page 20
The code for switch_to() and switch_mm() is uniquely
implemented by each architecture that Linux supports. When
Linux is ported to a new architecture, the new architecture
must provide an implementation for these functions.

Features of linux operating system :

An operating system is the software on a computer that


manages the way different programs use its hardware, and
regulates the ways that a user controls the computer. It is
considered the backbone of a computer, managing both
software and hardware resources. Operating systems are
responsible for everything from the control and allocation of
memory to recognizing input from external devices and
transmitting output to computer displays. They also manage
files on computer hard drives and control peripherals, like
printers and scanners. Some popular modern operating
systems for personal computers include Microsoft
Windows, Mac OS X, and Linux.

Key features of Linux Operating System:

Following are the key features of the Linux operating system:

Multitasking: several programs running at the same


time.
Multiuser: several users on the same machine at the
same time (and no two-user licenses!).
Multiplatform: runs on many different CPUs, not just
Intel.
Multiprocessor/multithreading: it has native kernel
support for multiple independent threads of control within a
single process memory space.
It has memory protection between processes, so that one
program can't bring the whole system down.
Demand loads executables: Linux only reads from disk
those parts of a program that are actually used.

OSD Page 21
Shared copy-on-write pages among executables. This
means that multiple process can use the same memory to
run in. When one tries to write to that memory, that page
(4KB piece of memory) is copied somewhere else. Copy-on-
write has two benefits: increasing speed and decreasing
memory use.
Virtual memory using paging (not swapping whole
processes) to disk: to a separate partition or a file in the file
system, or both, with the possibility of adding more
swapping areas during runtime (yes, they're still called
swapping areas). A total of 16 of these 128 MB (2GB in
recent kernels) swapping areas can be used at the same
time, for a theoretical total of 2 GB of useable swap space.
It is simple to increase this if necessary, by changing a few
lines of source code.
A unified memory pool for user programs and disk cache,
so that all free memory can be used for caching, and the
cache can be reduced when running large programs.
All source code is available, including the whole kernel and
all drivers, the development tools and all user programs;
also, all of it is freely distributable. Plenty of commercial
programs are being provided for Linux without source, but
everything that has been free, including the entire base
operating system, is still free.
Multiple virtual consoles: several independent login
sessions through the console, you switch by pressing a hot-
key combination (not dependent on video hardware). These
are dynamically allocated; you can use up to 64.
Supports several common file systems, including minix,
Xenix, and all the common system V file systems, and has
an advanced file system of its own, which offers file
systems of up to 4 TB, and names up to 255 characters
long.
Many networking protocols: the base protocols available in
the latest development kernels include TCP, IPv4, IPv6,
AX.25, X.25, IPX, DDP (AppleTalk), Netrom, and others.
Stable network protocols included in the stable kernels
currently include TCP, IPv4, IPX, DDP, and AX.25.
1. Free - All code can be seen, and edited, and recomplied,
which allows the community at large to help the main
developers fix bugs etc. You can also download 99% of

OSD Page 22
linux distros completely free of charge.

2. Completely customisable. Linux can be modified to your


hearts content, there are numerous window managers and
desktop environments for it, and each one of them is
infinitely themeable. You can change *everything* about
the way the OS interacts with you.

3. Little or No Viruses. Yes, there are viruses for Linux, but


only 2 ever got out in to the wild, and they were years
ago, and exploited very old holes in very old kernels. The
Other viruses were lab experiments. Also, Linux is
completely immune to all known spy-ware and malware.

4. Totally Secure. Linuxes base model invokes a true unix-


like multi-user environment. You are always signed in as a
user, unless you choose to sign in as root, and a user
cannot install software without first going through the
process of giving themselves access to the root account,
which will typically involve knowing the root password.
Anything that installs in the user account cannot affect the
wider system, and can be eliminated by simply erasing the
users home directory. This is why viruses are, at best,
ineffective

Advantages of linux :

Cost The most obvious advantage of using Linux is the


fact that it is free to obtain, whileMicrosoft products are
available for a hefty and sometimes recurring fee. Microsoft
licenses typically are only allowed to be installed on a single
computer, whereas a Linux distribution can be installed on
any number of computers, without paying a single dime.
Security In line with the costs, the security aspect
of Linux is much stronger than that of Windows. Why should
you have to spend extra money for virus protection
software? The Linuxoperating system has been around since
the early nineties and has managed to stay secure in the
realm of widespread viruses, spyware and adware for all

OSD Page 23
these years. Sure, the argument of the Linux desktop not
being as widely used is a factor as to why there are
no viruses. My rebuttle is that the Linux operating system is
open source and if there were a
widespread Linux virusreleased today, there would be
hundreds of patches released tomorrow, either by ordinary
people that use the operating system or by the distribution
maintainers. We wouldnt need to wait for a patch from a
single company like we do with Windows.
Choice (Freedom) The power of choice is a
great Linux advantage. With Linux, you have the power to
control just about every aspect of the operating system. Two
major features you have control of are your desktops look
and feel by way of numerous Window Managers, and
the kernel. In Windows, your either stuck using the boring
default desktop theme, or risking corruption or failure by
installing a third-party shell.
Software - There are so many software choices when it
comes to doing any specific task. You could search for a text
editor on Freshmeat and yield hundreds, if not thousands of
results. My article on 5 Linux text editors you should know
about explains how there are so many options just for editing
text on the command-line due to the open source nature
of Linux. Regular users and programmers contribute
applications all the time. Sometimes its a simple modification
or feature enhancement of a already existing piece of
software, sometimes its a brand new application. In addition,
software on Linux tends to be packed with more features and
greater usability than software on Windows. Best of all, the
vast majority of Linux software is free and open source. Not
only are you getting the software for no charge, but you have
the option to modify the source codeand add more features if
you understand the programming language. What more
could you ask for?
Hardware - Linux is perfect for those old computers with
barely any processing power or memory you have sitting in
your garage or basement collecting dust. Install Linux and
use it as a firewall, a file server, or a backup server. There
are endless possibilities. Old 386 or 486 computers with
barely any RAM run Linux without any issue. Good luck
running Windows on these machines and actually finding a
use for them.

OSD Page 24
Disadvantages of Linux:

Understanding Becoming familiar with


the Linux operating system requires patience as well as a
strong learning curve. You must have the desire to read and
figure things out on your own, rather than having everything
done for you. Check out the 20 must read howtos and
guides for Linux.
Compatibility Because of its free nature, Linux is
sometimes behind the curve when it comes to brand new
hardware compatibility. Though the kernel contributors and
maintainers work hard at keeping the kernel up to
date, Linux does not have as much of a corporate backing as
alternative operating systems. Sometimes you can find third
party applications, sometimes you cant.
Alternative Programs Though Linux developers have
done a great job at creating alternatives to popular Windows
applications, there are still some applications that exist on
Windows that have no equivalent Linux application
Disadvantages of Linux:


Many Windows programs will not run in
Linux. iTunes, Microsoft Office, Internet Explorer and
many other Windows programs will not run natively in
Linux. The good news is that there are decent ways
around most of these problems. For example, music
libraries can be managed with an iPod using programs
such as Amarok,Banshee, or Rhythmbox in Linux. Mozilla
Firefox and Google Chrome are outstanding Internet
browsers which can be used in the place of Internet
Explorer. It is also possible to run iTunes in Linux
using Wine, VirtualBox, or Parallels, though it is difficult to
have good results. LibreOffice and OpenOffice are
excellent office suites which can be used in the place of
Microsoft Office, but while overall compatibility in both
suites is good with Microsoft Office formats, it is not
perfect. Click here to learn more about Linux equivalents
to software commonly used in Windows [14].

There is a smaller selection of peripheral hardware


drivers for Linux. There is a smaller selection of

OSD Page 25
peripheral hardware drivers (for printers, scanners, and
other devices) in Linux as compared to Windows, though
many new Linux hardware drivers are constantly being
added. Closely related to this issue is the fact that not all
Linux distros work with all sets of computer hardware, so a
person may need to try more than one distro to find one
which works well with his/her computer. When it comes to
printers, some manufacturers offer better Linux support
than others; for example, HP offers excellent printer
support for Linux. Click here to learn more about Linux
hardware compatibility [15].

There is a learning curve for people who are new to
Linux. Despite this, most Linux distros, especially the
major ones, are very intuitive and user-friendly. Also, the
desktop environments in Linux are in many ways similar to
Windows in their appearance. One thing which should be
emphasized is that there is also a learning curve for
Windows XP users who switch to Windows 7 or Windows
8. Click here to learn more about the major Linux desktop
environments and to see pictures of them [16].
Advantages of Linux:


Freedom! Most Linux distros are free..... users do not
need to pay for a copy, but this is only one aspect of
freedom enjoyed by Linux users! In addition, Linux distros
can be freely downloaded and legally installed on as many
computers as you want and freely (and legally) given to
other people. Because most distros are open source, you
have access to the source code and can customize Linux
to be whatever you want it to be; you can even create
your own distro if you like!

Linux is easy to install! In many instances, it is actually


easier to install Linux to your computer than
Windows. Click here to learn more about how to download
and install Linux to your computer [1].

Linux is very stable! Linux systems rarely crash, and


when they do, the whole system normally does not go
down. The "blue screen of death" familiar to Windows

OSD Page 26
users is not a worry for Linux users.

Linux is less vulnerable to computer


malware! Because most computer malware are designed
to attack Windows (often through Active X which is not
typically found in Linux) the odds are considerably less for
Linux to be infected with a virus than Windows [2]. The
same holds true with spyware, trojans, and worms. While
Linux malware does exist, they are relatively few in
number and none have become widespread so far [3].
While Linux is very secure by its nature, users should still
employ good sense while surfing the Internet; click here to
learn more [4]. As long as Linux users download and install
only from their distro's official software repository, then
security is greatly increased. One nice security feature In
Linux is that files must be made to be executable by
someone with administrator privileges, which requires a
password. So even if a Linux virus is loaded on a Linux
computer, it will not be able to run without the user who
has administrator privileges intentionally making it
executable. Another important aspect of Linux security is
the fact that it is open source. Because the programing
code is available for anyone to view, there are many eyes
constantly examining it, which makes it highly difficult for
malware to be hidden within the code [5]. Also, security
patches normally come much quicker to Linux than other
operating systems because so many people are
contributing to it. Here are some resources with more
information about Linux security and why Linux computers
are much less likely than Windows to ever become
infected with a virus:
Linux typically does not slow down over time! Unlike
Windows, Linux does not easily become bogged down with
spyware, viruses, trojans, etc., which can greatly reduce a
computer's performance. Also, because Linux does not have
a registry like Windows, it is not plagued with registry errors
which can slow down a computer over time. Finally, the hard
drives on Windows (especially Windows XP and older)
computers need to be defragmented on a regular basis in order
to maintain faster performance, due to being formatted

OSD Page 27
in NTFS. On the other hand, because Linux is normally
formatted in a different way using ext4 among others, there is
no need to defragment a Linux hard drive.

Linux can breathe new life into old computers! If you


have an older computer (especially Pentium III or later) laying
around, you can install Linux and in essence have a new
computer. In many cases Linux will run faster and you can do
all of the basics such as browse the Internet, email, play
games, and create and edit documents, spreadsheets, and
PowerPoint presentations. It should also be mentioned that
Linux runs great on newer computers as well.

With Linux, you have so many choices in a wide variety


of distros! Linux comes in all sizes and flavors, which offers a
wide variety from which to choose the distro which will best suit
your needs. Another advantage of this variety is the innovation
that is taking place in the Linux world because it is open
source. You can learn more about different Linux distros by
going to distrowatch.com.

With many Linux distros, you have access to free


software which numbers in the thousands! Popular
distros such as Ubuntu, PCLinuxOS, andOpenSUSE offer
excellent software repositories within their package
managers where virtually any type of software can be
downloaded and installed to your Linux system for free. This
includes just about anything you can imagine, such as games,
educational software, office suites, and much more! Some
smaller distros, such as Peppermint OS, Lubuntu, Bodhi Linux,
and Puppy Linux are based on Ubuntu and as a result have
access to Ubuntu's software repositories. One very nice aspect
of these repositories is that the software found in them has
already been tested for compatibility and safety. For example,
the thousands of free and open source software found in the
Ubuntu Software Center has been tested and examined by

OSD Page 28
Ubuntu, so a user can be confident that the software will be
compatible with Ubuntu and will not include malware.

Difference between buffer and cache :

Key difference: A cache transparently stores data so that future


requests for that data can be served faster. A buffer, on the
other hand, temporarily stores data while the data is the
process of moving from one place to another.

Both cache and buffer are types of temporary storage that are
utilized in computer science. However, they differ in the
methods and the capabilities in which they are used. A cache
transparently stores data so that future requests for that data
can be served faster. A buffer, on the other hand, temporarily
stores data while the data is the process of moving from one
place to another, i.e. the input device to the output device.

There are two main types of caches, memory caching and disk
caching. Memory caching is when the cache is part of the main
memory, whereas disk caching is when the cache is part of
some other separate storage area, such as a hard disk. Caching
is the process of storing data in a cache so that the data can be
accessed faster in the future. The data that is stored within a
cache might be values that have been computed earlier or
duplicates of original values that are stored elsewhere. When
some data is requested, the cache is first checked to see
whether it contains that data. The data can be retrieved more
quickly from the cache than from its source origin.

An easy example to understand caching is to look at web


caching. A web cache is a mechanism for the temporary
storage (caching) of web documents, such as HTML pages and

OSD Page 29
images. This is mainly done to reduce bandwidth usage, server
load, and perceived lag. When a web page is loaded, the data
on the pages is cached; hence the next time the page is loaded
it is quicker, as data is already present, and only the changes
made to the page need to be loaded, which are in turn cached
for next time. Google's cache link in its search results provides
a way of retrieving information from websites that have
recently gone down and a way of retrieving data more quickly
than by clicking the direct link.

The buffer, on the other hand, is found mainly in the RAM and
acts as an area where the CPU can store data temporarily. This
area is used mainly when the computer and the other devices
have different processing speeds. Typically, the data is stored in
a buffer as it is retrieved from an input device (such as a
mouse) or just before it is sent to an output device (such as
speakers). However, the buffer may also be used when moving
data between processes within a computer.

So, the computer writes the data up into a buffer, from where
the device can access the data, as its own speed. This allows
the computer to be able to focus on other matters after it
writes up the data in the buffer; as oppose to constantly focus
on the data, until the device is done.

Buffers can be implemented in a fixed memory location in


hardware or by using a virtual data buffer in software, which
points to a data buffer are stored on a physical storage
medium. Majority of the buffers are utilized in the software.
These buffers typically use the faster RAM to store temporary
data, as RAM has a much faster access time than hard disk
drives. A buffer often adjusts timing by implementing a queue

OSD Page 30
or FIFO algorithm in memory. Hence, it is often writing data into
the queue at one rate and reading it at another rate.

A common example of this is streaming videos online, such as


YouTube. While, watching a video on YouTube, one may notice
that a gray bar tends to load before the red bar of the video
stream can play. The gray bar is the buffer. It downloads the
data of the video and saves it so that the video may play at an
uninterrupted rate. As you might have noticed that when the
red bar catches up to the gray bar, the video stops, in order to
load the rest of the video.

Buffers are also often used with I/O to hardware, such as disk
drives, sending or receiving data to or from a network, or
playing sound on a speaker. Buffers are used for many
purposes, such as interconnecting two digital circuits operating
at different rates, holding data for use at a later time, allowing
timing corrections to be made on a data stream, collecting
binary data bits into groups that can then be operated on as a
unit, and delaying the transit time of a signal in order to allow
other operations to occur.

However, a buffer cannot be used to instantaneously move


your location in the data stream, unless the new part has
already been moved to the buffer. Similar to the YouTube video,
which cannot be forwarded to a part that is not covered by the
gray bar. If you do, the buffer will relocate and restart from the
new location.

Still, the functions of a cache and buffer are not mutually


exclusive and are often combined for an ideal performance.

OSD Page 31
OSD Page 32
Paging and Demand Paging:

In computer operating systems, paging is one of the memory-management schemes by which


a computer can store and retrieve data from secondary storage for use in main memory.[1] In
the paging memory-management scheme, the operating system retrieves data from secondary
storage in same-size blocks called pages. The main advantage of paging over memory
segmentation is that it allows the physical address space of a process to be noncontiguous.
Before paging came into use, systems had to fit whole programs into storage contiguously,
which caused various storage and fragmentation problems.[2]

Paging is an important part of virtual memory implementation in most contemporary general-


purpose operating systems, allowing them to use disk storage for data that does not fit into
physical random-access memory (RAM).

Demand paging

In computer operating systems, demand paging (as opposed


to anticipatory paging) is a method of virtual memory management. In a
system that uses demand paging, the operating system copies a
disk page into physical memory only if an attempt is made to access it
and that page is not already in memory (i.e., if a page fault occurs). It
follows that a process begins execution with none of its pages in physical
memory, and many page faults will occur until most of a process's working
set of pages is located in physical memory. This is an example of a lazy
loading technique.

Basic concept[edit]

Demand paging follows that pages should only be brought into memory if the executing
process demands them. This is often referred to as lazy evaluation as only those pages
demanded by the process are swapped from secondary storage to main memory. Contrast this
to pure swapping, where all memory for a process is swapped from secondary storage to main
memory during the process startup.

OSD Page 33
Commonly, to achieve this process a page table implementation is used. The page table
maps logical memory to physical memory. The page table uses a bitwise operator to mark if a
page is valid or invalid. A valid page is one that currently resides in main memory. An invalid
page is one that currently resides in secondary memory. When a process tries to access a
page, the following steps are generally followed:

Attempt to access page.


If page is valid (in memory) then continue processing instruction as normal.
If page is invalid then a page-fault trap occurs.
Check if the memory reference is a valid reference to a location on secondary
memory. If not, the process is terminated (illegal memory access). Otherwise, we have
topage in the required page.
Schedule disk operation to read the desired page into main memory.
Restart the instruction that was interrupted by the operating system trap.
Advantages

Demand paging, as opposed to loading all pages immediately:

Only loads pages that are demanded by the executing process.


As there is more space in main memory, more processes can be loaded
reducing context switching time which utilizes large amounts of resources.
Less loading latency occurs at program startup, as less information is accessed from
secondary storage and less information is brought into main memory.
As main memory is expensive compared to secondary memory, this technique helps
significantly reduce the bill of material (BOM) cost in smart phones for example.
Symbian OS had this feature.
Disadvantages
Individual programs face extra latency when they access a page for the first time.
Programs running on low-cost, low-power embedded systems may not have
a memory management unit that supports page replacement.
Memory management with page replacement algorithms becomes slightly more
complex.

OSD Page 34
Possible security risks, including vulnerability to timing attacks; see Percival
2005 Cache Missing for Fun and Profit (specifically the virtual memory attack in section
2).
Thrashing which may occur due to repeated page faults.

When pure demand paging is used, page loading only occurs at the time of the data request,
and not before. In particular, when demand paging is used, a program usually begins
execution with none of its pages pre-loaded in RAM. Pages are copied from the executable
file into RAM the first time the executing code references them, usually in response topage
faults. As a consequence, pages of the executable file containing code not executed during a
particular run will never be loaded into memory.

Page faults

The main functions of paging are performed when a program tries to access pages that are not
currently mapped to physical memory (RAM). This situation is known as a page fault. The
operating system must then take control and handle the page fault, in a manner invisible to
the program. Therefore, the operating system must:

1. Determine the location of the data in secondary storage.


2. Obtain an empty page frame in RAM to use as a container for the data.
3. Load the requested data into the available page frame.
4. Update the page table to refer to the new page frame.
5. Return control to the program, transparently retrying the instruction that caused the
page fault.

If there is not enough available RAM when obtaining an empty page frame, a page
replacement algorithm is used to choose an existing page frame for eviction. If the evicted
page frame has been dynamically allocated during execution of a program, or if it is part of a
program's data segment and has been modified since it was read into RAM (in other words, if
it has become "dirty"), it must be written out to a location in secondary storage before being
freed. Otherwise, the contents of the page's frame in RAM are the same as the contents of the

OSD Page 35
page in its secondary storage, so it does not need to be written out to secondary storage. If, at
a later stage, a reference is made to that memory page, another page fault will occur and
another empty page frame must be obtained so that the contents of the page in secondary
storage can be again read into RAM.

Efficient paging systems must determine the page frame to empty by choosing one that is
least likely to be needed within a short time. There are various page replacement
algorithms that try to do this. Most operating systems use some approximation of the least
recently used (LRU) page replacement algorithm (the LRU itself cannot be implemented on
the current hardware) or a working set-based algorithm.

To further increase responsiveness, paging systems may employ various strategies to predict
which pages will be needed soon. Such systems will attempt to load pages into main memory
preemptively, before a program references them.

Page Faults :

A page fault (sometimes #pf or pf) is a trap to the software raised by the hardware when a
program accesses a page that is mapped in the virtual address space, but not loaded in
physical memory. In the typical case the operating system tries to handle the page fault by
making the required page accessible at a location in physical memory or terminates the
program in the case of an illegal access. The hardware that detects a page fault is the memory
management unit in a processor. The exception handling software that handles the page fault
is generally part of the operating system.

Contrary to what the name "page fault" might suggest, page faults are not always errors and
are common and necessary to increase the amount of memory available to programs in any
operating system that utilizes virtual memory, including OpenVMS, Microsoft
Windows, Unix-like systems (including Mac OS X, Linux, *BSD, Solaris, AIX, and HP-
UX), andz/OS. Microsoft uses the term hard fault in more recent versions of the Resource
Monitor (e.g., Windows Vista) to mean "page fault".[1]

Types[edit]

OSD Page 36
Minor[edit]

If the page is loaded in memory at the time the fault is generated, but is not marked in
the memory management unit as being loaded in memory, then it is called a minor or soft
page fault. The page fault handler in the operating system merely needs to make the entry for
that page in the memory management unit point to the page in memory and indicate that the
page is loaded in memory; it does not need to read the page into memory. This could happen
if the memory is shared by different programs and the page is already brought into memory
for other programs.

The page could also have been removed from a process's Working Set, but not yet written to
disk or erased, such as in operating systems that use Secondary Page Caching. For example,
HP OpenVMS may remove a page that does not need to be written to disk (if it has remained
unchanged since it was last read from disk, for example) and place it on a Free Page List if
the working set is deemed too large. However, the page contents are not overwritten until the
page is assigned elsewhere, meaning it is still available if it is referenced by the original
process before being allocated. Since these faults do not involve disk latency, they are faster
and less expensive than major page faults.

Major[edit]

This is the mechanism used by an operating system to increase the amount of program
memory available on demand. The operating system delays loading parts of the program from
disk until the program attempts to use it and the page fault is generated. If the page is not
loaded in memory at the time of the fault, then it is called a major or hard page fault. The
page fault handler in the OS needs to find a free location: either a page in memory, or another
non-free page in memory. This latter might be used by another process, in which case the OS
needs to write out the data in that page (if it has not been written out since it was last
modified) and mark that page as not being loaded in memory in its process page table. Once
the space has been made available, the OS can read the data for the new page into memory,
add an entry to its location in the memory management unit, and indicate that the page is
loaded. Thus major faults are more expensive than minor faults and add disk latency to the
interrupted program's execution.

Invalid[edit]

OSD Page 37
If a page fault occurs for a reference to an address that is not part of the virtual address space,
meaning there cannot be a page in memory corresponding to it, then it is called an invalid
page fault. The page fault handler in the operating system will then generally pass
a segmentation fault to the offending process, indicating that the access was invalid; this
usually results in abnormal termination of the code that made the invalid reference. A null
pointer is usually represented as a pointer to address 0 in the address space; many operating
systems set up the memory management unit to indicate that the page that contains that
address is not in memory, and do not include that page in the virtual address space, so that
attempts to read or write the memory referenced by a null pointer get an invalid page fault.

Handling illegal accesses and invalid page faults[edit]

Illegal accesses and invalid page faults can result in a segmentation fault or bus error,
resulting in programming termination (crash) or core dump, depending on the operating
system environment. Often these problems are caused by software bugs, but hardware
memory errors, such as those caused by overclocking, may corrupt pointers and make correct
software fail.

Operating systems such as Windows and UNIX (and other UNIX-like systems) provide
differing mechanisms for reporting errors caused by page faults. Windows uses structured
exception handling to report page fault-based invalid accesses as access violation exceptions,
and UNIX (and UNIX-like) systems typically use signals, such as SIGSEGV, to report these
error conditions to programs.

If the program receiving the error does not handle it, the operating system performs a default
action, typically involving the termination of the running process that caused the error
condition, and notifying the user that the program has malfunctioned. Recent versions of
Windows often report such problems by simply stating something like "this program must
close" (an experienced user or programmer with access to a debugger can still retrieve
detailed information). Recent Windows versions also write a minidump (similar in principle
to a core dump) describing the state of the crashed process. UNIX and UNIX-like operating
systems report these conditions to the user with error messages such as "segmentation
violation", or "bus error", and may also produce a core dump.

OSD Page 38
Page faults, by their very nature, degrade the performance of a program or operating system
and in the degenerate case can cause thrashing. Optimization of programs and the operating
system that reduce the number of page faults improve the performance of the program or
even the entire system. The two primary focuses of the optimization effort are reducing
overall memory usage and improving memory locality. To reduce the page faults in the
system, programmers must make use of an appropriate page replacement algorithm that suits
the current requirements and maximizes the page hits. Many have been proposed, such as
implementing heuristic algorithms to reduce the incidence of page faults. Generally, making
more physical memory available also reduces page faults.

Major page faults on conventional (hard disk) computers can have a significant impact on
performance. An average hard disk has an average rotational latency of 3ms, a seek-time of
5ms, and a transfer-time of 0.05 ms/page. So the total time for paging is near 8ms (8 000 s).
If the memory access time is 0.2 s, then the page fault would make the operation about
40,000 times slower.

Page Fragment :

In computer storage, fragmentation is a phenomenon in which storage space is used


inefficiently, reducing capacity or performance and often both. The exact consequences of
fragmentation depend on the specific system of storage allocation in use and the particular
form of fragmentation. In many cases, fragmentation leads to storage space being "wasted",
and in that case the term also refers to the wasted space itself. For other systems (e.g.
the FAT file system) the space used to store given data (e.g. files) is the same regardless of
the degree of fragmentation (from none to extreme).

There are three different but related forms of fragmentation: external fragmentation, internal
fragmentation, and data fragmentation, which can be present in isolation or conjunction.
Fragmentation is often accepted in return for improvements in speed or simplicity.

OSD Page 39
Basic principle[edit]

When a computer program requests blocks of memory from the computer system, the blocks
are allocated in chunks. When the computer program is finished with a chunk, it can free the
chunk back to the system, making it available to later be allocated again to another or the
same program. The size and the amount of time a chunk is held by a program varies. During
its lifespan, a computer program can request and free many chunks of memory.

When a program is started, the free memory areas are long and contiguous. Over time and
with use, the long contiguous regions become fragmented into smaller and smaller
contiguous areas. Eventually, it may become impossible for the program to obtain large
contiguous chunks of memory.

Types of fragmentation[edit]

Internal fragmentation[edit]

Due to the rules governing memory allocation, more computer memory is


sometimes allocated than is needed. For example, memory can only be provided to programs
in chunks divisible by 4, 8 or 16, and as a result if a program requests perhaps 23 bytes, it
will actually get a chunk of 32 bytes. When this happens, the excess memory goes to waste.
In this scenario, the unusable memory is contained within an allocated region. This
arrangement, termed fixed partitions, suffers from inefficient memory use - any process, no
matter how small, occupies an entire partition. This waste is called internal fragmentation.[1]
[2]

Unlike other types of fragmentation, internal fragmentation is difficult to reclaim; usually the
best way to remove it is with a design change. For example, in dynamic memory
allocation, memory pools drastically cut internal fragmentation by spreading the space
overhead over a larger number of objects.

External fragmentation[edit]

External fragmentation arises when free memory is separated into small blocks and is
interspersed by allocated memory. It is a weakness of certain storage allocation algorithms,
when they fail to order memory used by programs efficiently. The result is that, although free

OSD Page 40
storage is available, it is effectively unusable because it is divided into pieces that are too
small individually to satisfy the demands of the application. The term "external" refers to the
fact that the unusable storage is outside the allocated regions.

For example, consider a situation wherein a program allocates 3 continuous blocks of


memory and then frees the middle block. The memory allocator can use this free block of
memory for future allocations. However, it cannot use this block if the memory to be
allocated is larger in size than this free block.

External fragmentation also occurs in file systems as many files of different sizes are created,
change size, and are deleted. The effect is even worse if a file which is divided into many
small pieces is deleted, because this leaves similarly small regions of free spaces.

OSD Page 41
OSD Page 42

Вам также может понравиться