operating system banner

Table of Contents hide

Introduction

The operating system is software that functions as an interface between a user and computer hardware. The main objective of an operating system is to make the computer system convenient to use and to use computer hardware in an efficient manner. The operating system performs basic tasks such as receiving keyboard input, processing instructions and sending output to the screen.

Functions of operating system

  • Memory management
  • Processor management
  • Device management
  • Storage management
  • Application management
  • User interface
  • Security

Types of operating system

Single user operating system :

This operating system only allows one user at a time.

E.g. MS-DOS, Windows 9X

Multi-user operating system:

This operating system allows several users to access the computer system simultaneously.

E.g. LINUX, UNIX, WINDOWS 2000/7

Multi-Tasking Operating System:

In multitasking operating system, multiple processes can be running simultaneously.

E.g.  LINUX, UNIX, WINDOWS 95. 

Batch processing operating system:

In batch processing operating system users do not directly interact with the computer. Each user prepares their work on an offline device like punch cards and submits it to the operator. To speed up processing, jobs with similar needs are grouped together and run in a group.

Time-sharing operating system:

Time-sharing is a technique that allows many people at different terminals to use a particular computer system at the same time. Time sharing or multitasking is the logical extension of multiprogramming.

Multiprocessing operating system :

A multiprocessing operating system is made up of several processes that share common physical memory. It provides higher computing power and speed. In this system, all the processes operate under a single operating system.

Distributed Operating System:

In the distributed operating system, several core processes are used to serve multiple real-time applications and multiple users. These processes can vary in size and function. These processors are called sites, computers, nodes, etc.

Real-time operating system :

In the real-time operating system, data is processed as it arrives without bubble delay, so the response time is much less than online processing.

Response time- The time taken by the system to respond to an entry and display the required updated information is called the response time

Embedded operating system :

An embedded operating system is an operating system that supports a machine. These are self-contained in the device ROM.

Tasks of operating system :

Process Management:

Word process means a program that is under execution. Process management is an important part of an operating system because it enables the activities of planning, monitoring and executing a process. A process requires certain systems such as processor time, files, I / O devices, etc. All of these tasks are handled by the operating system as a process manager.

Device or Input/Output Management:

The OS device management module coordinates and assigns different input and output devices such as printers, disk drives, etc.

The operating system monitors and tracks I / O requests, and sends commands to these devices to take action for data transmission from I / O devices. It depends on features such as speed, transfer unit, data representation, buffering and queuing.

Memory Management:

It takes care of main memory allocation and de-allocation to various processes. It is also used to find out which part of memory is currently being used and by whom.

Storage Management:

The main activities of an operating system with regard to secondary storage management are:

  • Manage the free space available on the secondary storage device.
  • Allocation of storage space when new files need to be written.
  • Scheduling the memory access request.

Operating System – Services

  • An operating system provides services to both the user and the programs.
  • OS provides an environment for running programs.
  • OS provides users with the necessary services to run the programs conveniently.

Here are some of the common services provided by an operating system-

  • Program execution
  • Input-output operation
  • File system installation
  • Resource allocation
  • Communication
  • Error detection
  • Protection

Click here to see full operating system services in detail.

Some important terms we need to know before you jump into tutorial-

Bootstrap:

A bootstrap is the first code executed when the computer system starts up. The entire operating system depends on the bootstrap to function properly when it loads the operating system.

It resides in the EPROM, ROM or other non-volatile memory of the computer.

Kernel:

  • Kernel is system software which is part of operating system.
  • It is a Boss program that resides in the central core of the computer operating system, it has complete control over everything in the system.
  • Kernel provides interface b/w application and hardware.
  • During Booting, the kernel is the first program loaded into memory.

Shell:

In computing, a shell is a user interface between User and Kernel. Typically, operating system shells use a command-line interface (CLI) or graphical user interface (GUI), depending on the role of the computer and its particular operation.

shell

Process Management

Process:

A program that is under execution is known as Process. The process is not the same as the program code but much more than that. A process is an “active” entity as opposed to a program that is considered a “passive” entity. The attributes held by the process include the state of hardware, memory, processor, etc.

Process can be seen under task manager.

Process State

process-state

A process being an active entity changes state as it runs. A process can be one of the following states:

  1. New: The process is being created here.
  2. Ready: Process waiting for CPU and checks for all the requirements
  3. Waiting: Process waiting for an event
  4. Running: Instructions being executed. The running state can also be interrupted by the user.
  5. Terminated: The process has finished execution

Process control block (PCB)

