Operating System Notes
Operating System Notes
No matter its size and application, every computer needs an operating system to make it functional
and useful. The operating system is an integral part of modern computer systems. It is a well-
organized collection of programs that manages the hardware.
An Operating System provides an interaction between the users and computer hardware. A user is a
person sitting at the computer terminal concerned about the application rather than the architecture
of the computer. The user never interacts with the hardware directly. To get the services of the
hardware, he has to request through the operating system.
The operating system is a primary resource manager. It manages the hardware, including processors,
memory, Input-Output devices, and communication devices.
40.7M
904
Next
Stay
The operating system operates either in kernel mode or user mode. Compilers and editors run in user
mode, whereas operating system code runs in kernel mode.
Types of Operating Systems
An operating system is a well-organized collection of programs that manages the computer
hardware. It is a type of system software that is responsible for the smooth functioning of the
computer system.
In Batch operating system, access is given to more than one person; they submit their respective jobs
to the system for the execution.
The system put all of the jobs in a queue on the basis of first come first serve and then executes the
jobs one by one. The users collect their respective output when all the jobs get executed.
The purpose of this operating system was mainly to transfer control from one job to another as soon
as the job was completed. It contained a small set of programs called the resident monitor that
always resided in one part of the main memory. The remaining part is used for servicing jobs.
Advantages of Batch OS
o The use of a resident monitor improves computer efficiency as it eliminates CPU time between
two jobs.
Disadvantages of Batch OS
1. Starvation
For Example:
There are five jobs J1, J2, J3, J4, and J5, present in the batch. If the execution time of J1 is very high,
then the other four jobs will never be executed, or they will have to wait for a very long time. Hence
the other processes get starved.
2. Not Interactive
Batch Processing is not suitable for jobs that are dependent on the user's input. If a job requires the
input of two numbers from the console, then it will never get it in the batch processing scenario since
the user is not present at the time of execution.
In a multiprogramming environment, when a process does its I/O, The CPU can start the execution of
other processes. Therefore, multiprogramming improves the efficiency of the system.
Advantages of Multiprogramming OS
o Throughout the system, it increased as the CPU always had one program to execute.
o Response time can also be reduced.
Disadvantages of Multiprogramming OS
o Increased reliability: Due to the multiprocessing system, processing tasks can be distributed
among several processors. This increases reliability as if one processor fails, the task can be
given to another processor for completion.
o Increased throughout: As several processors increase, more work can be done in less.
o The multiple processors are busier at the same time to complete any task in a multitasking
environment, so the CPU generates more heat.
o In this type of operating system, network traffic reduces due to the division between clients
and the server.
o This type of system is less expensive to set up and maintain.
o In this type of operating system, the failure of any node in a system affects the whole system.
o Security and performance are important issues. So trained network administrators are required
for network administration.
The Application of a Real-Time system exists in the case of military applications, if you want to drop a
missile, then the missile is supposed to be dropped with a certain precision.
o Easy to layout, develop and execute real-time applications under the real-time operating
system.
o In a Real-time operating system, the maximum utilization of devices and systems.
o The time-sharing operating system provides effective utilization and sharing of resources.
o This system reduces CPU idle and response time.
o Program execution
o Control Input/output devices
o Program creation
o Error Detection and Response
o Accounting
o Security and Protection
o File Management
o Communication
Program execution
To execute a program, several tasks need to be performed. Both the instructions and data must be
loaded into the main memory. In addition, input-output devices and files should be initialized, and
other resources must be prepared. The Operating structures handle these kinds of tasks. The user
now no longer should fear the reminiscence allocation or multitasking or anything.
As there are numerous types of I/O devices within the computer system, and each I/O device calls for
its own precise set of instructions for the operation. The Operating System hides that info with the aid
of presenting a uniform interface. Thus, it is convenient for programmers to access such devices
easily.
Program Creation
The Operating system offers the structures and tools, including editors and debuggers, to help the
programmer create, modify, and debugging programs.
An Error in a device may also cause malfunctioning of the entire device. These include hardware and
software errors such as device failure, memory error, division by zero, attempts to access forbidden
memory locations, etc. To avoid error, the operating system monitors the system for detecting errors
and takes suitable action with at least impact on running applications.
While working with computers, errors may occur quite often. Errors may occur in the:
o Input/ Output devices: For example, connection failure in the network, lack of paper in the
printer, etc.
o User program: For example: attempt to access illegal memory locations, divide by zero, use
too much CPU time, etc.
o Memory hardware: For example, Memory error, the memory becomes full, etc.
To handle these errors and other types of possible errors, the operating system takes appropriate
action and generates messages to ensure correct and consistent computing.
Accounting
An Operating device collects utilization records for numerous assets and tracks the overall
performance parameters and responsive time to enhance overall performance. These personal
records are beneficial for additional upgrades and tuning the device to enhance overall performance.
Operating device affords safety to the statistics and packages of a person and protects any
interference from unauthorized users. The safety feature counters threats, which are published via
way of individuals out of doors the manage of the running device.
For Example:
When a user downloads something from the internet, that program may contain malicious code that
may harm the already existing programs. The operating system ensures that proper checks are
applied while downloading such programs.
If one computer system is shared amongst a couple of users, then the various processes must be
protected from another intrusion. For this, the operating system provides various mechanisms that
allow only those processes to use resources that have gained proper authorization from the
operating system. The mechanism may include providing unique users ids and passwords to each
user.
File management
Computers keep data and information on secondary storage devices like magnetic tape, magnetic
disk, optical disk, etc. Each storage media has its capabilities like speed, capacity, data transfer rate,
and data access methods.
For file management, the operating system must know the types of different files and the
characteristics of different storage devices. It has to offer the proportion and safety mechanism of
documents additionally.
Communication
The operating system manages the exchange of data and programs among different computers
connected over a network. This communication is accomplished using message passing and shared
memory.
There are four generations of operating systems. These can be described as follows −
The First Generation ( 1945 - 1955 ): Vacuum Tubes and Plugboards
Digital computers were not constructed until the second world war. Calculating engines with
mechanical relays were built at that time. However, the mechanical relays were very slow and were
later replaced with vacuum tubes. These machines were enormous but were still very slow.
These early computers were designed, built and maintained by a single group of people.
Programming languages were unknown and there were no operating systems so all the programming
was done in machine language. All the problems were simple numerical calculations.
By the 1950’s punch cards were introduced and this improved the computer system. Instead of using
plugboards, programs were written on cards and read into the system.
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
Program
A program is a piece of code which may be a single line or millions of lines. A computer program is
usually written by a computer programmer in a programming language. For example, here is a
simple program written in C programming language −
#include <stdio.h>
int main() {
printf("Hello, World! \n");
return 0;
}
A computer program is a collection of instructions that performs a specific task when executed by a
computer. When we compare a program with a process, we can conclude that a process is a
dynamic instance of a computer program.
A part of a computer program that performs a well-defined task is known as an algorithm. A
collection of computer programs, libraries and related data are referred to as a software.
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.
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.
7
CPU Scheduling Information
Process priority and other scheduling information which is required to schedule the process.
8
Memory management information
This includes the information of page table, memory limits, Segment table depending on
memory used by the operating system.
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.
The architecture of a PCB is completely dependent on Operating System and may contain different
information in different operating systems. Here is a simplified diagram of a PCB −
to a new process to execute its tasks. While a new process is running in the system, the previous process must
wait in a ready queue. The execution of the old process starts at that point where another process stopped it. It
defines the characteristics of a multitasking operating system in which multiple processes shared the same CPU
to perform multiple tasks without the need for additional processors in the system.
Following are the reasons that describe the need for context switching in the Operating system.
1. The switching of one process to another process is not directly in the system. A context
switching helps the operating system that switches between the multiple processes to use the
CPU's resource to accomplish its tasks and store its context. We can resume the service of the
process at the same point later. If we do not store the currently running process's data or
context, the stored data may be lost while switching between processes.
2. If a high priority process falls into the ready queue, the currently running process will be shut
down or stopped by a high priority process to complete its tasks in the system.
3. If any running process requires I/O resources in the system, the current process will be
switched by another process to use the CPUs. And when the I/O requirement is met, the old
process goes into a ready state to wait for its execution in the CPU. Context switching stores
the state of the process to resume its tasks in an operating system. Otherwise, the process
needs to restart its execution from the initials level.
4. If any interrupts occur while running a process in the operating system, the process status is
saved as registers using context switching. After resolving the interrupts, the process switches
from a wait state to a ready state to resume its execution at the same point later, where the
operating system interrupted occurs.
5. A context switching allows a single CPU to handle multiple process requests simultaneously
without the need for any additional processors.
1. Interrupts
2. Multitasking
3. Kernel/User switch
Interrupts: A CPU requests for the data to read from a disk, and if there are any interrupts, the
context switching automatic switches a part of the hardware that requires less time to handle the
interrupts.
Multitasking: A context switching is the characteristic of multitasking that allows the process to be
switched from the CPU so that another process can be run. When switching the process, the old state
is saved to resume the process's execution at the same point in the system.
Kernel/User Switch: It is used in the operating systems when switching between the user mode, and
the kernel/user mode is performed.
As we can see in the diagram, initially, the P1 process is running on the CPU to execute its task, and at
the same time, another process, P2, is in the ready state. If an error or interruption has occurred or
the process requires input/output, the P1 process switches its state from running to the waiting state.
Before changing the state of the process P1, context switching saves the context of the process P1 in
the form of registers and the program counter to the PCB1. After that, it loads the state of the P2
process from the ready state of the PCB2 to the running state.
The following steps are taken when switching Process P1 to Process 2:
1. First, thes context switching needs to save the state of process P1 in the form of the program
counter and the registers to the PCB (Program Counter Block), which is in the running state.
2. Now update PCB1 to process P1 and moves the process to the appropriate queue, such as the
ready queue, I/O queue and waiting queue.
3. After that, another process gets into the running state, or we can select a new process from the
ready state, which is to be executed, or the process has a high priority to execute its task.
4. Now, we have to update the PCB (Process Control Block) for the selected process P2. It
includes switching the process state from ready to running state or from another state like
blocked, exit, or suspend.
5. If the CPU already executes process P2, we need to get the status of process P2 to resume its
execution at the same time point where the system interrupt occurs.
Similarly, process P2 is switched off from the CPU so that the process P1 can resume execution. P1
process is reloaded from PCB1 to the running state to resume its task at the same point. Otherwise,
the information is lost, and when the process is executed again, it starts execution at the initial level.
Need of Thread:
o It takes far less time to create a new thread in an existing process than to create a new process.
o Threads can share the common data, they do not need to use Inter- Process communication.
o Context switching is faster when working with threads.
o It takes less time to terminate a thread than a process.
Types of Threads
In the operating system, there are two types of threads.
User-level thread
The operating system does not recognize the user-level thread. User threads can be easily
implemented and it is implemented by the user. If a user performs a user-level thread blocking
operation, the whole process is blocked. The kernel level thread does not know nothing about the
user level thread. The kernel-level thread manages user-level threads as if they are single-threaded
processes?examples: Java thread, POSIX threads, etc.
6.3M
212
Java Tricky Program 16 - Autoboxing, Inheritance and Overriding
1. The user threads can be easily implemented than the kernel thread.
2. User-level threads can be applied to such types of operating systems that do not support
threads at the kernel-level.
3. It is faster and efficient.
4. Context switch time is shorter than the kernel-level threads.
5. It does not require modifications of the operating system.
6. User-level threads representation is very simple. The register, PC, stack, and mini thread control
blocks are stored in the address space of the user-level process.
7. It is simple to create, switch, and synchronize threads without the intervention of the process.
1. User-level threads lack coordination between the thread and the kernel.
2. If a thread causes a page fault, the entire process is blocked.
Components of Threads
Any thread has the following components.
1. Program counter
2. Register set
3. Stack space
Benefits of Threads
o Enhanced throughput of the system: When the process is split into many threads, and each
thread is treated as a job, the number of jobs done in the unit time increases. That is why the
throughput of the system also increases.
o Effective Utilization of Multiprocessor system: When you have more than one thread in one
process, you can schedule more than one thread in more than one processor.
o Faster context switch: The context switching period between threads is less than the process
context switching. The process context switch means more overhead for the CPU.
o Responsiveness: When the process is split into several threads, and when a thread completes
its execution, that process can be responded to as soon as possible.
o Communication: Multiple-thread communication is simple because the threads share the
same address space, while in process, we adopt just a few exclusive communication strategies
for communication between two processes.
o Resource sharing: Resources can be shared between all threads within a process, such as
code, data, and files. Note: The stack and register cannot be shared between threads. There is a
stack and register for each thread.