OS_Module_1
OS_Module_1
MODULE 1
Operating System can be viewed from two viewpoints– User views & System views
User Views: - The user’s view of the operating system depends on the type of user.
If the user is using standalone system, then OS is designed for ease of use and high
performances. Here resource utilization is not given importance.
If the users are at different terminals connected to a mainframe or minicomputers, by
sharing information and resources, then the OS is designed to maximize resource
utilization. OS is designed such that the CPU time, memory and I/O are used efficiently
and no single user takes more than the resource allotted to them.
If the users are in workstations, connected to networks and servers, then the user have a
system unit of their own and shares resources and files with other systems. Here the OS is
designed for both ease of use and resource availability (files).
Other systems like embedded systems used in home device (like washing m/c) &
automobiles do not have any user interaction. There are some LEDs to show the status of
its work.
Users of hand-held systems, expects the OS to be designed for ease of use and
performance per amount of battery life.
System Views:- Operating system can be viewed as a resource allocator and control
program.
Resource allocator – The OS acts as a manager of hardware and software resources.
CPU time, memory space, file-storage space, I/O devices, shared files etc. are the
different resources required during execution of a program. There can be conflicting
request for these resources by different programs running in same system. The OS
assigns the resources to the requesting program depending on the priority.
Control Program – The OS is a control program and manage the execution of user
program to prevent errors and improper use of the computer.
Single-Processor Systems –
All special-purpose processors run limited instructions and do not run user processes.
These are managed by the operating system; the operating system sends them
information about their next task and monitors their status.
For example, a disk-controller processor, implements its own disk queue and scheduling
algorithm, thus reducing the task of main CPU. Special processors in the keyboard,
converts the keystrokes into codes to be sent to the CPU.
The use of special-purpose microprocessors is common and does not turn a single-
processor system into a multiprocessor. If there is only one general-purpose CPU, then
the system is a single-processor system.
2. Economy of scale - Multiprocessor systems can cost less than equivalent number of many
single-processor systems. As the multiprocessor systems share peripherals, mass storage,
and power supplies, the cost of implementing this system is economical. If several
processes are working on the same data, the data can also be shared among them.
3. Increased reliability- In multiprocessor systems functions are shared among several
processors. If one processor fails, the system is not halted, it only slows down. The job of
the failed processor is taken up, by other processors.
Two techniques to maintain ‘Increased Reliability’ - graceful degradation & fault tolerant
Graceful degradation – As there are multiple processors when one processor fails other
process will take up its work and the system goes down slowly.
Fault tolerant – When one processor fails, its operations are stopped, the system failure
is then detected, diagnosed, and corrected.
The benefit of this model is that many processes can run simultaneously. N processes can run if
there are N CPUs—without causing a significant deterioration of performance. Operating
systems like Windows, Windows XP, Mac OS X, and Linux—now provide support for SMP. A
recent trend in CPU design is to include multiple compute cores on a single chip. The
communication between processors within a chip is faster than communication between two
single processors.
Clustered Systems
Clustered systems are two or more individual systems connected together via a network and
sharing software resources. Clustering provides high availability of resources and services. The
service will continue even if one or more systems in the cluster fail. High availability is generally
obtained by storing a copy of files (s/w resources) in the system.
There are two types of Clustered systems – asymmetric and symmetric
1. Asymmetric clustering – one system is in hot-standby mode while the others are
running the applications. The hot-standby host machine does nothing but monitor the
active server. If that server fails, the hot-standby host becomes the active server.
2. Symmetric clustering – two or more systems are running applications, and are
monitoring each other. This mode is more efficient, as it uses all of the available
hardware. If any system fails, its job is taken up by the monitoring system.
Other forms of clusters include parallel clusters and clustering over a wide-area network (WAN).
Parallel clusters allow multiple hosts to access the same data on the shared storage. Cluster
technology is changing rapidly with the help of SAN (storage-area networks). Using SAN
resources can be shared with dozens of systems in a cluster that are separated by miles.
This set of jobs is a subset of the jobs kept in the job pool. Since the number of jobs that
can be kept simultaneously in memory is usually smaller than the number of jobs that can
be kept in the job pool (in secondary memory). The operating system picks and begins to
execute one of the jobs in memory. Eventually, the job may have to wait for some tasks,
such as an I/O operation, to complete. In a non-multiprogramming system, the CPU
would sit idle.
In a multiprogrammed system, the operating system simply switches to, and executes,
another job. When that job needs to wait, the CPU is switched to another job, and so on.
Eventually, the first job finishes waiting and gets the CPU back. Thus, the CPU is never
idle.
Multiprogrammed systems provide an environment in which the various system resources
(for example, CPU, memory, and peripheral devices) are utilized effectively, but they do
not provide for user interaction with the computer system.
In Time sharing (or multitasking) systems, a single CPU executes multiple jobs by
switching among them, but the switches occur so frequently that the users can interact
with each program while it is running. The user feels that all the programs are being
Operating-System Operations
Modern operating systems are interrupt driven. If there are no processes to execute, no I/O
devices to service, and no users to whom to respond, an operating system will sit quietly, waiting
for something to happen. Events are signaled by the occurrence of an interrupt or a trap. A trap
(or an exception) is a software-generated interrupt. For each type of interrupt, separate segments
of code in the operating system determine what action should be taken. An interrupt service
routine is provided that is responsible for dealing with the interrupt.
Dual-Mode Operation
Since the operating system and the user programs share the hardware and software resources of
the computer system, it has to be made sure that an error in a user program cannot cause
problems to other programs and the Operating System running in the system. The approach taken
is to use a hardware support that allows us to differentiate among various modes of execution.
The system can be assumed to work in two separate modes of operation:
1. User mode
2. Kernel mode (supervisor mode, system mode, or privileged mode).
A hardware bit of the computer, called the mode bit, is used to indicate the current
mode: kernel (0) or user (1). With the mode bit, we are able to distinguish between a
task that is executed by the operating system and one that is executed by the user.
When the computer system is executing a user application, the system is in user mode.
When a user application requests a service from the operating system (via a system call),
the transition from user to kernel mode takes place.
At system boot time, the hardware starts in kernel mode. The operating system is then loaded
and starts user applications in user mode. Whenever a trap or interrupt occurs, the hardware
switches from user mode to kernel mode (that is, changes the mode bit from 1 to 0). Thus,
whenever the operating system gains control of the computer, it is in kernel mode.
The dual mode of operation provides us with the means for protecting the operating system
from errant users—and errant users from one another.
The hardware allows privileged instructions to be executed only in kernel mode. If an
attempt is made to execute a privileged instruction in user mode, the hardware does
not execute the instruction but rather treats it as illegal and traps it to the operating
system. The instruction to switch to user mode is an example of a privileged
instruction.
Initial control is within the operating system, where instructions are executed in kernel
mode. When control is given to a user application, the mode is set to user mode.
Eventually, control is switched back to the operating system via an interrupt, a trap, or
a system call.
Operating-System Services
An operating system provides an environment for the execution of programs. It provides certain
services to programs and to the users of those programs.
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).
System Calls
System calls provides an interface to the services of the operating system. These are
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
fromone 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
4. Information management
5. Communications
6. Protection
1) Process Control
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)
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
systemcalls.
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:
Shared memory is faster, and is generally the better approach where large amounts of
data are to be shared. This model is difficult to implement, and it consists of only few
system calls.
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.
PROCESS MANAGEMENT
Process Concept
A process is a program under execution.
Its current activity is indicated by PC (Program Counter) and the contents of the
processor's registers.
The Process
Process memory is divided into four sections as shown in the figure below:
The stack is used to store temporary data such as local variables, function
parameters, function return values, return address etc.
The heap which is memory that is dynamically allocated during process run time
The data section stores global variables.
The text section comprises the compiled program code.
Note that, there is a free space between the stack and the heap. When the stack is full, it
grows downwards and when the heap is full, it grows upwards.
Process in memory
Process State
A Process has 5 states. Each process may be in one of the following states –
1. New - The process is in the stage of being created.
2. Ready - The process has all the resources it needs to run. It is waiting to be assigned to
the processor.
Operations on Processes
Process Creation
A process may create several new processes. The creating process is called a parent
process, and the new processes are called the children of that process. Each of these
new processes may in turn create other processes. Every process has a unique process
ID.
On typical Solaris systems, the process at the top of the tree is the ‘sched’ process with
PID of 0. The ‘sched’ process creates several children processes – init, pageout and
fsflush. Pageout and fsflush are responsible for managing memory and file systems. The
init process with a PID of 1, serves as a parent process for all user processes.
A process will need certain resources (CPU time, memory, files, I/O devices) to accomplish its
task. When a process creates a subprocess, the subprocess may be able to obtain its resources in
two ways:
Directly from the operating system
Subprocess may take the resources of the parent process.
The resource can be taken from parent in two ways –
The parent may have to partition its resources among its children
Share the resources among several children.
There are two options for the parent process after creating the child:
Wait for the child process to terminate and then continue execution. The parent
Two possibilities for the address space of the child relative to the parent:
The child may be an exact duplicate of the parent, sharing the same program and data
segments in memory. Each will have their own PCB, including program counter,
registers, and PID. This is the behaviour of the fork system call in UNIX.
The child process may have a new program loaded into its address space, with all new code and
data segments. This is the behaviour of the spawn system calls in Windows
In UNIX OS, a child process can be created by fork() system call. The fork system call, if
successful, returns the PID of the child process to its parents and returns a zero to the child
process. If failure, it returns -1 to the parent. Process IDs of current process or its direct parent
can be accessed using the getpid( ) and getppid( ) system calls respectively.
The parent waits for the child process to complete with the wait() system call. When the child
process completes, the parent process resumes and completes its execution.
In windows the child process is created using the function createprocess( ). The createprocess( )
returns 1, if the child is created and returns 0, if the child is not created.
Process Termination
A process terminates when it finishes executing its last statement and asks the operating
system to delete it, by using the exit () system call. All of the resources assigned to the
process like memory, open files, and I/O buffers, are deallocated by the operating
system.
A process can cause the termination of another process by using appropriate system call.
The parent process can terminate its child processes by knowing of the PID of the child.
A parent may terminate the execution of children for a variety of reasons, such as:
The child has exceeded its usage of the resources, it has been allocated.
The task assigned to the child is no longer required.
The parent is exiting, and the operating system terminates all the children.
This is called cascading termination.
Interprocess Communication
Interprocess Communication- Processes executing may be either co-operative or independent
processes.
Independent Processes – processes that cannot affect other processes or be affected by
other processes executing in the system.
Cooperating Processes – processes that can affect other processes or be affected by
other processes executing in the system.
Co-operation among processes are allowed for following reasons –
Information Sharing - There may be several processes which need to access the same
file. So the information must be accessible at the same time to all users.
Computation speedup - Often a solution to a problem can be solved faster if the
problem canbe broken down into sub-tasks, which are solved simultaneously (particularly
when multiple processors are involved.)
Modularity - A system can be divided into cooperating modules and executed by
sending information among one another.
Convenience - Even a single user can work on multiple tasks by information sharing.
1) Shared-Memory Systems
A region of shared-memory is created within the address space of a process, which
needs to communicate. Other process that needs to communicate uses this shared
memory.
The form of data and position of creating shared memory area is decided by the process.
Generally, a few messages must be passed back and forth between the cooperating
processes first in order to set up and coordinate the shared memory access.
The process should take care that the two processes will not write the data to the shared
memory at the same time.
This example uses shared memory as a circular queue. The in and out are two pointers to the
array. Note in the code below that only the producer changes "in", and only the consumer changes
"out".
The producer process – Note that the buffer is full when [ (in+1) % BUFFER_SIZE == out]
The consumer process – Note that the buffer is empty when [ in == out]
2) Message-Passing Systems
A mechanism to allow process communication without sharing address space. It is used in
distributed systems.
Message passing systems uses system calls for "send message" and "receive message".
A communication link must be established between the cooperating processes before
messages can be sent.
There are three methods of creating the link between the sender and the receiver.
Direct or indirect communication (naming)
Synchronous or asynchronous communication (Synchronization)
Automatic or explicit buffering.
1) Naming
Processes that want to communicate must have a way to refer to each other. They can use either
direct or indirect communication.
a) Direct communication- the sender and receiver must explicitly know each other’s name.
The syntax for send() and receive() functions are as follows-
send (P, message) – send a message to process P
receive (Q, message) – receive a message from process Q
Disadvantages of direct communication – any changes in the identifier of a process, may have to
change the identifier in the whole system (sender and receiver), where the messages are sent and
received.
2) Synchronization
The send and receive messages can be implemented as either blocking or non-blocking.
Blocking (synchronous) send - sending process is blocked (waits) until the
message is received by receiving process or the mailbox.
Non-blocking (asynchronous) send - sends the message and continues (does not wait)
Blocking (synchronous) receive - The receiving process is blocked until a message is
available.
Non-blocking (asynchronous) receive - receives the message without block. The
received message may be a valid message or null.
3) Buffering
When messages are passed, a temporary queue is created. Such queue can be of three capacities:
Zero capacity – The buffer size is zero (buffer does not exist). Messages are not stored
in the queue. The senders must block until receivers accept the messages.
Bounded capacity- The queue is of fixed size(n). Senders must block if the queue is
full. After sending ‘n’ bytes the sender is blocked.
Unbounded capacity - The queue is of infinite capacity. The sender never blocks.
MULTITHREADED PROGRAMMING
Overview
A thread is a basic unit of CPU utilization
It consists of
thread ID
PC
register-set and
stack
It shares with other threads belonging to the same process its code-section &data-section.
A traditional (or heavy weight) process has a single thread of control.
If a process has multiple threads of control, it can perform more than one task at a time.
Such a process is called multithreaded process.
Multithreading Models
Support for threads may be provided at either
The user level, for user threads or
By the kernel, for kernel threads.
User-threads are supported above the kernel and are managed without kernel support.
Kernel-threads are supported and managed directly by the OS.
1) Many-to-one model
2) One-to-one model
Each user thread is mapped to a kernel thread.
Advantages:
It provides more concurrency by allowing another thread to run when a thread makes a
blocking system-call.
Multiple threads can run in parallel on multiprocessors.
Disadvantage:
3) Many-to-many model
Many user-level threads are multiplexed to a smaller number of kernel threads.
Advantages:
Developers can create as many user threads as necessary
The kernel threads can run in parallel on a multiprocessor.
When a thread performs a blocking system-call, kernel can schedule another thread for
execution.
Two Level Model
A variation on the many-to-many model is the two level-model
Similar to M:N, except that it allows a user thread to be bound to kernel thread.
For example:
HP-UX
Tru64 UNIX