A process control block contains information about the process like quantum, registers,  priority, etc. During the creation of a process, the operating system performs various operations. It assigns a process identification number (PID) to each process to identify processes. As the OS supports multiprogramming, it must keep track of all the processes. For this task, the process control block (PCB) is used to track the execution status of the process.

The information associated with each processor are:

  1. Process State- The state, can be any one of the 5 states shown in the process state diagram, such as new, ready, waiting and terminated.
  2. Program counter- It indicates the address of the next instruction executed for this process.
  3. CPU registers- Resistors vary greatly depending on the number and type and also depending on the architecture of the computer. They include an index register, accumulators, a stack pointer, and general-purpose registers.
  4. CPU scheduling information- It includes information like process priority pointers to scheduling queues and other scheduling parameters.
  5. Memory management information- This information contains the value of the base register and limit register page tables, the segment tables. These tables are used to refer to the correct memory locations for the process to use.
  6. Accounting information- This includes processor time and real time used, delays, account number, etc.
  7. I/O status information- This includes the list of I / O devices allocated to the process, the list of open files, etc.

Thread

A thread is a flow of execution through process code, with its own program counter that keeps track of the instruction to be executed next, system registers that contain its current work variables, and a stack that contains the execution history.

A thread is also called a lightweight process. Threads provide a way to improve the performance of the application by parallelism. Each thread belongs to exactly one process, and no thread exists outside of the process. Each thread represents a separate flow of control.

Difference between process and thread

Process Thread
The process is heavyweight or resource-intensive. The thread is light weight , taking less resources than a process.
Process switching requires interaction with the operating system. Thread switching does not need to interact with the operating system.
In multiple processing environments, each process runs the same code but has its own memory and file resources. All threads can share the same set of open files, child processes.
If a process is blocked, no other process can run until the first process is unblocked. While one thread is blocked and pending, a second thread of the same task can run.
Several processes without using threads use more resources. Several threaded processes use fewer resources.
In several processes, each process operates independently of the others. A thread can read, write, or modify data from another thread.

Advantages of Thread:

  • Threads minimize the time it takes to switch contexts.
  • Efficient communication.
  • The use of threads provides concurrency within a process.
  • Threads allow the use of multiprocessor architectures on a larger scale and more efficiency.
  • It is more economical to create and change the context.

Types of Thread

There are two types of thread-

  1. User Level Threads
  2. Kernel Level Threads

User Level Thread

User-level threads are implemented in user-level libraries rather than through system calls, so changing threads does not need to call the operating system, and both are of interest to the kernel. In fact, the kernel knows nothing about user-level threads and handles them as if they were carrying single-threaded processes. The three main thread libraries are POSIX threads, Win32 threads, and Java threads.

Advantages:

  • A user-level thread can run on any operating system.
  • Thread switching doesn’t require kernel-mode privileges.
  • Scheduling can be application specific in user level threads.
  • User-level threads are quick to create and maintain.

Disadvantages:

  • In a typical operating system, most system calls are blocking.
  • Multithreaded applications cannot take advantage of multiprocessing.

Kernel Level Threads

In this method, the kernel knows and manages the threads. No execution system is necessary in this case. The kernel have a thread table that keeps track of all threads in the system. In addition, the kernel also manages the traditional process table to keep track of the processes. The operating system kernel provides a system call to create and manage threads.

Advantages:

Kernel-level threads are especially useful for applications that blocks frequently.

Disadvantages:

Kernel threads are generally slower to create and manage than user threads.

Multithreading Models

Some operating systems provide both user-level threading and kernel-level threading functionality.

The way they can interact in multi-threaded processes is as follows-

  1. One to one Model
  2. Many to one Model
  3. Many to many Model

One to one Model

The one-to-one model creates a distinct kernel thread to handle each user thread. This model restrict the number of threads that can be created.

The diagram below shows the one-to-one thread model in which 1 user-level thread multiplex with 1 kernel-level thread.

ONE-TO-ONE-THREAD

Many to one Model

 In the many-to-one model, many user-level threads are all mapped to a single kernel thread. In this model, thread management is handled by the thread library in user space, which is efficient in nature.

The diagram below shows the many-to-one thread model in which 4 user-level threads multiplex with 1 kernel-level thread.

many-to-one-thread

Many to many Model

Many to many model multiplexers any number of users threads onto an equal or smaller number of kernel threads.

The diagram below shows the many-to-many thread model in which 4 user-level threads multiplex with 3 kernel-level threads.

MANY-TO-MANY-THREAD

Difference between user-level and kernel-level thread –

