Download as pdf or txt
Download as pdf or txt
You are on page 1of 26

chapter 2

Notebook: Operating System


Created: 04-09-2020 20:05 Updated: 15-10-2020 22:23
Author: Abhishek Singh

We can view an operating system from several view points: 


Programmer's view focuses on the services that the system
provides. 
User's view focuses on the interface that it makes available to
users and programmers.
Operating System designer's view focuses on its components and
their interconnections.
Operating System Services
An operating system provides the environment for the execution of
programs.
It makes certain services available to the programs and to the
user of those programs.
These services make life of both programmer and user easier.
                                                

User Interface: Almost all operating systems provide user


1.
interface (UI).
These interfaces can take several forms:
Graphical User Interface (GUI): It consists of a window
system, with mouse as a pointing device and keyboard to
enter text.
Touch-screen Interface: Mobiles and Tablets use this
interface, enabling user to slide their finger across screen or
press buttons on screen.
Command-line Interface (CLI): It uses text commands and
methods for entering them.
systems can provide any one, two or all of these interfaces.
2. Program Execution: The system must be able to load a
program in to memory and to run that program. The program
must be able to end its execution, either normally or
abnormally (indicating error).
3. I/O Operations: A running program may require I/O, which
may involve a file or an I/O device. For efficiency and
protection, users usually cannot control I/O devices directly.
Therefore, the operating system must provide a means to do
I/O.
4. File-system manipulation: Programs need to read, write,
search and delete files and directories. Some operating
systems also include permission management to allow or
deny access to files or directories based on file ownership.
Many operating systems provide a variety of file systems, to
allow personal choice, specific features or performance
characteristics.
5. Communications: Exchange of information may occur
between processes that are executing on the same computer
or on different computer systems tied together by a network.
This communication may be implemented via
shared memory, in which two or more processes read and write
to a shared section of memory, or via 
message passing, in which packets of information in predefined
formats are moved between processes by the operating system.
6. Error detection: The operating system needs to be detecting
and correcting errors constantly.
Errors may occur in the:
CPU and memory hardware(such as a memory error or a
power failure)
I/O devices(such as a parity error on disk, a connection
failure on a network, or lack of paper in the printer)
User program (such as an arithmetic overflow or an attempt
to access an illegal memory location).
For each type of error, the operating system should take the
appropriate action to ensure correct and consistent computing. 
Some of these are: 
terminate an error-causing process
return an error code to a process for the process to detect
and possibly correct
When choice is left it may halt the system.

following services are not for helping user but rather for ensuring
efficient operation of system itself

7. Resource allocation: Operating system manages the


allocation of resources to multiple processes running at the
same time. Some (such as CPU cycles, main memory, and file
storage) may have special allocation code, whereas others
(such as I/O devices) may have much more general request
and release code.
8. Logging(Accounting): Operating Systems keeps track of
which programs use how much and what kinds of computer
resources. This record keeping may be used for accounting
(so that users can be billed) or simply for accumulating usage
statistics. Usage statistics may be a valuable tool for system
administrators who wish to reconfigure the system to
improve computing services.  
9. Protection and Security: Protection involves ensuring all the
access to system resources is controlled.
Security involves measures to protect the system form outsiders.
It involves authenticating users by means of password-id, 
defending external I/O devices from invalid access attempts
and recording all such connections for different break-ins.

User and Operating System Interface


There are several ways by which users can interact with operating
system.
Some of these are:
1. Command Interpreters: Command Interpreter (or Command-
Line interface) is a special program that allows users to
enter commands to be directly performed by operating
system. 
Some OS include interpreter in the kernel
while some others like windows,  UNIX, Linux, treat command
interpreter as a special program (that is running when a
process is initiated or when a user first logs in).

On systems with multiple command line interpreters, interpreters


are known as shells.
Eg. of shells on Unix/Linux: C shell, Bourne-Again Shell, Korn
Shell
Most of these commands are related to File manipulation
The commands we enter to be performed by operating system can
be implemented in two ways:
Command Interpreter may itself contain the code to execute
various commands. Size of command Interpreter is large.
New commands are difficult to add.
Command Interpreter may just call the system programs to
implement the code. Size is small. Used by Unix. New
commands are easier to add

