OS Chapter - 2
OS Chapter - 2
Operating-System Services
List and explain the services provided by OS for the user and efficientoperation of system.
An operating system provides an environment for the execution of programs. It provides certain
services to programs and to the users of those programs.
User Interfaces - Means by which users can issue commands to the system. Depending on the
operating system these may be a command-line interface ( e.g. sh, csh, ksh, tcsh, etc.), a
Graphical User Interface (e.g. Windows, X-Windows, KDE, Gnome, etc.), or a batch
command systems.
In Command Line Interface (CLI)- commands are given to the system.
In Batch interface – commands and directives to control these commands are put in a file and
then the file is executed.
In GUI systems- windows with pointing device to get inputs and keyboard to enter the text.
Program Execution - The OS must be able to load a program into RAM, run the program, and
terminate the program, either normally or abnormally.
I/O Operations - The OS is responsible for transferring data to and from I/O devices,
including keyboards, terminals, printers, and files. For specific devices, special functions are
provided (device drivers) by OS.
File-System Manipulation – Programs need to read and write files or directories. The services
required to create or delete files, search for a file, list the contents of a file and change the file
permissions are provided by OS.
Communications - Inter-process communications, IPC, either between processes running on
the same processor, or between processes running on separate processors or separate machines.
May be implemented by using the service of OS- like shared memory or message passing.
Error Detection - Both hardware and software errors must be detected and handled
appropriately by the OS. Errors may occur in the CPU and memory hardware (such as power
failure and memory error), in I/O devices (such as a parity error on tape, a connection failure
on a network, or lack of paper in the printer), and in the user program (such as an arithmetic
overflow, an attempt to access an illegal memory location).
Resource Allocation – Resources like CPU cycles, main memory, storage space, and I/O
devices must be allocated to multiple users and multiple jobs at the same time.
Accounting – There are services in OS to keep track of system activity and resource usage,
either for billing purposes or for statistical record keeping that can be used to optimize future
performance.
Protection and Security – The owners of information (file) in multiuser or networked
computer system may want to control the use of that information. When several separate
processes execute concurrently, one process should not interfere with other or with OS.
Protection involves ensuring that all access to system resources is controlled. Security of the
system from outsiders must also be done, by means of a password.
Command Interpreter
Command Interpreters are used to give commands to the OS. There are multiple command
interpreters known as shells. In UNIX and Linux systems, there are several different shells, like
the Bourne shell, C shell, Bourne-Again shell, Korn shell, and others.
The main function of the command interpreter is to get and execute the user-specified
command. Many of the commands manipulate files: create, delete, list, print, copy, execute,
and so on.
The commands can be implemented in two general ways-
i) The command interpreter itself contains the code to execute the command. For example, a
command to delete a file may cause the command interpreter to jump to a particular section
of its code that sets up the parameters and makes the appropriate system call.
ii) The code to implement the command is in a function in a separate file. The interpreter
searches for the file and loads it into the memory and executes it by passing the parameter.
Thus by adding new functions new commands can be added easily to the interpreter
without disturbing it.
System Calls
System calls is a means to access the services of the operating system generally written in C or
C++, although some are written in assembly for optimal performance.
The below figure illustrates the sequence of system calls required to copy a file content from
one file (input file) to another file (output file).
An example to illustrate how system calls are used: writing a simple program to read data from one
file and copy them to another file
There are number of system calls used to finish this task. The first system call is to write a
message on the screen (monitor). Then to accept the input filename. Then another system
call to write message on the screen, then to accept the output filename.
When the program tries to open the input file, it may find that there is no file of that name
or that the file is protected against access. In these cases, the program should print a
message on the console (another system call) and then terminate abnormally (another
system call) and create a new one (another system call).
Now that both the files are opened, we enter a loop that reads from the input file (another
system call) and writes to output file (another system call).
Finally, after the entire file is copied, the program may close both files (another system
call), write a message to the console or window (system call), and finally terminate
normally (final system call).
Most programmers do not use the low-level system calls directly, but instead use an
"Application Programming Interface", API.
Instead of direct system calls provides for greater program portability between different
systems. The API then makes the appropriate system calls through the system call interface,
using a system call table to access specific numbered system calls.
Each system call has a specific numbered system call. The system call table (consisting of
system call number and address of the particular service) invokes a particular service
routine for a specific system call.
The caller need know nothing about how the system call is implemented or what it does
during execution.
Figure: The handling of a user application invoking the open() system call.
1. Process Control
2. File management
3. Device management
4. Information management
5. Communications
6. Protection
Process control system calls include end, abort, load, execute, create process, terminate
process, get/set process attributes, wait for time or event, signal event, and allocate and free
memory.
Processes must be created, launched, monitored, paused, resumed, and eventually stopped.
When one process pauses or stops, then another must be launched or resumed
Process attributes like process priority, max. allowable execution time etc. are set and
retrieved by OS.
After creating the new process, the parent process may have to wait (wait time), or wait for
an event to occur(wait event). The process sends back a signal when the event has occurred
(signal event)
In DOS, the command interpreter loaded first. Then loads the process and transfers control to it.
The interpreter does not resume until the process has completed, as shown in Figure
Because UNIX is a multi-tasking system, the command interpreter remains completely resident
when executing a process, as shown in Figure below.
The user can switch back to the command interpreter at any time, and can place the running
process in the background even if it was not originally launched as a background process.
In order to do this, the command interpreter first executes a "fork" system call, which
creates a second process which is an exact duplicate (clone ) of the original command
interpreter. The original process is known as the parent, and the cloned process is known as
the child, with its own unique process ID and parent ID.
The child process then executes an "exec" system call, which replaces its code with that of
the desired process.
The parent (command interpreter) normally waits for the child to complete before issuing a
new command prompt, but in some cases it can also issue a new prompt right away, without
waiting for the child process to complete. (The child is then said to be running "in the
background", or "as a background process". ).
2. File Management
The file management functions of OS are –
File management system calls include create file, delete file, open, close, read, write,
reposition, get file attributes, and set file attributes.
After creating a file, the file is opened. Data is read or written to a file.
The file pointer may need to be repositioned to a point.
The file attributes like filename, file type, permissions, etc. are set and retrieved using
system calls.
These operations may also be supported for directories as well as ordinary files.
3. Device Management
Device management system calls include request device, release device, read, write,
reposition, get/set device attributes, and logically attach or detach devices.
When a process needs a resource, a request for resource is done. Then the control is
granted to the process. If requested resource is already attached to some other process,
the requesting process has to wait.
In multiprogramming systems, after a process uses the device, it has to be returned to
OS, so that another process can use the device.
Devices may be physical ( e.g. disk drives ), or virtual / abstract ( e.g. files, partitions,
and RAM disks ).
4. Information Maintenance
Information maintenance system calls include calls to get/set the time, date, system data,
and process, file, or device attributes.
These system calls care used to transfer the information between user and the OS.
Information like current time & date, no. of current users, version no. of OS, amount of free
memory, disk space etc. are passed from OS to the user.
5. Communication
Communication system calls create/delete communication connection, send/receive
messages, transfer status information, and attach/detach remote devices.
The message passing model must support calls to:
o Identify a remote process and/or host with which to communicate.
o Establish a connection between the two processes.
o Open and close the connection as needed.
6. Protection
Protection provides mechanisms for controlling which users / processes have access to
which system resources.
System calls allow the access mechanisms to be adjusted as needed, and for non- privileged
users to be granted elevated access permissions under carefully controlled temporary
circumstances.
System Programs
A collection of programs that provide a convenient environment for program development and
execution (other than OS) are called system programs or system utilities.
1. File management - programs to create, delete, copy, rename, print, list, and generally
manipulate files and directories.
2. Status information - Utilities to check on the date, time, number of users, processes running,
data logging, etc. System registries are used to store and recall configuration information for
particular applications.
3. File modification - e.g. text editors and other tools which can change file contents.
4. Programming-language support - E.g. Compilers, linkers, debuggers, profilers, assemblers,
library archive management, interpreters for common languages, and support for make.
5. Program loading and execution - loaders, dynamic loaders, overlay loaders, etc., as well as
interactive debuggers.
6. Communications - Programs for providing connectivity between processes and users,
including mail, web browsers, remote logins, file transfers, and remote command execution.
Operating-System Design and Implementation
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: batch, time shared, single user, multiuser, distributed, real time, or general purpose.
Beyond this highest design level, the requirements may be much harder to specify. The
requirements can, however, be divided into two basic groups
1. User goals (User requirements)
2. System goals (system requirements)
User requirements are features that users care about and understand like system should be
convenient to use, easy to learn, reliable, safe and fast.
System requirements are written for the developers, ie. People who design the OS. Their
requirements are like easy to design, implement and maintain, flexible, reliable, error free and
efficient.
Implementation
Traditionally OS were written in assembly language.
In recent years, OS are written in C, or C++. Critical sections of code are still written in
assembly language.
The first OS that was not written in assembly language was the Master Control Program
(MCP).
The advantages of using a higher-level language for implementing operating systems are:
The code can be written faster, more compact, easy to port to other systems and is easier
to understand and debug.
The only disadvantages of implementing an operating system in a higher-level language
are reduced speed and increased storage requirements.
Operating-System Structure
Simple Structure
Many operating systems do not have well-defined structures. They started as small, simple, and
limited systems and then grew beyond their original scope. Eg: MS-DOS.
In MS-DOS, the interfaces and levels of functionality are not well separated. Application
programs can access basic I/O routines to write directly to the display and disk drives. Such
freedom leaves MS-DOS in bad state and the entire system can crash down when user
programs fail.
UNIX OS consists of two separable parts: the kernel and the system programs. The kernel is
further separated into a series of interfaces and device drivers. The kernel provides the file
system, CPU scheduling, memory management, and other operating-system functions through
system calls.
Layered Approach
The OS is broken into number of layers (levels). Each layer rests on the layer below it, and
relies on the services provided by the next lower layer.
Bottom layer (layer 0) is the hardware and the topmost layer is the user interface.
A typical layer, consists of data structure and routines that can be invoked by higher-level
layer.
Advantage of layered approach is simplicity of construction and debugging.
The layers are selected so that each uses functions and services of only lower-level layers. So
simplifies debugging and system verification. The layers are debugged one by one from the
lowest and if any layer doesn’t work, then error is due to that layer only, as the lower layers are
already debugged. Thus the design and implementation is simplified.
A layer need not know how its lower level layers are implemented. Thus hides the operations
from higher layers.
Figure: A layered Operating System
Disadvantages of layered approach:
The various layers must be appropriately defined, as a layer can use only lower level layers.
Less efficient than other types, because any interaction with layer 0 required from top layer.
The system call should pass through all the layers and finally to layer 0. This is an overhead.
Microkernels
This method structures the operating system by removing all nonessential components from the
kernel and implementing them as system and user-level programs thus making the kernel as
small and efficient as possible.
The removed services are implemented as system applications.
Most microkernels provide basic process and memory management, and message passing
between other services.
The main function of the microkernel is to provide a communication facility between the client
program and the various services that are also running in user space.
Benefit of microkernel –
System expansion can also be easier, because it only involves adding more system
applications, not rebuilding a new kernel.
Mach was the first and most widely known microkernel, and now forms a major component of
Mac OSX.
Disadvantage of Microkernel -
Performance overhead of user space to kernel space communication
Modules
Modern OS development is object-oriented, with a relatively small core kernel and a set of
modules which can be linked in dynamically.
Modules are similar to layers in that each subsystem has clearly defined tasks and interfaces,
but any module is free to contact any other module, eliminating the problems of going through
multiple intermediary layers.
The kernel is relatively small in this architecture, similar to microkernels, but the kernel does
not have to implement message passing since modules are free to contact each other directly.
Eg: Solaris, Linux and MacOSX.
The Max OSX architecture relies on the Mach microkernel for basic system management
services, and the BSD kernel for additional services. Application services and dynamically
loadable modules (kernel extensions ) provide the rest of the OS functionality.
Resembles layered system, but a module can call any other module.
Resembles microkernel, the primary module has only core functions and the knowledge of how
to load and communicate with other modules.
Virtual Machines
Implementation
The virtual-machine concept is useful, it is difficult to implement.
Work is required to provide an exact duplicate of the underlying machine. Remember that the
underlying machine has two modes: user mode and kernel mode.
The virtual-machine software can run in kernel mode, since it is the operating system. The
virtual machine itself can execute in only user mode.
Benefits
Able to share the same hardware and run several different execution environments(OS).
Host system is protected from the virtual machines and the virtual machines are protected from
one another. A virus in guest OS, will corrupt that OS but will not affect the other guest
systems and host systems.
Even though the virtual machines are separated from one another, software resources can be
shared among them. Two ways of sharing s/w resource for communication are:
o To share a file system volume (part of memory).
o To develop a virtual communication network to communicate between the virtual
machines.
The operating system runs on and controls the entire machine. Therefore, the current system
must be stopped and taken out of use while changes are made and tested. This period is
commonly called system development time. In virtual machines such problem is eliminated.
User programs are executed in one virtual machine and system development is done in another
environment.
Multiple OS can be running on the developer’s system concurrently. This helps in rapid
porting and testing of programmer’s code in different environments.
System consolidation – two or more systems are made to run in a single system.
Simulation –
Here the host system has one system architecture and the guest system is compiled in different
architecture. The compiled guest system programs can be run in an emulator that translates each
instructions of guest program into native instructions set of host system.
Para-Virtualization –
This presents the guest with a system that is similar but not identical to the guest’s preferred system.
The guest must be modified to run on the para-virtualized hardware.
Examples
VMware
VMware is a popular commercial application that abstracts Intel 80X86 hardware into isolated
virtual machines. The virtualization tool runs in the user-layer on top of the host OS. The
virtual machines running in this tool believe they are running on bare hardware, but the fact is
that it is running inside a user-level application.
VMware runs as an application on a host operating system such as Windows or Linux and
allows this host system to concurrently run several different guest operating systems as
independent virtual machines.
In below scenario, Linux is running as the host operating system; FreeBSD, Windows NT, and
Windows XP are running as guest operating systems. The virtualization layer is the heart of VMware,
as it abstracts the physical hardware into isolated virtual machines running as guest operating systems.
Each virtual machine has its own virtual CPU, memory, disk drives, network interfaces, and so forth.
System Boot
Operating system must be made available to hardware so hardware can start it.
Small piece of code – bootstrap loader, locates the kernel, loads it into memory, and starts it
Sometimes two-step process where boot block at fixed location loads bootstrap loader.
When power initialized on system, execution starts at a fixed memory location Firmware used
to hold initial boot code.