User-Level Thread Kernel-Level Thread
The user thread is implemented by the user. Kernel thread are implemented by the operating system.
User-level threads are faster to create and manage. Kernel-level threads are slower to create and manage.
Context switch does not require any hardware support. In this hardware support is required.
User-level thread is generic and can run on any operating system. Kernel-level thread is specific to the operating system.

Schedulers

Schedulers are special system software that manages process scheduling in various ways. Their main task is to select the jobs to be submitted in the system and decide on the process to be executed.

Long -Term Scheduler

  • It is also called a job scheduler.
  • A long-term scheduler determines which programs are allowed into the system for processing. It selects the processes from the queue and then loads them into memory for execution.
  • It also controls the degree of multiprogramming.
  • If the degree of multiprogramming is stable, then the average process creation rate should equal the average departure rate of processes leaving the system.

Medium-Term Scheduler

  • In this process, the swapping scheme is used to reduce the degree of multiprogramming.
  • The process is swapped out and can be swapped in later if needed.

Short-Term Scheduler

  • It is also called CPU scheduler.
  • It is faster than long-term schedulers.
  • It is also known as dispatchers, and make the decision of which process to execute next.

Dispatcher

A dispatcher is a special program that plays a role after the scheduler. When the scheduler completes its job of selecting a process, it is the dispatcher which brings that process to the desired state / queue. The dispatcher is the module that gives process control over the CPU after it has been selected by the short term scheduler. This function involves the following:

  • Change of context.
  • Switch to user mode.
  • Jump to the appropriate location in the user program to restart that program.
  • The time it takes for the dispatcher to stop one process and start another is called dispatch latency.

Scheduling Algorithms

There are six popular process scheduling algorithms which we are going to discuss −

  1. First-Come First-Serve Scheduling (FCFS)
  2. Shortest Job First Scheduling (SJN)
  3. Priority Scheduling
  4. Round Robin Scheduling (RR)
  5. Multiple-Level Queue Scheduling
  6. Multiple-Level Feedback Queue Scheduling

Before we discuss different times, we need to know the different time in relation to a process.

Arrival Time: Time when the process arrives in the ready queue.

Completion Time: Time at which the process completes its execution.

Burst Time: time required by a process for the execution of the CPU.

Turn Around Time:  Time difference between Completion time and arrival time.

Turnaround time = time of completion – time of arrival

Waiting time (W.T): Time difference between the Turn around time and the burst time.

Waiting time = Turn around time – burst time

First-Come First-Serve Scheduling (FCFS)

The simplest of all scheduling algorithms is first come, first served. It involves that, the process that requests the CPU first, gets the CPU allocated to it first. It can be easily implemented using  a  FIFO Queue.

Characteristics of FCFS method:

  • It is easy to implement using a FIFO Queue.
  • Average waiting time is usually high in this case.

Shortest Job First Scheduling (SJF)

The idea behind the SJF algorithm is to pick the small and fastest job that needs to be done, eliminate it first, and then choose the next fastest smallest job to do next.

Characteristics of SJF method:

  • The processes with the shortest burst time are scheduled first.
  • It is the best approach to minimize waiting time.
  • If two processes have the same bust time, FCFS is used to break the tie.
  • Average waiting time of SJF is less in comparison to FCFS.
  • This is a non preemptive scheduling algorithm.

Priority Scheduling:

Priority scheduling is a more general case of SJF, where each job is assigned a priority and the job with the highest priority is scheduled first.

Characteristics of Priority Scheduling:

  • Equal priority processes are scheduled in in FCFS order.
  • When a process arrives in the ready queue, its priority is compared to the priority of the running process.
  • The greater the CPU burst, the lower the priority and vice versa.
  • Priority Scheduling can be either preemptive  or non preemptive.

Round Robin Scheduling (RR)

In this each process is assigned a fixed time (Time Quantum / Time Slice) cyclically, specially designed for the timeshare system. Duration of time slice may range between 10 millisecond to about 100 milliseconds.  The ready queue is treated as circular queue. The processor scheduler goes around the queue, allocating the CPU to each process for a time interval of up to one times quantum.

Characteristics of RR:

  • It is basically a preemptive scheduling algorithm design for time-sharing systems.
  • Context switching is used to save states of preemptive processes.
  • The ready queue in this case is a FIFO queue.

Multiple-Level Queue Scheduling

When processes can be easily categorized, several distinct queues can be established. The processes belonging to each group have a specified priority. Each queue can have its own scheduling algorithm like FCFS or RR.

Characteristics of Multiple-Level Queue Scheduling :

  • The process in a queue with a lower priority will not run until all the processes in a queue with a higher priority are executed.
  • In this process are permanently assigned to a queue upon entry into the system.
  • Each queue can have its own scheduling algorithms.