2.Graphical User Interface: This a window-and-menu based


system where users use a pointing device ex. Mouse to
select, execute various files and programs.
The first GUI was developed by Xerox PARC for Xerox Alto
computer.
Many systems now include both CLI and GUI interfaces.
Microsoft Windows is GUI with CLI “command” shell
Apple Mac OS X is “Aqua” GUI interface with UNIX kernel
underneath and shells available
Unix and Linux have CLI with optional GUI interfaces (CDE,
KDE, GNOME)
3. Touch Screen Interface: Mobile systems, tablets and handheld
devices  use touch-screen interface. Here users interact with
OS by performing gestures on screen such as swiping
fingers, pressing, etc. Apple iPhone and iPad uses
springboard touchscreen interface.

System Calls
System calls provide an interface to the operating system
services.
These calls are generally available as functions written in C/C++.
Example of How system calls are used: 
We are writing a simple a program of transferring contents if one
file to other file.
                                                                        

Systems execute thousand of system calls per second.


Typically, application developers design programs according to an
application programming interface(API). The API specifies a set of
functions that are available to an application
programmer,including the parameters that are passed to each
function and the return values the programmer can expect. These
functions invoke the actual system calls on behalf of programmer.
Three of the most common APIs available to application
programmers are:
Windows API for Windows systems
POSIX API for POSIX-based systems
Java API for programs that run on the Java virtual machine.
A programmer can access an API via a library of code that is
provided by the operating system.
Why would an application programmer prefer programming
according to an API rather than invoking actual system calls?
Program portability: An application programmer designing an
app using an API can expect his program to run on any
system that supports the same API
Actual System calls are more difficult to use than APIs.

A Run-time Environment (RTE) is a set of softwares, compiler,


interpreter and libraries needed to execute application program.
RTE provides system-call interface that serves as the link to the
system calls made available by the operating system.  
The system-call interface intercepts function calls in the API and
invokes the necessary system calls within the operating system. 
Typically, a number is associated with each system call, and the
system-call interface maintains a table indexed according to these
numbers. 
The system-call interface then invokes the intended system call in
the operating-system kernel and returns the status of the system
call.
Three general methods are used to pass parameters to the
operating system(which may be needed during some function-
call):
Pass the parameters in registers
If number of parameters are large then they can be stored in
a block or table in memory and the address of the block is
passed as a  parameter in a register.
Parameters also can be placed, or pushed, onto a stack by
the program and popped off the stack by the operating
system.

Types of System calls


There are six categories of system calls:

1. Process Control
        system calls:
end(): running program halts normally
        abort(): running program halts ab-normally
If a system call is made to terminate the currently running
program abnormally, or if the program runs into a problem
and causes an error trap, a dump of memory is sometimes
taken and an error message is generated. The dump is
written to a special log file on disk and may be examined by a
debugger to detect bugs.
If the program discovers an error in its input and wants to
terminate abnormally, it may also want to define an error
level. More severe errors can be indicated by a higher-level
error parameter. It is then possible to combine normal and
abnormal termination by defining a normal termination as an
error at level 0.
load(): bringing a program in memory
        execute(): executing a loaded program
create_process(): To create a new process,
terminate_process(): to terminate a process
In order to control a new process we need to be able to
get_process_attributes() and set_process_attributes()
wait_time(): waiting for existing process call to finish
        wait_event(): waiting for a specific event to occur
        signal_event(): Signalling the occurence of an event
when two or more processes share data. To ensure the
integrity of the data being shared, operating systems often
provide system calls allowing a process to lock shared data.
Then, no other process can access the data until the lock is
released. such system calls include acquire_lock() and
release_lock()
2. File Management
create() and delete() for creating and deleting files/directories
open() and close() for opening and closing files/directories
read(), write() and reposition() for reading, writing and re-
positioning file.
get_file_attributes() and set_file_attributes() for getting and
setting various file attributes.
move() and copy()

