unit1_OS
unit1_OS
Define OS:
Objectives of OS:
1. Convenience: An OS makes a computer more convenient to use.
2. Efficiency: An OS allows the computer system resources to be used in an efficient
manner.
3. Ability to evolve: An OS should be constructed in such a way as to permit the
effective development, testing, and introduction of new system functions without
interfering with service.
Functions of an operating System:
Memory Management: Memory management refers to management of Primary
Memory or Main Memory. An Operating System does the following activities for memory
management: OS Keeps tracks of primary memory, i.e., what part of it are in use by
whom, what part are not in use. In multi-programming, the OS decides which process
will get memory when and how much.OS allocates the memory when a process
requests it to do so. It de-allocates the memory when a process no longer needs it or
has been terminated.
Processor Management: In multi-programming environment, the OS decides which
process gets the processor when and for how much time. This function is called process
scheduling. An Operating System does the following activities for processor
management: OS keeps tracks of processor and status of process.OS allocates the
processor (CPU) to a process. It de-allocates processor when a process is no longer
required.
Device Management: An Operating System manages device communication via their
respective drivers. It does the following activities for device management: Keeps tracks
of all devices. The program responsible for this task is known as the I/O controller.
Decides which process gets the device when and for how much time. OS allocates the
device in the most efficient way.It de-allocates devices in most efficient way.
File Management: A file system is normally organized into directories for easy
navigation and usage. These directories may contain files and other directions.An
Operating System does the following activities for file management: Keeps track of
information, location, uses, status etc. The collective facilities are often known as file
system. OS Decides who gets the resources.It allocates the resources and also de-
allocates the resources when not in need.
Security: OS prevents unauthorized access to programs and data.For shared or public
systems, the OS controls access to the system as a whole and to specific system
resources.
Control over system performance: OS will collect usage statistics for various
resources and monitor performance parameters such as response time, Recording
delays between request for a service and response from the system.
Job accounting: OS Keeps track of time and resources used by various jobs and
users.On any system, this information is useful in anticipating the need for future
enhancements and in tuning the system to improve performance and can be used for
job accounting purposes.
Error detection & Response: A variety of errors can occur while a computer system is
running. These include internal and external hardware errors, such as a memory error,
or a device failure or malfunction; and various software errors. In each case, the OS
must provide a response that clears the error condition with the least impact on running
applications. The response may range from ending the program that caused the error,
to retrying the operation, to simply reporting the error to the application, Production of
dumps, traces, error messages, and other debugging and error detecting aids.
Booting the computer: Booting is the process of starting or restarting the computer. If
computer is switched off completely and then turned on then it is cold booting. If
computer is restarted then it is warm booting. Booting of the computer is done by OS.
Coordination between other software and users: An OS enables coordination of
hardware components, coordination and assignment of compilers, interpreters,
assemblers and other software to the various users of the computer systems.
Simple Structure
Operating systems such as MS-DOS and the original UNIX did not have well-
defined structures.
There was no CPU Execution Mode (user and kernel), and so errors in
applications could cause the whole system to crash.
Monolithic
Functionality of the OS is invoked with simple function calls within the kernel,
which is one large program.
Device drivers are loaded into the running kernel and become part of the kernel.
Layered Approach
Microkernels
This structures the operating system by removing all nonessential portions of the kernel
and implementing them as system and user level programs.
System Calls
The system call provides an interface to the operating system services.
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.
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.
1. Process Control
2. File Management
3. Device Management
4. Information Maintenance
5. Communication
Process Control
Process control is the system call that is used to direct the processes. Some
process control examples include creating, load, abort, end, execute,
process, terminate the process, etc.
File Management
File management is a system call that is used to handle the files. Some file
management examples include creating files, delete files, open, close, read,
write, etc.
Device Management
Device management is a system call that is used to deal with devices. Some
examples of device management include read, device, write, get device
attributes, release device, etc.
Information Maintenance
Information maintenance is a system call that is used to maintain
information. There are some examples of information maintenance, including
getting system data, set time or date, get time or date, set system data, etc.
Communication
Communication is a system call that is used for communication. There are
some examples of communication, including create, delete communication
connections, send, receive messages, etc.
System Programs
System programs provide an environment where programs can be developed and executed. In
the simplest sense, system programs also provide a bridge between the user interface and
system calls. In reality, they are much more complex. For example, a compiler is a complex
system program.
The system program serves as a part of the operating system. It traditionally lies
between the user interface and the system calls. The user view of the system is actually
defined by system programs and not system calls because that is what they interact
with and system programs are closer to the user interface.
An image that describes system programs in the operating system hierarchy is as
follows −
In the above image, system programs as well as application programs form a bridge between
the user interface and the system calls. So, from the user view the operating system observed is
actually the system programs and not the system calls.
1. File Management –
A file is a collection of specific information stored in the memory of a
computer system. File management is defined as the process of
manipulating files in the computer system, its management includes the
process of creating, modifying and deleting files.
It helps to create new files in the computer system and placing them at
specific locations.
It helps in easily and quickly locating these files in the computer system.
It makes the process of sharing files among different users very easy and
user-friendly.
It helps to store files in separate folders known as directories.
These directories help users to search files quickly or to manage files
according to their types of uses.
It helps users to modify the data of files or to modify the name of files in
directories.
2. Status Information –
Information like date, time amount of available memory, or disk space is
asked by some users. Others providing detailed performance, logging, and
debugging information which is more complex. All this information is
formatted and displayed on output devices or printed. Terminal or other
output devices or files or a window of GUI is used for showing the output of
programs.
3. File Modification –
For modifying the contents of files we use this. For Files stored on disks or
other storage devices, we used different types of editors. For searching
contents of files or perform transformations of files we use special
commands.
4. Programming-Language support –
For common programming languages, we use Compilers, Assemblers,
Debuggers, and interpreters which are already provided to users. It provides
all support to users. We can run any programming language. All languages
of importance are already provided.
6. Communications –
Virtual connections among processes, users, and computer systems are
provided by programs. Users can send messages to another user on their
screen, User can send e-mail, browsing on web pages, remote login, the
transformation of files from one user to another.
Process
A process is basically a program in execution. The execution of a process must
progress in a sequential fashion.
A process is defined as an entity which represents the basic unit of work to be
implemented in the system.
To put it in simple terms, we write our computer programs in a text file and 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 −
Process Scheduling
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.
Process scheduling is an essential part of a Multiprogramming operating systems.
Such operating systems allow more than one process to be loaded into the executable
memory at a time and the loaded process shares the CPU using time multiplexing.
Schedulers
Schedulers are special system software which handle process scheduling in various
ways. Their main task is to select the jobs to be submitted into the system and to
decide which process to run. Schedulers are of three types −
Long-Term Scheduler
Short-Term Scheduler
Medium-Term Scheduler
Operation on a Process:
The execution of a process is a complex activity. It involves various
operations. Following are the operations that are performed while execution
of a process:
1. Creation: This the initial step of process execution activity. Process creation
means the construction of a new process for the execution. This might be
performed by system, user or old process itself. There are several events that
leads to the process creation. Some of the such events are following:
When we start the computer, system creates several background processes.
A user may request to create a new process.
A process can create a new process itself while executing.
Batch system takes initiation of a batch job.
2. Scheduling/Dispatching: The event or activity in which the state of the
process is changed from ready to running. It means the operating system puts
the process from ready state into the running state. Dispatching is done by
operating system when the resources are free or the process has higher priority
than the ongoing process. There are various other cases in which the process
in running state is preempted and process in ready state is dispatched by the
operating system.
3. Blocking: When a process invokes an input-output system call that blocks
the process and operating system put in block mode. Block mode is basically a
mode where process waits for input-output. Hence on the demand of process
itself, operating system blocks the process and dispatches another process to
the processor. Hence, in process blocking operation, the operating system puts
the process in ‘waiting’ state.
4. Preemption: When a timeout occurs that means the process hadn’t been
terminated in the allotted time interval and next process is ready to execute,
then the operating system preempts the process. This operation is only valid
where CPU scheduling supports preemption. Basically this happens in priority
scheduling where on the incoming of high priority process the ongoing process
is preempted. Hence, in process preemption operation, the operating system
puts the process in ‘ready’ state.
5. Termination: Process termination is the activity of ending the process. In
other words, process termination is the relaxation of computer resources taken
by the process for the execution. Like creation, in termination also there may be
several events that may lead to the process termination. Some of them are:
Process completes its execution fully and it indicates to the OS that it has
finished.
Operating system itself terminates the process due to service errors.
There may be problem in hardware that terminates the process.
One process can be terminated by another process.
Cooperating processes
In the computer system, there are many processes which may be either
independent processes or cooperating processes that run in the operating
system. A process is said to be independent when it cannot affect or be
affected by any other processes that are running the system. It is clear that
any process which does not share any data (temporary or persistent) with
any another process then the process independent. On the other hand, a
cooperating process is one which can affect or affected by any another
process that is running on the computer. the cooperating process is one
which shares data with another process.
There are several reasons for providing an environment that allows process
cooperation:
Information sharing
In the information sharing at the same time, many users may want the same
piece of information(for instance, a shared file) and we try to provide that
environment in which the users are allowed to concurrent access to these
types of resources.
Computation speedup
When we want a task that our process run faster so we break it into a
subtask, and each subtask will be executing in parallel with another one. It is
noticed that the speedup can be achieved only if the computer has multiple
processing elements (such as CPUs or I/O channels).
Modularity
In the modularity, we are trying to construct the system in such a modular
fashion, in which the system dividing its functions into separate processes.
Convenience
An individual user may have many tasks to perform at the same time and
the user is able to do his work like editing, printing and compiling.
1. Directly share a logical address data space (i.e. code & data) -
This may result in data inconsistency. It is implemented on threads.
2. Share data only through files/ messages - So we will deal with
various to order....orderly execution of cooperating process so that
data consistency is maintained.
Here,
Shared Resources
1. buffer
2. counter
When producer and consumer are not executed can current then
inconsistency arises. Here the value of a counter that is used by both
producer and consumer will be wrong if both are executed concurrently
without any control. The producer and consumer processes share the
following variables:
var n;
type item = .....;
var Buffer : array [0,n-1] of item;
In, out:0..n-1;
With the variables in and out initialized to the value 0. In The shared buffer
there are two logical pointers; in and out that is implemented in the form of
a circular array. The in variables points to the next free position in the buffer
and out variable points to the first full position in the buffer. When, in =
out the buffer is empty and when in+1 mod n = out the buffer is full.
Inter Process Communication (IPC)
1. Shared Memory
2. Message passing
Figure 1 below shows a basic structure of communication between processes
via the shared memory method and via the message passing method.
#define buff_max 25
#define mod %
struct item{
// or consumed data
---------
int free_index = 0;
int full_index = 0;
C
item nextProduced;
while(1){
// for production.
// if so keep waiting.
shared_buff[free_index] = nextProduced;
C
item nextConsumed;
while(1){
while((free_index == full_index);
nextConsumed = shared_buff[full_index];
In the above code, the Producer will start producing again when the
(free_index+1) mod buff max will be free because if it it not free, this implies
that there are still items that can be consumed by the Consumer so there is no
need to produce more. Similarly, if free index and full index point to the same
index, this implies that there are no items to consume.
The message size can be of fixed size or of variable size. If it is of fixed size, it
is easy for an OS designer but complicated for a programmer and if it is of
variable size then it is easy for a programmer but complicated for the OS
designer. A standard message can have two parts: header and body.
The header part is used for storing message type, destination id, source id,
message length, and control information. The control information contains
information like what to do if runs out of buffer space, sequence number,
priority. Generally, message is sent using FIFO style.
C
void Producer(void){
int item;
Message m;
while(1){
receive(Consumer, &m);
item = produce();
build_message(&m , item ) ;
send(Consumer, &m);
Consumer Code
C
void Consumer(void){
int item;
Message m;
while(1){
receive(Producer, &m);
item = extracted_item();
send(Producer, &m);
consume_item(item);
}
Client/Server Communication
Client/Server communication involves two components, namely a client and a server.
They are usually multiple clients in communication with a single server. The clients send
requests to the server and the server responds to the client requests.
There are three main methods to client/server communication. These are given as
follows −
Sockets
Sockets facilitate communication between two processes on the same machine or
different machines. They are used in a client/server framework and consist of the IP
address and port number. Many application protocols use sockets for data connection
and data transfer between a client and a server.
Socket communication is quite low-level as sockets only transfer an unstructured byte
stream across processes. The structure on the byte stream is imposed by the client and
server applications.
A diagram that illustrates sockets is as follows −
Threading issues:
The fork() and exec() system calls
The fork() is used to create a duplicate process. The meaning of the fork() and exec()
system calls change in a multithreaded program.
If one thread in a program which calls fork(), does the new process duplicate all threads,
or is the new process single-threaded? If we take, some UNIX systems have chosen to
have two versions of fork(), one that duplicates all threads and another that duplicates
only the thread that invoked the fork() system call.
If a thread calls the exec() system call, the program specified in the parameter to exec()
will replace the entire process which includes all threads.
Signal Handling
Generally, signal is used in UNIX systems to notify a process that a particular event has
occurred. A signal received either synchronously or asynchronously, based on the
source of and the reason for the event being signaled.
All signals, whether synchronous or asynchronous, follow the same pattern as given
below −
A signal is generated by the occurrence of a particular event.
The signal is delivered to a process.
Cancellation
Thread cancellation is the task of terminating a thread before it has completed.
For example − If multiple database threads are concurrently searching through a
database and one thread returns the result the remaining threads might be cancelled.
A target thread is a thread that is to be cancelled, cancellation of target thread may
occur in two different scenarios −
Asynchronous cancellation − One thread immediately terminates the target
thread.
Deferred cancellation − The target thread periodically checks whether it should
terminate, allowing it an opportunity to terminate itself in an ordinary fashion.
Thread polls
Multithreading in a web server, whenever the server receives a request it creates a
separate thread to service the request.
Some of the problems that arise in creating a thread are as follows −
The amount of time required to create the thread prior to serving the request
together with the fact that this thread will be discarded once it has completed its
work.
If all concurrent requests are allowed to be serviced in a new thread, there is no
bound on the number of threads concurrently active in the system.
Unlimited thread could exhaust system resources like CPU time or memory.
A thread pool is to create a number of threads at process start-up and place them into a
pool, where they sit and wait for work.