Multiple-Level Feedback Queue Scheduling

It allows a process to move between queues. The idea is to separate processes with different CPU burst characteristics. If a process uses too much CPU time it will be moved to a lower priority queue. Similarly, a process that waits too long in a lower Priority Queue may be moved to a higher- priority queue.

Multi-label feedback queue scheduling is defined by the following parameters.

  • The number of queues.
  • The scheduling algorithm for each queue.
  • The method used to determine when to upgrade a process to a higher priority queue.
  • The method used to determine when to demote a process to a lower priority queue.

Deadlock

Deadlock is a situation in which a set of processes is blocked because each process contains a resource and is waiting for another resource acquired by another process.

A deadlock situation can arise if the following four condition hold simultaneously in a system.

Mutual exclusion: At least one resource must be kept in an unshareable mode; that is, only one process can use the resource at a time. If another process requests this resource, the requesting process should be delayed until the resource is released.

Hold and wait: A process must contain at least one resource and wait to acquire additional resources currently held by other processes.

No preemption: The resource cannot be preempted; that is, a resource can only be released voluntarily by the process that owns it, once that process has completed its task.

Circular wait: There must be a set {P1, P2, …. Pn} of pending processes such that P0 waits for a resource which is owned by P2, …, Pn-1 waits for a resource which is owned by Pn and Pn waits a resource owned by P0.

Deadlock Detection

A deadlock can be detected by a resource scheduler because it keeps track of all the resources allocated to different processes. Once a blockage is detected, it can be resolved using the following methods:

  • All the processes involved in the deadlock are removed. This is not a good approach because all the progress made by the processes is destroyed.
  • Resources can be preempted from some processes and provided to others until the deadlock is resolved.

Deadlock Prevention

It is very important to prevent a deadlock before it happens. Thus, the system checks each transaction before it is executed to ensure that it does not lead to a deadlock. If there is even a slight chance that a transaction could lead to a deadlock in the future, it is never allowed to execute.

Deadlock Avoidance

It is better to avoid a deadlock than to take action after the deadlock. A deadlock avoidance algorithm dynamically checks the state of resource allocation to ensure that there can never be a circular wait condition. The resource allocation state is defined by the number of available and allocated resources and the maximum demands of the processes.

Deadlock avoidance can be done with Banker’s Algorithm.

Banker’s Algorithm

It is a resource allocation and deadlock avoidance algorithm that tests all the requests made by the processes for the resources, it checks the safe state, if after permitting the request, the system remains in the safe state it allows the request and if there is no safe state it does not allow the request made by the process.

Inputs to the banker’s algorithm:

  • Max resource requirement for each process.
  • Max free resources available in the system.
  • Resources currently allocated by each process.

Memory Management

Main Memory: Main memory refers to physical memory which is the internal memory of a computer. The term Main is used to distinguish it from external mass storage devices such as disk drives. Main memory is also known as RAM. The computer can only modify data in main memory. So every program we run and every file we access needs to be copied from the storage device to main memory.

Logical addresses: An address generated by the CPU is commonly referred to as a logical address. The logical address space can also be referred to as a virtual address space. As a user, we can see the logical address.

Physical addresses: The address seen by the memory unit i.e the one loaded into the memory address register of the memory is called physical address.

Memory management unit(MMU): The memory management unit is a hardware component responsible for managing access to the memory requested by the central unit (CPU). Its function includes the translation of virtual addresses into two physical addresses.

The MMU uses a base register also known as a relocation register. The value of this register is added to the logical address, to obtain the physical addresses. This means that, depending on the value of the relocation register, the logical addresses are relocated to their actual physical location.

Memory management techniques

Various memory management techniques exist, such as partitioning memory into different fixed-size partitions or dynamic memory allocation. In addition, overlays and swapping are also used as part of memory management. But the two most important and most used strategies are pagination and segmentation.

Paging

If a big process is executed, then only some part of the process is stored in RAM and the rest in virtual memory which is secondary memory. During paging, physical memory (main memory) is divided into fixed-size blocks called frames, and logical memory (virtual memory) is divided into blocks called pages. These pages are stored in the main memory frames and we directly access the frames.

When the requested page is not present in memory, we call it page fault.

Page Fault

A page fault occurs when a program tries to access a block of memory that is not stored in physical memory or RAM. The fault informs the operating system to locate the data in virtual memory and then transfer it from the storage device, such as a hard drive or SSD, to the system RAM.

The page fault can be handled by the operating system by –

Step 1: First, the operating system looks at the CPU to decide if the requested page is invalid or not present in the frame.