3. Device Management
A process may require several resource during its execution.
If the resources are available they can be granted and control can
be returned to the user process.
Otherwise, process will have to wait until sufficient resources are
available.
The various resources controlled by operating system can be
thought of as devices.
users on a multi-user system first need to request() device, then
after they are finished with the device they need to release() it.
We can read(), write() and reposition() devices.

4.Information Maintenance
Many system calls simply exist for the purpose of transferring the
information b/w user program and operating system.
for ex- system calls for getting current date, time, version number,
free-space, etc.
System calls to dump() memory are also provided for debugging
Operating system maintains information about all its processes
using system calls.
System calls are also used to get and set process information.

5.Communication
Inter-process communication can be of two types:
Message-Passing Model: Communicating processes
exchange messages with one another to transfer information.
Before communication a connection must be established, and the
name of the other communicator must be known. Each computer
in a network has  a host name and each process has a process
name. The names can be translated into an identifier by
get_hostid() and get_processid() system calls.
open(), close(), open_connection() and close_connection() system
calls can then be provided.
accept_connection() for allowing communication to take place.
read_messages() and write_messages() system calls are used to
exchange messages b/w client and server
close_connection() call terminates the communication.

Shared-Memory Model: Processes use


shared_memory_create() and shared_memory_attach() calls
to create and gain access to regions of memory owned by
other processes.

Message passing is useful for exchanging smaller amounts of


data, because no conflicts need to be avoided. It is also easier to
implement than shared memory for inter-computer
communication. 
Shared memory allows maximum speed and convenience of
communication, since it can be done at memory transfer speeds
when it takes place within a computer. Problems exist, however, in
the areas of protection and synchronization between the
processes sharing memory.

6.Protection
Protection provides a mechanism for controlling access to the
resources provided by a computer system. 
set_permission() and get_permission(), manipulate the permission
settings of resources such as files and disks.
The allow_user() and deny_user() system calls specify whether
particular users can or cannot be allowed access to certain
resources.

System services
System services, also known as system utilities, provide a
convenient environment for program development and execution. 
System programs are just bundle of useful system calls.
They can be divided into these categories:

1. File Management: These programs create, delete, copy,


