OS Unit I
OS Unit I
5.Error handling:
Errors can occur anytime and anywhere. An error may occur in CPU, in I/O devices
or in the memory hardware. Following are the major activities of an operating system
with respect to error handling.
6.Resource Management:
In case of multi-user or multi-tasking environment, resources such as main memory,
CPU cycles and files storage are to be allocated to each user or job. Following are the
major activities of an operating system with respect to resource management.
7.Protection:
Considering a computer system having multiple users and concurrent execution of
multiple processes, the various processes must be protected from each other's
activities.Protection refers to a mechanism or a way to control the access of programs,
processes, or users to the resources defined by a computer system. Following are the
major activities of an operating system with respect to protection.
3.Multiprogramming OS:
In a multiprogramming OS more than one program can be used at the same time. It
may or may not be multiprocessing. In a single CPU system, multiple programs are
executed one after another by dividing the CPU into small time slice.
example: Windows, Mac, Linuxetc.
4. Multitasking OS:
In a multitasking system more than one task can be performed at the same time but
they are executed one after another through a single CPU by time sharing. For
example: Windows, Linux, Mac, Unixetc.
Multitasking OS are of two types:
a) Pre-emptive multitasking
b) Co-operative multitasking
In the pre-emptive multitasking, the OS allows CPU times slice to each program.
After each time slice, CPU executes another task. Example: Windows XP
In co-operative multitasking, a task can control CPU as long as it requires. However,
it will free CPU to execute another program if it doesn’t require CPU. Example:
windows 3.x, multi finder etc.
5. Multithreading:
6. Batch Processing:
A batch processing is a group of processing system in which all the required input of
all the processing task is provided initially. The result of all the task is provided after
the completion of all the processing. Its main functions are:
7.Online Processing:
SYSTEM CALLS
The interface between a process and an operating system is provided by system calls.
In general, system calls are available as assembly language instructions. They are also
included in the manuals used by the assembly level programmers. System calls are
usually made when a process in user mode requires access to a resource. Then it
requests the kernel to provide the resource via a system call.
A figure representing the execution of the system call is given as follows
As can be seen from this diagram, the processes execute normally in the user mode
until a system call interrupts this. Then the system call is executed on a priority basis
in the kernel mode. After the execution of the system call, the control returns to the
user mode and execution of user processes can be resumed.
In general, system calls are required in the following situations −
If a file system requires the creation or deletion of files. Reading and writing
from files also require a system call.
Creation and management of new processes.
Network connections also require system calls. This includes sending and
receiving packets.
Access to a hardware devices such as a printer, scanner etc. requires a system
call.
Types of System Calls
There are mainly five types of system calls. These are explained in detail as follows −
1. Process Control
These system calls deal with processes such as process creation, process termination
etc.
2. File Management
These system calls are responsible for file manipulation such as creating a file, reading
a file, writing into a file etc.
3. Device Management
These system calls are responsible for device manipulation such as reading from
device buffers, writing into device buffers etc.
4. Information Maintenance
These system calls handle information and its transfer between the operating system
and the user program.
5. Communication
These system calls are useful for inter process communication. They also deal with
creating and deleting a communication connection.
Some of the examples of all the above types of system calls in Windows and Unix are
given as follows
Types of
Windows Linux
System Calls
CreateProcess() fork()
Process
ExitProcess() exit()
Control
WaitForSingleObject() wait()
CreateFile() open()
File ReadFile read()
Management ()WriteFile() write()c
CloseHandle() lose()
SetConsoleMode() ioctl()
Device
ReadConsole() read()
Management
WriteConsole() write()
GetCurrentProcessID() getpid()
Information
SetTimer() alarm()
Maintenance
Sleep() sleep()
CreatePipe() pipe()
Communicatio
CreateFileMapping() shmget()
n
MapViewOfFile() mmap()
open()
The open() system call is used to provide access to a file in a file system. This system
call allocates resources to the file and provides a handle that the process uses to refer to
the file. A file can be opened by multiple processes at the same time or be restricted to
one process. It all depends on the file organization and file system.
read()
The read() system call is used to access data from a file that is stored in the file system.
The file to read can be identified by its file descriptor and it should be opened using
open() before it can be read. In general, the read() system calls takes three arguments
i.e. the file descriptor, buffer which stores read data and number of bytes to be read
from the file.
write()
The write() system calls writes the data from a user buffer into a device such as a file.
This system call is one of the ways to output data from a program. In general, the write
system calls takes three arguments i.e. file descriptor, pointer to the buffer where data
is stored and number of bytes to write from the buffer.
close()
The close() system call is used to terminate access to a file system. Using this system
call means that the file is no longer required by the program and so the buffers are
flushed, the file metadata is updated and the file resources are de-allocated.
SYSTEM PROGRAMS
In an operating system a user is able to use different types of system programs and the
system program is responsible for all the application software performance of the
computer.
The system programs are responsible for the development and execution of a program
and they can be used by the help of system calls because system calls define different
types of system programs for different tasks.
File management − These programs create, delete, copy, rename, print, exit and
generally manipulate the files and directory.
Status information − It is the information regarding input, output process,
storage and the CPU utilization time how the process will be calculated in how
much memory required to perform a task is known by status information.
Programming language supports − compiler, assembler, interrupter are
programming language support used in the operating system for a particular
purpose in the computer.
Program Loading and execution − The needs to enter the program and after the
loading of a program it needs to execute the output of the programs and this task
is also performed by system calls by the help of system programs.
Communication − These services are provided by the user because by using this
number of devices communicates with each other by the help of device or
wireless and communication is necessary for the operating system.
Background services − There are different types of services available on the
operating system for communication and background service is used to change
the background of your window and it also works for scanning and detecting
viruses in the computer.
Operating System Design:
It is quite complicated to define all the goals and specifications of the operating system
while designing it. The design changes depending on the type of the operating system
i.e., if it is batch system, time shared system, single user system, multi user system,
distributed system etc.
There are basically two types of goals while designing an operating system. These are
User Goals
The operating system should be convenient, easy to use, reliable, safe and fast
according to the users. However, these specifications are not very useful as there is no
set method to achieve these goals.
System Goals
The operating system should be easy to design, implement and maintain. These are
specifications required by those who create, maintain and operate the operating
system. But there is not specific method to achieve these goals as well.
Operating System Mechanisms and Policies
There is no specific way to design an operating system as it is a highly creative task.
However, there are general software principles that are applicable to all operating
systems.
A subtle difference between mechanism and policy is that mechanism shows how to
do something and policy shows what to do. Policies may change over time and this
would lead to changes in mechanism. So, it is better to have a general mechanism that
would require few changes even when a policy change occurs.
For example - If the mechanism and policy are independent, then few changes are
required in mechanism if policy changes. If a policy favours I/O intensive processes
over CPU intensive processes, then a policy change to preference of CPU intensive
processes will not change the mechanism.
Operating System Implementation
The operating system needs to be implemented after it is designed. Earlier they were
written in assembly language but now higher level languages are used. The first system
not written in assembly language was the Master Control Program (MCP) for
Burroughs Computers.
Advantages of Higher Level Language
There are multiple advantages to implementing an operating system using a higher
level language such as: the code is written more fast, it is compact and also easier to
debug and understand. Also, the operating system can be easily moved from one
hardware to another if it is written in a high level language.
Disadvantages of Higher Level Language
Using high level language for implementing an operating system leads to a loss in
speed and increase in storage requirements. However in modern systems only a small
amount of code is needed for high performance, such as the CPU scheduler and
memory manager. Also, the bottleneck routines in the system can be replaced by
assembly language equivalents if required.
INTRODUCTION TO PROCESS
Process:
The Program in execution is called as Process. The execution of a process must
progress in a sequential fashion.
When we execute this program, it becomes a process which performs all the
tasks mentioned in the program.
When a program is loaded into the memory and it becomes a process, it can be
divided into four sections ─ stack, heap, text and data. The following image shows a
simplified layout of a process inside main memory.
1 Stack
The process Stack contains the temporary data such as method/function
parameters, return address and local variables.
2 Heap
This is dynamically allocated memory to a process during its run time.
3 Text
This includes the current activity represented by the value of Program
Counter and the contents of the processor's registers.
4 Data
This section contains the global and static variables.
When a process executes, it passes through different states. These stages may differ in
different operating systems. In general, a process can have one of the following five
states at a time.
1 Start
This is the initial state when a process is first started / created.
2 Ready
The process is waiting to be assigned to a processor. Ready processes are waiting to
have the processor allocated to them by the operating system so that they can run.
Process may come into this state after Start state or while running it by but
interrupted by the scheduler to assign CPU to some other process.
3 Running
Once the process has been assigned to a processor by the OS scheduler, the process
state is set to running and the processor executes its instructions.
4 Waiting
Process moves into the waiting state if it needs to wait for a resource, such as waiting
for user input, or waiting for a file to become available.
5 Terminated or Exit
Once the process finishes its execution, or it is terminated by the operating system, it
is moved to the terminated state where it waits to be removed from main memory.
A Process Control Block is a data structure maintained by the Operating System for
every process. The PCB is identified by an integer process ID (PID). A PCB keeps all
the information needed to keep track of a process as listed below in the table:
1 Process State
The current state of the process i.e., whether it is ready, running, waiting, or
whatever.
2 Process privileges
This is required to allow/disallow access to system resources.
3 Process ID
Unique identification for each of the process in the operating system.
4 Pointer
A pointer to parent process.
5 Program Counter
Program Counter is a pointer to the address of the next instruction to be executed for
this process.
6 CPU registers
Various CPU registers where process need to be stored for execution for running
state.
9 Accounting information
This includes the amount of CPU used for process execution, time limits, execution
ID etc.
10 IO status information
This includes a list of I/O devices allocated to the process.
Definition
The process scheduling is the activity of the process manager that handles the
removal of the running process from the CPU and the selection of another process on
the basis of a particular strategy.
The Operating System maintains the following important process scheduling queues:
Job queue − This queue keeps all the processes in the system.
Ready queue − This queue keeps a set of all processes residing in main
memory, ready and waiting to execute. A new process is always put in this
queue.
Short-Term Scheduler
Medium-Term Scheduler
Long Term Scheduler
The primary objective of the job scheduler is to provide a balanced mix of jobs,
such as I/O bound and processor bound. It also controls the degree of
multiprogramming. If the degree of multiprogramming is stable, then the average rate
of process creation must be equal to the average departure rate of processes leaving
the system.
Context Switch
A context switch is the mechanism to store and restore the state or context of a
CPU in Process Control block so that a process execution can be resumed from the
same point at a later time. Using this technique, a context switcher enables multiple
processes to share a single CPU.
Operations on Process
1. Process Creation
2. Process Termination
Process Creation
1. System initialization
Numerous processes are created when an operating system is booted. Some of
them are
Foreground processes − Processes that interact with users and perform work
for them.
The running process will issue system calls to create one or more new processes
to help it do its job.
In UNIX, the system call which is used to create a new process is fork()
When the operating system creates a new process and runs the next job from the
input queue in it.
Process Termination
Error exit – This type of error occurs due to program bugs like executing an
illegal instruction, referencing, or dividing by zero.
Any process that shares data with the other process is a Cooperating process.
Inter process communication comes under Cooperating processes.
Advantages of IPC
a) Information sharing
b) Computational speedup
c) Modularity
d) Convenience
1. Shared memory
2. Message Passing
1. Shared memory:
In this model, a region of memory is shared by the cooperating
processes is established.
Process can exchange information by reading and writing the data
to the shared region.
Suppose process1 and process2 are executing simultaneously and
they share some resources or use some information from another process.
Process1 generate information about certain computations or resources
being used and keeps it as a record in shared memory. When process2
needs to use the shared information, it will check in the record stored in
shared memory and take note of the information generated by process1
and act accordingly.
Eg: Producer-Consumer problem
There are two processes: Producer and Consumer. Producer produces
some item and Consumer consumes that item. The two processes share a
common space or memory location known as a buffer where the item
produced by Producer is stored and from which the Consumer consumes
the item, if needed.
2. Message Passing:
3. Pipes
These are Inter process communication methods that contain two end points. Data is
entered from one end of the pipe by a process and consumed from the other end by the
other process.
The two different types of pipes are ordinary pipes and named pipes. Ordinary pipes
only allow one way communication. For two way communication, two pipes are
required. Ordinary pipes have a parent child relationship between the processes as the
pipes can only be accessed by processes that created or inherited them.
Named pipes are more powerful than ordinary pipes and allow two way
communication. These pipes exist even after the processes using them have
terminated. They need to be explicitly deleted when not required anymore.
A diagram that demonstrates pipes are given as follows:
Thread in OS
Thread is a sequential flow of tasks within a process. Threads in OS can be of the
same or different types. Threads are used to increase the performance of the
applications.
Each thread has its own program counter, stack, and set of registers. But the threads of
a single process might share the same code and data/file. Threads are also termed as
lightweight processes as they share common resources.
Eg: While playing a movie on a device the audio and video are controlled by different
threads in the background.