If the page is not valid, the request is aborted or if it is not in the frame, then it is brought into the frame by another process.

Step 2: To load the page first, it gets the empty frame from the secondary page.

Stem 3: Swap page into the frame via a disk operation.

Step 4: Reset the page table to indicate the page.

Step 5: Then restart the instruction that caused the page fault and provide the requested page.

Note :- What, if there is no free frame present in the secondary memory?

=> If there is no free frame present, then the page replacement technique will be followed.

Page Replacement

In this system will find some pages that are rarely used and swap them out, if the system does not find a page to swap then the same page can be brought in memory multiple times whenever needed.

Some of the commonly used page replacement algorithms are:

  1. FIFO – This is the simplest page replacement algorithm that associates each page with the time when that page was brought in memory. For page replacement, the oldest page is selected. The page that is paged in first is also the one that is paged out first. In this case, the page fault rate increases as the number of available frames increases.
  2. Optimal Page Replacement – This algorithm replaces the page that will not be used for the longest time. This is called optimal because it ensures the lowest possible page-fault rate for a fixed number of frames. But this is impractical as it requires knowledge of how to use the pages in advance, which is not always possible.
  3. LRU Page Replacement – LRU stands for Least Recently Used hello hello. This algorithm as its name replaces the least recently used page. For this, it keeps track of the time of the last use of each page. It is better than FIFO but has a higher fault rate than the Optima page replacement algorithm.

Segmentation

Segmentation is a memory management technique that supports the user’s view of memory. This technique of dividing a computer’s primary memory into sections called segments. Every segment has a name and a length. The address specifies the segment name and offsets in the segment. To simplify implementation, segments are numbered and referenced by a segment number rather than a segment name.

Difference between compiler and interpreter

Compiler Interpreter
A program that translates a high-level language program into machine language is called a compiler. A program that translates statements of high-level language into machine language is called Interpreter.
The entire program is translated as a whole into machine code. Translate programs one statement at a time.
It displays all errors and warning at a time and without fixing all errors program cannot be executed. It reads only one statement, so the interpreter displays one error at a time and you must fix the error to execute the next statement.
It needs more memory. Compared to the compiler, an interpreter needs less memory.
The overall execution time is comparatively faster. The overall execution time is slower.
Programming languages like C, C++ uses compiler. Programming languages like Python, Ruby uses Interpreter.

Thrashing

As the number of page faults increases in RAM, the processor gets busy in moving pages from secondary memory to RAM, during this process, the CPU utilization (performance) decreases, and this decreasing state is called thrashing. Therefore, it can be said that the thrashing is caused due to poor paging algorithms.

thrashing

In the given graph, the degree of multiprogramming means the processes brought to RAM for processing.

Fragmentation

When processes are loaded and removed from memory, free memory space is broken into small pieces. Sometimes, this process can no longer be allocated to memory due to its small memory block size, and the memory block remains unused. This problem is known as fragmentation. Fragmentation causes slow access time.

Fragmentation causes slow access time.

Fragmentation is of two types –

  1. Internal fragmentation – The memory block allocated to the process is larger. Part of the memory remains unused because it cannot be used by another process.
  2. External fragmentation – The total memory space is sufficient to satisfy a request or to reside a process there, but it is not contiguous and therefore cannot be used.

Semaphore

Semaphore was proposed by Edsger Dijkstra. It is a significant technique for managing concurrent processes for complex mutual exclusion problems.

Semaphores are of two types −

Counting semaphore

This type of semaphore uses a count that allows the task to be acquired or released many times. If the initial count equals 0, the counting semaphore should be created in the unavailable state.

However, if the count is greater then 0, the semaphore is created in the available state and the number of tokens it has is equal to its count.

Binary semaphore-

Binary semaphores are quite similar to counting semaphores, but their value is limited to 0 and 1. In this type of semaphore, the wait operation only works if semaphore = 1, and the signal operation succeeds when semaphore = 0. It is easy to implement as compared to counting semaphores.

Wait and Signal Operations in Semaphores

Both of these operations are used to implement process synchronization. The goal of this semaphore operation is to get mutual exclusion.

Wait for Operation-

This type of semaphore operation helps us control the entry of a task into the critical section. However, if the value of wait is positive, the value of the wait X argument is decremented. If the value is negative or zero, no operation is performed. It is also called the P (S) operation. Once the value of the semaphore is reduced, which becomes negative, command is held until the required conditions are satisfied.

Signal operation-

This operation is used to control the output of a task from a critical section. This helps to increase the value of the argument by 1, which is denoted by V (S).

Leave a Reply

Your email address will not be published. Required fields are marked *