rename, print, list, and generally access and manipulate files
and directories.
2. Status information: These programs ask the system for the
date, time, amount of available memory or disk space,
number of users, or similar status information. Some
systems also support a registry, which is used to store and
retrieve configuration information.
3. File modification: Several text editors may be available to
create and modify the content of files stored on disk or other
storage devices. There may also be special commands to
search contents of files or perform transformations of the
text.                                                                             
4. Programming-Language support system programs:
Compilers, debuggers, interpreters are example of such
system programs.
5. Program loading and execution: several loaders are available
for loading compiled programs in memory for execution, ex.
relocatable loaders, linkage editors, overlay loaders, and
absolute loaders. debugging systems for high-level
languages or machine languages are needed as well.
6. Communications: These programs provide the mechanism for
creating virtual connections among processes, users, and
computer systems. They allow users to send messages to
one another’s screens, to browse web pages,to send e-mail
messages, to login remotely, or to transfer files from one
machine to another.
7. Background services: All general-purpose systems have
methods for launching certain system-program processes at
boot time. Some of these processes terminate after
completing their tasks, while others continue to  run until the
system is halted. Constantly running system-program
processes are known as services, subsystems, or daemons.
Application programs are user programs which are not
considered part of OS. They are used for solving common user
problems. application programs include web browsers, word
processors and text formatters, spreadsheets, database systems,
compilers, plotting and statistical-analysis packages, and games.
Operating System Design and Implementation
Here we are discussing the problems we face in designing and
implementing an operating system. There are no complete
solutions to such problems, but there are approaches that have
proved successful.
Design Goals
The first problem in designing a system is to define goals and
specifications. 
At the highest level, the design of the system will be affected by
the choice of hardware and the type of system: traditional
desktop/laptop, mobile, distributed, or real time. 
Beyond this highest design level, the requirements may be much
harder to specify. 
The requirements can, however, be divided into two basic groups: 
user goals: System should be easy to learn, convenient to use,
reliable, fast and secure.
System goals: System developers want system to be easy to
design, implement and maintain, bug free, flexible, reliable and
efficient.
In short, there is no unique solution to the problem of defining the
requirements of an operating system.
Mechanisms and Policies
Mechanisms determine how to do something
Policies determine what will be done
For example, the timer construct is a mechanism for ensuring
CPU protection, but deciding how long the timer is to be set for a
particular user is a policy decision.
Separation of policy and mechanism is important for flexibility of
OS
If policy and mechanism are not separate  then any change in
policies would require change in the underline mechanism.
A mechanism separate from policy can work across a range of
policies. Any change in the policy then just requires re-definition
of certain parameters.
Microsoft Windows has closely encoded both mechanism and
policy into the system to enforce a global look and feel across all
devices that run the Windows operating system. All applications
have similar interfaces, because the interface itself is built into the
kernel and system libraries.
The “standard” Linux kernel has a specific CPU scheduling
algorithm, which is a mechanism that supports a certain policy.
Anyone is free to modify or replace the scheduler to support a
different policy.
Policy decisions are important for all resource allocation.
Whenever it is necessary to decide whether or not to allocate a
resource, a policy decision must be made.  
Implementation 
Earlier OS were written in assembly languages, Now they are
mostly written in high-level languages such C/C++
Actually a mix of languages may be used:
Lowest level of kernel may be written in assembly language and C
Higher level routines may be written in C/C++
System programs may be written in C++ or Higher languages.
Advantage of using Higher Level language over Assembly
language:
1. Code is written faster, more compact, easier to understand
and debug
2. OS is easier to port to different hardware specifications
(whereas Assembly language codes can only run on the
processors for which it is written)

Disadvantages :
Operating systems written in higher level languages have reduced
speed and require more storage.
Eg.
MS-DOS was written in Intel 8088 assembly language. As a result,
it is available only on Intel family of CPUs
Linux was written in C and is available on a number of different
CPUs, such as INTEL 80X86, Motorola 680X0, SPARC, etc. 
Although operating systems are large, only a small amount of the
code is critical to high performance; 
the interrupt handlers, I/O manager, memory manager, and CPU
scheduler are probably the most critical routines.
Operating-System Structure
An operating system must be engineered carefully for it to
function properly and modify easily.
Here we are going to discuss how operating system components
are interconnected and melded into a kernel.

1. Simple Structure
                                                                                    

This was followed by MS-DOS


Here Interfaces and levels of functionality are not well separated.
Application programs, System programs can directly interact with
base hardware.
This structure has problem, since Application programs can
directly interact with hardware, entire system crashes when a
user program fails.
 
2. Monolithic Structure

In this structure all the functionality of Kernel is placed in a single


static binary file that runs in a single address space. That is why
this structure is called monolithic structure.
It is also called a tightly coupled system because changes in one
part of the system can have wide-range of effects on other parts.
example of such a structuring is the traditional Unix system, which
consists of two separable parts: the kernel and the system
programs. Kernel is further divided into a series of interface and
device drivers.
                                                        

Everything below the system-call interface and above the physical


hardware is the kernel. 
The kernel provides the file system, CPU scheduling, memory
management, and other OS functions through system calls.
Disadvantage: Since all the functionality is included in a single file,
it is difficult to maintain and extend.
Advantage: High speed and efficiency
Loosely Coupled systems are the ones where the system is
divided into separate smaller components that have specific and
limited functionality.
All these components together comprise the kernel. Advantage of
this system is that changes in one part of the system affect only
that part of the system.

3. Layered Approach
In this approach the operating system is divided into a number of
layers (levels). The bottom layer (layer 0) is the hardware layer
and the highest layer (layer N) is the user interface layer.
Each higher level layer can invoke the functionalities of lower level
layer.
Each lower layer hides all the implementation and data structures
form higher layers and only provides functionality.
                                                                                    

Advantages:
Simplicity of construction (each layer has limited and specific
functionality)
debugging - Layers are debugged from 0 to N, so when
debugging Mth layer and error occurs, then that error must
only be in Mth layer only, as all the lower layers have already
been debugged.

Disadvantages: 
Challenges in defining appropriate functionality for each layer
Overall performance is poor due to the overhead of requiring
a user program to traverse through multiple layers to obtain
an operating-system service.

example: Layered systems have been successfully used in


computer networks and web applications.

4. Microkernels
Micro-kernel approach structures the operating system by
removing all non-essential components from the kernel and
implementing them as user level programs that reside in separate
address spaces. The result is smaller kernel.
Mach OS was the first micro-kernel based OS.
The main function of the microkernel is to provide
communication between the client program and the various
services that are also running in user space. Communication
is provided through message passing.
                                                                

Advantages:
Extending the operating system is easier. All new services
are added to user space and consequently do not require
modification of the kernel.
OS is easier to port from one hardware design to another.
More security and reliability, since most services are running
as user—rather than kernel—processes. If a service fails, the
rest of the operating system remains untouched.

The best-known illustration of a microkernel operating system is


Darwin, the kernel component of the macOS and iOS operating
systems. Darwin, infact, consists of two kernels, one of which is
the Mach microkernel.
Disadvantage:
the performance of microkernels can suffer due to increased
system-function overhead. When two user-level services
communicate, messages have to be copied between the services,
which reside in separate address spaces. In addition, the
operating system may have to switch from one process to the next
to exchange the messages. The overhead involved in copying
messages and switching between processes affects the
performance.

5.Modules
The best methodology by far for the operating system design is
loadable-kernel modules (LKMs).
Here Kernel is a set of  core components and can link in additional
services via modules during run-time or at boot time.
This implementation is now used commonly in Linux, Unix, Solaris,
Windows, macOS
The idea of the design is for the kernel to provide core services,
while other services are implemented dynamically, as the kernel
is running.
                                    

This system resembles Layered system in the sense that each


kernel section has defined and protected interfaces. 
But is more flexible than layered system because any module can
call any other module.
This system resemble microkernel approach in the sense that it
also has a primary module implementing core functions and
knowledge of how to load and communicate with other modules.
But it is more efficient because modules don't need to invoke
message passing in order to communicate.
Linux uses loadable kernel modules, primarily for supporting
device drivers and file systems. LKMs can be “inserted” into the
kernel as the system is started (or booted) or during run time,
such as when a USB device is plugged into a running machine. If
the Linux kernel does not have the necessary driver, it can be
dynamically loaded. LKMs can be removed from the kernel during
runtime as well. For Linux, LKMs allow a dynamic and modular
kernel, while maintaining the performance benefits of a monolithic
system.

6.Hybrid Systems
Operating systems generally combine different structures
resulting in hybrid systems.
For example, Linux is monolithic, because having the operating
system in a single address space provides very efficient
performance. However, it also modular, so that new functionality
can be dynamically added to the kernel. 
Windows is largely monolithic as well, but it retains some behavior
typical of microkernel systems, including providing support for
separate subsystems (known as operating-system personalities)
that run as user-mode processes.
macOS and iOS
                                                    

applications can be designed to take advantage of user-


experience features or application framework or the core
framework. Additionally, an application can forego frameworks
entirely and communicate directly with the kernel environment. 
various layers :
• User experience layer: This layer defines the software interface
that allows users to interact with the computing devices. macOS
uses the Aqua user interface, whereas iOS uses the Springboard
user interface.
• Application frameworks layer: This layer includes the Cocoa and
Cocoa Touch frameworks, which provide an API for the Objective-
C and Swift programming languages. Cocoa is used for macOS and
Cocoa touch provides support for hardware features unique to
mobile devices.
• Core frameworks: This layer defines frameworks that support
graphics and media including, Quicktime and OpenGL.
• Kernel environment: This environment, also known as Darwin,
includes the Mach microkernel and the BSD UNIX kernel.
Differences b/w macOS and iOS:
macOS is intended for desktop and laptop computer systems,
it is compiled to run on Intel architectures. iOS is designed for
mobile devices and thus is compiled for ARM-based
architectures. 
        iOS kernel has been modified somewhat to address specific
features and needs of mobile systems, such as power
management and aggressive             memory management. 
iOS has more stringent security settings than macOS.  
iOS operating system is generally much more restricted to
developers than macOS. For example, iOS restricts access to
POSIX and BSD APIs on iOS, whereas they are openly
available to developers on macOS.

Darwin
Darwin uses a hybrid structure. 
                                                                                

Darwin is a layered system that consists primarily of the


Mach microkernel and the BSD UNIX kernel.
Whereas most operating systems provide a single system-
call interface to the kernel, Darwin provides two system-call
interfaces: Mach system calls (known as traps) and BSD
system calls (which provide POSIX functionality).
Beneath the system-call interface, Mach provides
fundamental operating system services, including memory
management, CPU scheduling, and interprocess
communication (IPC) facilities such as message passing and
remote procedure calls(RPCs). 
Much of the functionality provided by Mach is available
through kernel abstractions, which include tasks (a Mach
process), threads, memory objects, and ports (used for IPC).
In addition to Mach and BSD, the kernel environment provides
an I/O kit for development of device drivers and dynamically
loadable modules (which macOS refersto as kernel
extensions, or kexts).

we know microkernel systems have message passing overhead.


To address such performance problems, Darwin combines Mach,
BSD, the I/O kit, and any kernel extensions into a single address
space. Thus, Mach is not a pure microkernel in the sense that
various subsystems run in user space. Message passing within
Mach still does occur, but nocopying is necessary, as the services
have access to the same address space.
Android 
The Android operating system was designed by the Open Handset
Alliance and was developed for Android smartphones and tablet
computers. 
Whereas iOS is designed to run on Apple mobile devices and is
close-sourced, Android runs on a variety of mobile platforms and
is open sourced.
Android is similar to iOS in that it is a layered stack of software
that provides a rich set of frameworks supporting graphics, audio,
and hardware features.
                                                                                            
Google has designed a separate Android API for Java
Development.
Java applications are compiled into a form that can execute on the
Android RunTime ART, a virtual machine designed for Android and
optimized for mobile devices with limited memory and CPU
processing capabilities.
ART performs ahead-of-time (AOT) compilation. Here, .dex files
are compiled into native machine code when they are installed on
a device, from which they can execute on the ART. AOT compilation
allows more efficient application execution as well as reduced
power consumption, features that are crucial for mobile systems.
The set of native libraries available for Android applications
includes frameworks for developing web browsers(webkit),
database support(SQLite), and network support, such as secure
sockets (SSLs).
Google has chosen to abstract the physical hardware through the
hardware abstraction layer, or HAL. By abstracting all hardware,
such as the camera, GPS chip, and other sensors, the HAL
provides applications with a consistent view independent of
specific hardware.
Google uses Bionic C library for android.
At the bottom of Android’s software stack is the Linux kernel.
Google has modified the Linux kernel used in Android in a variety
of areas to support the special needs of mobile systems, such as
power management. It has also made changes in memory
management and allocation and has added a new form of IPC
known as Binder.
Building and Booting an Operating System
Operating-System Generation
Following steps have to be performed when generating operating
system from scratch.
1. Write the operating system source code (or obtain previously
written source code). 
2. Configure the operating system for the system on which it will
run. 
3. Compile the operating system. 
4. Install the operating system. 
5. Boot the computer and its new operating system.
Configuring the system involves specifying which features will be
included, and this varies by operating system.
SYSGEN program obtains information concerning the specific
configuration of the hardware system
Used to build system-specific compiled kernel or
system-tuned
Can generate more efficient code than one general
kernel

System Boot
The process of starting a computer by loading the kernel is known
as booting the system.
steps included in booting are as follows:
1. A small piece of code known as the bootstrap program or boot
loader locates the kernel. 
2. The kernel is loaded into memory and started. 
3. The kernel initializes hardware. 
4. The root file system is mounted.
Bootstrap programs also runs diagnostic to determine the state of
machine. —for example, inspecting memory and the CPU and
discovering devices.
If the diagnostics pass, the program can continue with the booting
steps. The bootstrap can also initialize all aspects of the system,
from CPU registers to device controllers and the contents of main
memory. Sooner or later, it starts the operating system and
mounts the root file system. It is only at this point is the system
said to be running.
Some computer systems use a multi-stage boot process: 
When the computer is first powered on, a small boot loader
located in non-volatile firmware known as BIOS is run. 
This initial boot loader usually does nothing more than load a
second boot loader, which is located at a fixed disk location called
the boot block. 
The program stored in the boot block may be sophisticated enough
to load the entire operating system into memory and begin its
execution.
many computers have replaced BIOS-based boot process with
UEFI(Unified Extensible Firmware Interface), 
because of the following advantages of UEFI:
1. It has better support for 64-bit systems and larger disks.
2. It is single, complete Boot manager and is then faster than
multistage BIOS boot process.

GRUB is an open-source bootstrap program for Linux and UNIX


systems. Boot parameters for the system are set in a GRUB
configuration file, which is loaded at startup. GRUB is flexible and
allows changes to be made at boot time, including modifying
kernel parameters and even selecting among different kernels
that can be booted.
The most common Android boot loader is LK (little kernel).
whereas Linux discards the initramfs once all necessary drivers
have been loaded, Android maintains initramfs as the root file
system for the device.
boot loaders for most operating systems—including Windows,
Linux, macOS, iOS, and Android—provide booting into recovery
mode or single-user mode for diagnosing hardware issues, fixing
corrupt file systems, and even re-installing the operating system.

Operating System Debugging


Debugging is the activity of finding and fixing errors in a system,
both in hardware and in software. Performance problems are
considered bugs, so debugging can also include performance
tuning, which seeks to improve performance by removing
processing bottlenecks
Failure Analysis
If a process fails, most operating systems write the error
information to a log file to alert system administrators or users
that the problem occurred. The operating system can also take a
core dump—a capture of the memory of the process— and store it
in a file for later analysis.
Debugging user-level process code is a challenge. 
Operating-system kernel debugging is even more complex
because of the size and complexity of the kernel, its control of the
hardware, and the lack of user-level debugging tools. 
A failure in the kernel is called a crash. When a crash occurs,
error information is saved to a log file, and the memory state is
saved to a crash dump.
Performance Monitoring and Tuning
Performance tuning seeks to improve performance by removing
processing bottlenecks. 
To identify bottlenecks, we must be able to monitor system
performance.
We have two approaches to measure system performance (they
provide per-process or system-wide observations):

Counters: Operating systems keep track of system activity


through a series of counters, such as the number of system
calls made or the number of operations performed to a
network device or disk.
Most of the counter-based tools on Linux systems read statistics
from the /proc file system. /proc is a “pseudo” file system that
exists only in kernel memory and is used primarily for querying
various per-process as well as kernel statistics.
Windows systems provide the Windows Task Manager, a tool that
includes information for current applications as well as
processes, CPU and memory usage, and networking statistics.

Tracing:  Tracing tools collect data for a specific event—such


as the steps involved in a system-call invocation.

DTrace:
DTrace tool in Solaris, FreeBSD, Mac
OS X allows live instrumentation on
production systems
Probes fire when code is executed
within a provider, capturing state
data and sending it to consumers
of those probes
Example of following XEventsQueued
system call move from libc library to
kernel and back

DTrace code to record amount of time


each process with UserID 101 is in
running mode (on CPU) in
nanoseconds

Profiling is periodic sampling of instruction pointer to look for


statistical trends
Kernighan’s Law
“Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are, by
definition, not smart enough to debug it.”

You might also like