0% found this document useful (0 votes)
65 views69 pages

OS Unit-1 Material

Operating system
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
65 views69 pages

OS Unit-1 Material

Operating system
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 69

Module 1

INTRODUCTION TO OPERATING SYSTEM AND PROCESS


MANAGEMENT
Introduction: Definition, Operating System Structure and Services, System
Calls.
Process Management: Process Scheduling, Process Control Block, Inter Process
Communication, Threads, Multithreading Models, CPU Scheduling Criteria,
Scheduling Algorithms, Multiprocessor Scheduling.
Operating Systems
Overview
1. Introduction
1.1. Operating System – Definition:
Operating System is a program that acts as intermediary between user programs and computer
hardware.
{OR}
An operating system is a program that manages the computer hardware. It also provides a basis for
application programs and acts as an intermediary between the computer user and the computer
hardware.
Examples:
Microsoft Windows, Linux, iOS, Mac OS, Andriod, etc…,
1.2. Computer System Structure:
Computer system can be divided into four components, as shown in below figure:

1. Hardware – provides basic computing resources CPU, memory, I/O devices.


2. Operating system – Controls and coordinates use of hardware among various applications and
users.
3. Application programs –Application programs are performing specific task. Examples are word
processors, compilers, web browsers, database systems, video games etc...,.
4. Users of the computer.
1.3. Operating system goals:
1. Execute user programs and make solving user problems easier.
2. Make the computer system convenient to use. (Ease of use)
3. Use the computer hardware in an efficient manner. (Resource utilization)
2. OPERATING SYSTEM FUNCTIONS
Operating systems have many functions:
1. User interface
Provides an interface for computer interaction.
Control of inputs and outputs.
Without it, most computers would be too difficult for the average person to use.
2. Management of hardware and peripherals
The operating system is responsible for controlling all the devices connected to the
computer. It tells them how to interact and operate correctly.
Device drivers are used to manage these connections.
3. Processor management
The operating system manages the CPU.
When software is opened, the OS finds it and loads it into memory (RAM).
The CPU can then be instructed to execute the program.
The operating system will manage the sharing of processor time.
4. Interrupt and error handling
Several programs can be stored in RAM at the same time, but the processor can only process
one at a time.
Through the use of regular interruption signals, the OS can prioritise the requests made to the
processor.
This gives the illusion that the CPU is actually dealing with more than one program or task at
once (multitasking).
Errors are spotted and usually alert the user.
5. Memory management
The operating system is responsible for transferring programs to and from memory.
It keeps track of memory usage, and decides how much should be given to each program.
The OS also decides what happens if there is not enough memory.
6. File management
A file system is created to organise files and directories.
This gives programs a consistent way to store and retrieve data.
The OS is also responsible for the naming, sorting, deleting, moving and copying of files (at
the request of the user).
Look-up tables are used to relate file names to storage locations.
7. Security
The operating system is responsible for the creation and application of user accounts and
passwords.
An OS also comes with many utility programs, including firewalls.

3. OPERATING SYSTEMS
OPERATIONS
3.1. Operating systems 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.
An interrupt is a signal emitted by hardware or software when a process or an event needs
immediate attention.
There are two types of interrupts, they are:
1. Hardware Interrupts
2. Software Interrupts (also called trap or exception )
Hardware interrupt is an interrupt generated from an external device or hardware. For
example, pressing a keyboard key or moving mouse that causes the processor to read the keystroke
or mouse position
A trap (or an exception or software interrupt) is a software-generated interrupt caused
either by an error. For example, division by zero or invalid memory access.
The interrupt-driven nature of an operating system defines that system’s general structure.
For each type of interruption, separate segments of code in the operating system determine which
action to take. An interrupt service routine is provided that is responsible for dealing with the
interrupt.
The operating systems share the hardware and software resources of the computer system to
user programs. If there is an error in a user program could cause problems only for the one program
or many processes could be affected. For example, if a process gets stuck in an infinite loop, this
loop could prevent the correct operation of many other processes.
To protect these types of errors, operating system will run in dual-mode operation.
1. Dual-Mode Operation
Dual-mode operation allows OS to protect itself and other system components. Dual-mode
operation consists of two modes:
1. User Mode
2. Kernel Mode (also called Supervisor Mode, System Mode, Or Privileged Mode)
A mode bit is added to the hardware of the computer 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 on behalf of the operating
system and one that is executed on behalf of the user.
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 state of the mode bit to 0). Thus, whenever the

operating system gains control of the computer, it is in kernel mode. The system always switches to
user mode (by setting the mode bit to 1) before passing control to a user program. This is shown in
below figure.

The dual mode of operation protects the operating system from errant users. We accomplish
this protection by specifying some machine instructions that may cause harm as privileged
instructions. The instruction to switch to kernel mode, I/O control, timer management, and interrupt
management are few examples of a privileged instruction.
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.
Life cycle of instruction:
Initial control resides in 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.
2. Timer
Timer prevents a user program from running too long.
A timer can be set to interrupt the computer after a specified period. The period may be fixed
(for example, 1/60 second) or variable (for example, from 1 millisecond to 1 second).
A variable timer is generally implemented by a fixed-rate clock and a counter. The operating
system sets the counter. Every time the clock ticks, the counter is decremented. When the counter
reaches 0, an interrupt occurs.
3.2. Differences between hardware interrupt and software interrupt:

S.No. Hardware Interrupt Software Interrupt

Hardware interrupt is an interrupt


Software interrupt is the interrupt that
1 generated from an external device or
is caused either by an error.
hardware.

It does not increment the program


2 It increment the program counter.
counter.

It has lowest priority than software It has highest priority among all
3
interrupts interrupts.

Hardware interrupt is triggered by Software interrupt is triggered by


external hardware and is considered software and considered one of the
4
one of the ways to communicate with ways to communicate with kernel or to
the outside peripherals, hardware. trigger system calls.

5 It is an asynchronous event. It is synchronous event.

Keystrokes and mouse movements are All system calls are examples of
6
examples of hardware interrupt. software interrupts
4. Computing environments
1. Traditional Computing:
As computing matures, the lines separating many of the traditional computing environments
are blurring.
Office environment
A few years ago, this environment consisted of PCs connected to a network, with servers
providing file and print services. Terminals attached to mainframes providing batch and timesharing.
Now a day’s the environment is web based computing, Companies establish portals, which
provide web accessibility to their internal servers. Network computers are the terminals.
Home networks
At home, most users had a single computer with an internet connection to the office. Some
homes even have firewalls to protect their networks from security breaches
Time-sharing systems
Time-sharing systems used a timer and scheduling algorithms to cycle processes rapidly
through the CPU, giving each user a share of the resources.
2. Client–Server Computing:
Personal Computers (Clients) connected to Servers (Centralized Systems). Server systems
satisfy requests generated by client systems. This is called a client–server system, has the general
structure depicted in below Figure.

Types of Server Systems


Server systems can be broadly categorized into 2 types:
1. Compute-Server System
2. File-Server System
1. Compute-Server System:
In compute-server system, a client can send a request to perform an action; the server
executes the action and sends back results to the client. For example read data.
These are the database systems.
2. File-Server System:
In file-server system, clients can create, update, read, and delete files. An example of
such a system is a Web server that delivers files to clients running Web browsers.
3. Peer-to-Peer Computing:
In this model, clients and servers are not distinguished from one another; instead, all nodes
within the system are considered peers, and each may act as either a client or a server, depending on
whether it is requesting or providing a service.
To participate in a peer-to-peer system, a node must first join the network of peers. Once a
node has joined the network, it can begin providing services to other nodes and requesting services
from other nodes in the network.
The services availability is determined one of two ways:
1. Registers its service with central lookup service on network, or
2. Broadcast request for service and respond to requests for service via discovery
protocol
Examples peer-to-peer computing are Napster and Gnutella.
4. Web-Based Computing:
The Web has become ubiquitous. PCs, PDAs, cell phones are most prevalent access devices.
More devices becoming networked to allow web access.
New category of devices to manage web traffic among similar servers: load balancers
Use of operating systems like Windows 95, client-side, have evolved into Linux and
Windows XP, which can be clients and servers

5. Open-Source Operating Systems


5.1. Open-Source Operating System Definition:
 Open-source operating systems are available in source-code format rather than as compiled binary
code.
 Examples: Linux, BSD UNIX, Solaris, etc.

5.2. Benefits of Open-Source Operating System:


1. Programmers, who contribute to the code by helping to debug it, analyze it, provide support,
and suggest changes.
2. With the source code in hand, a student can modify the operating system and then compile
and run the code to try out those changes, which is another excellent learning tool.
3. Open-source code is more secure than closed-source code.
4. Companies that earn revenue from selling their programs tend to be hesitant to open-source
their code.
5.3. History:
Richard Stallman in 1983 started the GNU project to create a free, open-source UNIX
compatible operating system.
In 1985, he published the GNU Manifesto, which argues that all software should be free and
open-sourced.
He also formed the Free Software Foundation (FSF) with the goal of encouraging the free
exchange of software source code and the free use of that software.
Rather than copyright its software, the FSF “copylefts” the software to encourage sharing and
improvement.
The GNU General Public License (GPL) codifies copylefting and is common license under
which free software is released.
5.4. Linux:
As an example of an open-source operating system, consider GNU/Linux.
The GNU project produced many UNIX-compatible tools, including compilers, editors, and
utilities, but never released a kernel.
In 1991, a student in Finland, Linus Torvalds, released a rudimentary UNIX-like kernel using
the GNU compilers and tools and invited contributions worldwide.
The advent of the Internet meant that anyone interested could download the source code,
modify it, and submit changes to Torvalds.
Releasing updates once a week allowed this so-called Linux operating system to grow
rapidly, enhanced by several thousand programmers.
5.5. BSD UNIX:
BSD UNIX has a longer and more complicated history than Linux.
It started in 1978 as a derivative of AT&T’s UNIX.
Releases from the University of California at Berkeley came in source and binary form, but
they were not open-source because a license from AT&T was required.
BSDUNIX’s development was slowed by a lawsuit by AT&T, but eventually a fully
functional open-source version, 4.4BSD-lite, was released in 1994.
5.6. Solaris:
Solaris is the commercial UNIX-based operating system of Sun Microsystems.
Originally, Sun’s SunOS operating system was based on BSD UNIX.
Sun moved to AT&T’s System V UNIX as its base in 1991.
In 2005, Sun open-sourced some of the Solaris code, and over time, the company has added
more and more to that open-source code base.
Unfortunately, not all of Solaris is open-sourced, because some of the code is still owned by
AT&T and other companies.
However, Solaris can be compiled from the open source and linked with binaries of the
close-sourced components, so it can still be explored, modified, compiled, and tested.
5.7. Differences between Open Source Software and Free Source Software:
S.No. Open Source Software Free Source Software

Freely available and Source also available (can be


1 Freely available but copyrighted.
edited to make your own copyright)

2 It has distribution of License. Freedom to run program for any purpose.

All Open Source Software under free software But all free software does not come under
3
terminology. open source terminology.

4 It is good for your business. It is good for morality of society.

5.8. Differences between Open Source Software and Closed Source Software:
S.No. Open Source Software Closed Source Software

Open source software refers to the computer Closed source software refers to the computer
1 software which source is open means the software which source code is closes means
general public can access and use. public is not given access to the source code.

This code can be modified by other users and


The only individual or organization who has
2 organizations means that the source code is
created the software can only modify the code.
available for anyone to look at.

3 The price of open source software is very less. The price of closed source software is high.

Programmers freely provide improvement for Programmers are hired by the software
4
recognition if their improvement is accepted. firm/organization to improve the software.

Open software can be installed into any Closed software needs have a valid license before
5
computer. installation into any computer.

6 Open source software fails fast and fix faster. Closed source software has no room for failure.

In opened source software no one is In closed source software the vendor is


7
responsible for the software. responsible if anything happened to software.

Examples of Open-source Operating Examples of Closed-source Operating System:


8
System: Linux, BSD UNIX, Solaris, etc. Microsoft Windows etc.

Examples of Open-source software are


Examples of Closed-source software are Skype,
Firefox, OpenOffice, Gimp, Alfresco,
Google earth, Java, Adobe Flash, Virtual Box,
9 Android, Zimbra, Thunderbird, MySQL,
Adobe Reader, Microsoft office, , WinRAR, mac
Mailman, Moodle, TeX, Samba, Perl, PHP,
OS, Adobe Flash Player etc.0
KDE etc.
6. Operating System
Services
6.1. Operating System Services:
Operating System provides certain services to programs and to the users of those programs.
Below Figure shows one view of the various operating-system services and how they interrelate.

1. User interface:
Almost all operating systems have a user interface (UI). This interface can take several
forms.
1. Command-line interface (CLI), which uses text commands and a method for entering
them.
2. Batch interface, in which commands and directives to control those commands are
entered into files, and those files are executed.
3. Graphical user interface (GUI) is used. Here, the interface is a window system with a
pointing device to direct I/O, choose from menus, and make selections and a keyboard to
enter text.

Some systems provide two or all three of these variations.


2. Program execution:
The system must be able to load a program into memory and to run that program. The
program must be able to end its execution, either normally or abnormally (indicating error).
3. I/O operations:
A running program may require I/O, which may involve a file or an I/O device. For
efficiency and protection, users usually cannot control I/O devices directly. Therefore, the operating
system must provide a means to do I/O.
4. File-system manipulation:
Programs need to read and write files and directories. They also need to create and delete
them by name, search for a given file, and list file information.
Finally, some programs include permissions management to allow or deny access to files or
directories.
Many operating systems provide a variety of file systems, sometimes to allow personal
choice, and sometimes to provide specific features or performance characteristics.
5. Communications:
Let consider, two or more processes are executing on a same computer system or different
computer systems connected together by a computer network. If one process needs to exchange
information with another process, this communication could be implemented by operating system
via shared memory or through message passing.
6. Error detection:
The operating system needs to be constantly aware of possible errors. Errors may occur:
1. In the CPU and memory hardware (such as a memory error or a power failure),
2. 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
3. In the user program (such as an arithmetic overflow, an attempt to access an illegal
memory location, or a too-great use of CPU time).
For each type of error, the operating system should take the appropriate action to ensure
correct and consistent computing.
7. Resource allocation:
When there are multiple users or multiple jobs running at the same time, resources must be
allocated to each of them. Many different types of resources are managed by the operating system,
such as CPU cycles, main memory, file storage, I/O devices, printers, modems, USB storage drives,
and other peripheral devices.
8. Accounting:
We want to keep track of which users use how much and what kinds of computer resources.
This record keeping may be used for accounting or accumulating usage statistics.
9. Protection and security:
Protection – any mechanism for controlling access of processes or users to resources defined by
the OS.
Security – defines of the system against internal and external attacks.
If a system is to be protected and secure, precautions must be instituted throughout it.
7. User and Operating-System Interface
7.1. User and Operating System Interface:
Here, we discuss two fundamental user and operating system interfaces:
1. Command Interpreter or Command Line Interface(CLI) or Character User Interface(CUI):
It allows users to directly enter commands to be performed by the operating system
In some systems command interpreters are known as shells. For example on UNIX and
Linux systems has different shells such as Bourne shell, C shell, Bourne-Again shell, Korn
shell, and others. User may choose any one.
Command interpreter primarily fetches a command from user and executes it. Sometimes
commands built-in, sometimes just names of programs.
Example of CLI based Operating Systems: MS DOS, Unix, etc.
Advantages:
1. It uses less memory compare to GUI.
2. It is less expensive.
Disadvantages:
1. In CLI, only one task can do at a time.
2. Only supports the usage of keyboard.
3. User must remember the commands to use the system.
2. Graphical User Interfaces:
It allows users to interface with the operating system via a graphical user interface, or GUI.
It Provides User-friendly desktop metaphor interface - The user moves the mouse to position
its pointer on images, or icons, on the screen (the desktop) that represent programs, files,
directories, and system functions.
Depending on the mouse pointer’s location, clicking a button on the mouse can invoke a
program, select a file or directory—known as a folder—or pull down a menu that contains
commands.
Various mouse buttons over objects in the interface cause various actions.
Example of GUI based Operating Systems:
o Microsoft Windows, macOS, Ubuntu Unity, and GNOME Shell for desktops.
o Android, Apple’s iOS, Black Berry OS, Windows 10 Mobile, Palm OS-WebOS, and
Firefox OS for smart phones.
Advantages:
1. Easy to use.
2. More than one task can do at a time (Multiprocessing).
Disadvantages:
1. Uses more memory compare to CLI.
2. Development of OS is more complex.
3. More expensive.
7.2. Differences between CUI and GUI

S.No. CUI (Character User Interface) GUI (Graphical User Interface)


The user interacts with the computer using The user interacts with the system using
1
commands like text. Graphics like icons, images.
2 Difficult to use. Easy to use.
3 It has high speed. It has a low speed.
4 It has a low memory requirement. It has a high memory requirement.
5 Keyboard is used typing commands. Keyboard and Mouse are used to navigate.

Note:
Many systems now include both CLI and GUI interfaces:
1. Microsoft Windows is GUI with CLI “command” shell.
2. Apple Mac OS X as “Aqua” GUI interface with UNIX kernel underneath and shells available.
3. Solaris is CLI with optional GUI interfaces (Java Desktop, KDE).

8. Systems calls
8.1. System Calls:
Definition:
System calls provide an interface to the services provided by an operating system
Typically system calls written in a high-level language (C or C++).
Example:
o Let consider, copy one file to another file. The sequence of system calls invoked in this
example is shown in below figure:
Why use APIs rather than system calls?
The developers design system calls using Application Program Interface (API) rather than direct
development.
Three most common APIs are Win32 API for Windows, POSIX API for POSIX-based systems
(including virtually all versions of UNIX, Linux, and Mac OS X), and Java API for the Java
virtual machine (JVM)
API Example:

System Call Implementation:


 Typically, a number associated with each system call.
 System-call interface maintains a table indexed according to these Numbers
 The system call interface invokes intended system call in OS kernel and returns status of the
system call and any return values
 The caller need know nothing about how the system call is implemented
 Just needs to obey API and understand what OS will do as a result call.
 Most details of OS interface hidden from programmer by API and managed by run-time support
library (set of functions built into libraries included with compiler).
 The below figure shows API – System call – OS Relationship.

System Call Parameter Passing


 Often, more information is required than simply identity of desired system call.
 Exact type and amount of information vary according to OS and call
 Three general methods used to pass parameters to the OS
 Simplest approach is to pass the parameters in registers.
 In some cases, may be more parameters than registers, parameters stored in a block, or table, in
memory, and address of block passed as a parameter in a register.
 This approach taken by Linux and
Solaris.
 Parameters are placed, or pushed onto the stack by the program and popped off the stack by the
operating system.
 Block and stack methods do not limit the number or length of parameters being passed.
 Parameter Passing via Table is shown in below figure:
Types of System Calls
Types of System Calls:
System calls are categorised into 6 types:
1. Process control
2. File management
3. Device management
4. Information maintenance
5. Communications
6. Protection
1. Process Control:
This system calls perform the task of process creation, process termination, etc.
Functions:
o End and Abort
o Load and Execute
o Create Process and Terminate Process
o Wait and Signed Event
o Allocate and free memory
2. File management:
File management system calls handle file manipulation jobs like creating a file, reading, and
writing, etc.
Functions:
o Create a file
o Delete file
o Open and close file
o Read, write, and reposition
o Get and set file attributes
3. Device management:
A process may need several resources to execute—main memory, disk drives, access to files,
and so on. The various resources controlled by the operating system can be thought of as devices.
Some of these devices are physical devices (for example, disk drives), and others are abstract or
virtual devices (for example, files).
Functions:
o request device, release device
o read, write, reposition
o get device attributes, set device attributes
o logically attach or detach devices
4. Information maintenance:
It handles information and its transfer between the OS and the user program.
Functions:
o Get or set time and date
o Get process and device attributes
5. Communications:
There are two common models of inter-process communication:
1. Message passing model
2. Shared-memory model.
In the message-passing model, the communicating processes exchange messages with one
another to transfer information. Messages can be exchanged between the processes either directly or
indirectly through a common mailbox.
In the shared-memory model, processes use shared memory for communication. create and
attach shared memory system calls are used to create and gain access to regions of memory owned
by other processes.
Functions:
o create, delete communication connection
o send, receive messages
o transfer status information
o Attach or detach remote devices
6. Protection:
Protection provides a mechanism for controlling access to the resources provided by a
computer system.System calls providing protection include set permission and get permission,
which manipulate the permission settings of resources such as files and disks.The allow user and
deny user system calls specify whether particular users can—or cannot—be allowed access to
certain resources.

. Process Control in Single Tasking (MS DOS) and Multi-Tasking (FreeBSD) Systems:
Process Control in MS DOS:
 The MS-DOS operating system is an example of a single-tasking system.
 It has a command interpreter that is invoked when the computer is started, shown in below figure.

o
 Because MS-DOS is single-tasking, it uses a simple method to run a program and does not create a
new process. It loads the program into memory, writing over most of itself to give the program as
much memory as possible, shown in below figure.

o
 Next, it sets the instruction pointer to the first instruction of the program. The program then runs, and
either an error cause a trap, or the program executes a system call to terminate.
 In either case, the error code is saved in the system memory for later use.
 Following this action, the small portion of the command interpreter that was not overwritten
resumes execution.
 Its first task is to reload the rest of the command interpreter from disk. Then the command
interpreter makes the previous error code available to the user or to the next program.
Process Control in FreeBSD:
o FreeBSD (derived from Berkeley UNIX) is an example of a multitasking system.
o When a user logs on to the system, the shell of the user’s choice is run. This shell is similar to
the
o MS-DOS shell in that it accepts commands and executes programs that the user requests.
o Since FreeBSD is a multitasking system, the command interpreter may continue running
while another program is executed, as shown in below figure.

o
o To start a new process, the shell executes a fork() system call. Then, the selected
program is loaded into memory via an exec() system call, and the program is executed.
o Depending on the way the command was issued, the shell then either waits for the
process to finish or runs the process “in the background.”
o When the process is done, it executes an exit() system call to terminate, returning to the
invoking process a status code of 0 or a nonzero error code.
o This status or error code is then available to the shell or other programs.
o
9. Operating system structure

9.1Operating system structure


Operating system can be implemented with the help of various structures.
The operating system defines how the various components of the operating system are
interconnected and melded into the kernel.
Depending on this we have following structures of the operating system:
1. Simple Structure
2. Layered Approach
3. Micro-kernels
4. Modules
1. Simple Structure:
 The Simple Structured operating systems do not have well defined structure and are small, simple
and limited systems.
 The interfaces and levels of functionality are not well separated.
 MS-DOS is an example of such operating system.
 In MS-DOS, application programs are able to access the basic I/O routines to write directly to the
display and disk drives.
 These types of operating system cause the entire system to crash if one of the user programs fails.
The below figure illustrates structure of MS-DOS:

Application Programs

Resident System Program

MS-DOS Device Drivers

ROM BIOS Device Drivers

Figure: MS DOS Layer Structure


 Another example of simple structured operating system is the original UNIX operating system.
 UNIX operating system consists of two parts: the kernel and the system programs.
 The kernel is further separated into a series of interfaces and device drivers, as shown in below
figure:

2. Layered Approach:
In this layered approach, structure the operating system is broken into number of layers
(levels).
The bottom layer (layer 0) is the hardware and the topmost layer (layer N) is the user interface.
Each layer uses the functions of the lower level layers only.
The below figure illustrates the layered structure of the operating system.

 Advantages:
 This simplifies the debugging process as if lower level layers are debugged and an error occurs
during debugging then the error must be on that layer only.
 Operating system can retain greater control over the computer and over the applications
that make use of the computer.
o Disadvantages:
o Each layer, the data needs to be modified and passed on which adds overhead to the system.
o Less efficient than other structures
o Careful planning of the layers and implementation is necessary.
3. Micro-kernels:
The micro-kernels structure designs the operating system by removing all non-essential
components from the kernel and implementing them as system and user programs. This result
in a smaller kernel called the micro-kernel.
Communication takes place between user modules using message passing.
Tru64 UNIX is implemented with Mach kernel and Mac OS X kernel (also known as Darwin)
is implemented with Mach micro kernel.
Mac OS X structure is illustrated in below figure:

In the above figure:


o The top layers include application environments and a set of services providing a
graphical interface to applications.
o Below these layers is the kernel environment, which consists primarily of the Mach
microkernel and the BSD kernel.
o Mach provides memory management, support for remote procedure calls (RPCs) and
inter-process communication (IPC).
o The BSD component provides a BSD command line interface, support for networking
and file systems, and an implementation of POSIX APIs, including Pthreads
Advantages:
o All new services need to be added to user space and does not require the kernel to be
modified.
o More secure and reliable (less code is running in kernel mode).
o Easier to extend a microkernel.
.Disadvantages:
o Performance and communication overhead of user space to kernel space.
4. Modules:
Most modern operating systems implement kernel modules Uses object-oriented approach to
create modular kernel.
The kernel has only set of core components and other services are added as dynamically
loadable modules to the kernel either during run time or boot time.
It resembles layered structure
Each core component is separate; each talks to the others over known interfaces.
Modular structure is similar to layered structure but with more flexible.
For example, the Solaris operating system structure, shown in below figure, is organized
around a core kernel with seven types of loadable kernel modules.
10. System Boot.
o The procedure of starting a computer by loading the kernel is known as booting the system.
Bootstrap Loader:
o A small piece of code known as the bootstrap program or bootstrap loader locates the
kernel, loads it into main memory, and starts its execution.
o Bootstrap loader is stored in ROM (Read-Only Memory).
o ROM is convenient because it needs no initialization and cannot be infected by a computer
virus.
Booting Process in computers:
o When a CPU receives a reset event—for instance, when it is powered up or rebooted—the
instruction register is loaded with a bootstrap loader.
o The bootstrap program can perform a variety of tasks:
It runs diagnostics to determine the state of the machine. If the diagnostics pass, the
program can continue with the booting steps.
It can also initialize all aspects of the system, from CPU registers to device controllers
and the contents of main memory.
Loads the operating system from boot block into the RAM.
Boot block is a fixed location in hard disk, where the operating located in hard disk.
o Once the operating system is loaded into the RAM, it will take over the system to perform the
tasks.

Booting Process in cellular phones, PDAs, and game consoles:


o Store the entire operating system in ROM.
o Storing the operating system in ROM is suitable for small operating systems, simple
supporting hardware, and rugged operation.
o A problem with this approach is that changing the bootstrap code requires changing the ROM
hardware chips.
o Some systems resolve this problem by using erasable programmable read-only memory
(EPROM).
Processes
1. Process Concept
1.1. The Process:
 Definition: A process is a program in execution.
Process in Memory:
A process includes
o Text section, which contains program code.
o Process stack, which contains temporary data (such as function parameters, return
addresses, and local variables).
o Data section, which contains global variables.
o Heap, which is memory that is dynamically allocated during process run time.
The structure of a process in memory is shown in below figure.

Program VS Process:
o A program is a passive entity, such as a file containing a list of instructions stored on
disk (also called an executable file).
o A process is an active entity, with a program counter specifying the next instruction to
execute and a set of associated resources.

S.No. Program Process

Program contains a set of instructions Process is an instance of an executing


1.
designed to complete a specific task. program.

Program is a passive entity as it resides in Process is an active entity as it is created


2.
the secondary memory. during execution and loaded into the main
S.No. Program Process

memory.

Program exists at a single place and Process exists for a limited span of time as it
3.
continues to exist until it is deleted. gets terminated after the completion of task.

4. Program is a static entity. Process is a dynamic entity.

Program does not have any resource Process has a high resource requirement; it
5. requirement; it only requires memory space needs resources like CPU, memory address,
for storing the instructions. I/O during its lifetime.

Process has its own control block called


6. Program does not have any control block.
Process Control Block.

In addition to program data, a process also


Program has two logical components: code
7. requires additional information required for
and data.
the management and execution.

Many processes may execute a single


program. There program code may be the
8. Program does not change itself.
same but program data may be different.
These are never same.

1.2. Process State:


 The state of a process defines current activity of that process.
 Each process may be in one of the following states:
o New: The process is being created.
o Running: Instructions are being executed.
o Waiting: The process is waiting for some event to occur (such as an I/O completion or
reception of a signal).
o Ready: The process is waiting to be assigned to a processor.
o Terminated: The process has finished execution.
 The state diagram of the process (life cycle of process) is illustrated below figure.
1.3. Process Control Block
 Each process is represented in the operating system by a process control block (PCB).
 PCB also called a task control block.
 A PCB is shown in below figure:

 Many pieces of information associated with a specific process, including these:


1. Process state: The state may be new, ready, running, waiting, halted, and so on.
2. Program counter: The counter indicates the address of the next instruction to be executed
for this process.
3. CPU registers: They include accumulators, index registers, stack pointers, and general-
purpose registers, plus any condition-code information.
Along with the program counter, this state information must be saved when an interrupt
occurs, to allow the process to be continued correctly afterward, see the below figure:
4. CPU-scheduling information: This information includes a process priority, pointers to
scheduling queues, and any other scheduling parameters.
5. Memory-management information: This information may include such information as the
value of the base and limit registers, the page tables, or the segment tables.
6. Accounting information: This information includes the amount of CPU and real time used,
time limits, account numbers, job or process numbers, and so on.
7. I/O status information: This information includes the list of I/O devices allocated to the
process, a list of open files, and so on.

2. Process scheduling
2.1. Process Scheduling:
 The goal of multiprogramming is to maximize CPU usage, with certain processes running at
all times.
 The goal of time sharing is to change the CPU frequently between processes so that users
can interact with each program while it is running.
 To achieve these objectives, the process scheduler selects the process available for execution
on the CPU.
 For a single-processor system, there will be one processor and one process will execute at a
time, the rest will have to wait until the CPU is free and rescheduled.
2.1.1. Scheduling Queues
o As processes enter the system, they are inserting into a job queue.
o Job queue consists of all processes in the system.
o The processes that are residing in main memory and are ready and waiting to execute are
kept on a list called the ready queue.
o A ready-queue is implemented using linked list, contains header points to the first and
final PCBs in the list.
o Each PCB includes a pointer field that points to the next PCB in the ready queue.
o When a process is allocated the CPU, it executes for a while and waits for the occurrence
of a particular event, such as the completion of an I/O request.
o The list of processes waiting for a particular I/O device is called a device queue. Each
device has its own device queue. See the below figure:

o The representation of process scheduling is a queueing diagram, illustrated in below


figure.
o In the above figure:
 Each rectangular box represents a queue. Two types of queues are present: the
ready queue and a set of device queues.
 The circles represent the resources that serve the queues
 And the arrows indicate the flow of processes in the system.
o Process Execution:
 A new process is initially put in the ready queue.
 It waits there until it is selected for execution.
 Once the process is allocated the CPU and is executing, one of several events
could occur:
1. The process could issue an I/O request and then be placed in an I/O
queue.
2. The process could create a new sub-process and wait for the sub-process’s
termination.
3. The process could be removed forcibly from the CPU as a result of an
interrupt and be put back in the ready queue.
 In the first two cases, the process eventually switches from the waiting state to
the ready state and is then put back in the ready queue.
 A process continues this cycle until it terminates, at which time it is removed
from all queues and has its PCB and resources de-allocated.
2.1.2. Schedulers
A process migrates among the various scheduling queues throughout its lifetime. The
operating system must select processes from these queues for scheduling in some fashion. The
selection process is carried out by the appropriate scheduler.
In the batch system, immediately executable processes are spooled to the mass-storage
device (usually the disk), where they stored for further execution. The long-term scheduler, or
job scheduler, selects processes from this pool and loads them into memory for execution. The
short-term scheduler, or CPU scheduler, selects processes from memory that are ready to
execute and allocates the CPU to one of them.
Short-term scheduler is invoked very frequently (milliseconds), must be fast. Long-term
scheduler is invoked very infrequently (seconds, minutes), may be slow. The long-term
scheduler controls the degree of multiprogramming.
Most processes can be described as either I/O bound or CPU bound. I/O-bound process
spends more time doing I/O than it spends doing computations. A CPU-bound process generates
I/O requests infrequently, using more of its time doing computations. It is important that the long-
term scheduler select a good process mix of I/O-bound and CPU-bound processes.
If all processes are I/O bound, the ready queue will almost always be empty, and the
short-term scheduler will have little to do. If all processes are CPU bound, the I/O waiting
queue will almost always be empty, devices will go unused, and again the system will be
unbalanced. The best performing system is a combination of CPU-bound and I / O-bound
processes.
In some time sharing systems like MS Windows and UNIX have no long-term scheduler.
All the processors are available to short-term scheduler. The stability of these systems depends
either on a physical limitation (such as the number of available terminals) or on the self-adjusting
nature of human users.
In some other timesharing systems, may added medium-term scheduler. The
diagrammatic representation of medium-term scheduler is shown in below figure.

The main idea behind the medium-term scheduler is that sometimes it is beneficial to
remove processes from memory (and from active contention for the CPU) and thereby reduce the
level of multiprogramming. Later, the process can be reintroduced into memory, and its execution
can be continued where it left off. This scheme is called swapping. The process is swapped out,
and is later swapped in, by the medium-term scheduler.

2.1.3. Context Switch


When CPU switching to another process, the system must save the state of the current
process and restore the saved state for the new process. This task is known as a Context switch.
When a context switch occurs, the kernel saves the state of the old process in its PCB and
loads the saved context of the new process scheduled to run.
Context-switch time is pure overhead, because the system does no useful work while
switching.
Context-switch times are highly dependent on hardware support (like, memory speed, the
number of registers, and special instructions).

3. Operations on processes
3.1. Operations on Processes:
The processes in most systems can execute concurrently, and they may be created and
deleted dynamically. Thus, these systems must provide a mechanism for process creation and
termination.
3.1.1. Process Creation:
o Process may create several new processes, via a create-process system call, during the
execution.
o The creating process is called a parent process, and the new processes are called the
children of that process.
o Each new process may create other processes, forming a tree of processes.
o Generally, process identified and managed via a process identifier (pid).
o pid is unique integer value.
o Resource Sharing:
 Parent and children share all resources.
 Children share subset of parent’s resources.
o Execution:
 Parent and children execute concurrently.
 Parent waits until children terminate.
o Address space:
 The child process is a duplicate of the parent process.
 The child process has a new program loaded into it.
o Example:
 Unix Operating System:
1. Each process is identified by its process identifier (pid).
2. A new process is created by the fork() system call.
3. The new process consists of a copy of the address space of the original
process. This mechanism allows the parent process to communicate easily
with its child process.
4. Both processes (the parent and the child) continue execution at the
instruction after the fork(), with one difference: the return value for the
fork() is zero for the new (child) process, whereas the (nonzero) process
identifier of the child is returned to the parent.
5. The exec() system call is used after a fork() system call by one of the two
processes to replace the process’s memory space with a new program.
6. The parent waits for the child process to complete with the wait() system
call.
7. When the child process completes the parent process resumes from the
call to wait(), where it completes using the exit() system call.
8. This is also illustrated in below figure.

3.1.2. Process Termination:


o A process terminates when it finishes executing its final statement and asks the operating
system to delete it by using the exit() system call.
o At that point, the process may return a status value (an integer) to its parent process.
o Process’ resources are de-allocated by operating system.
o Parent may terminate execution of children processes using abort system call.
o A parent may terminate the execution of one of its children for a variety of reasons, such
as these:
 The child has exceeded its usage of some of the resources that it has been
allocated.
 The task assigned to the child is no longer required.
 The parent is exiting, and the operating system does not allow a child to continue
if its parent terminates. This phenomenon, is called cascading termination.
4. Inter‐process communication
4.1. Inter-process communication (IPC):
 Processes executing concurrently in the operating system may be either independent processes
or cooperating processes.
 Independent process:
o Any process that does not share data with any other process is called independent
process.
o An independent process cannot affect or be affected by the other processes executing in
the system.
 Cooperating process:
o Any process that shares data with other processes is called cooperating process.
o A cooperating process can affect or be affected by the other processes executing in the
system.
 There are several reasons for providing an environment that allows process cooperation:
o Information sharing: Since several users may be interested in the same piece of
information (for instance, a shared file), we must provide an environment to allow
concurrent access to such information.
o Computation speedup: If we want a particular task to run faster, we must break it into
subtasks, each of which will be executing in parallel with the others. Notice that such a
speedup can be achieved only if the computer has multiple processing elements (such as
CPUs or I/O channels).
o Modularity: We may want to construct the system in a modular fashion, dividing the
system functions into separate processes or threads.
o Convenience: Even an individual user may work on many tasks at the same time. For
instance, a user may be editing, printing, and compiling in parallel.
 Inter-process communication (IPC):
o Cooperating processes require an inter-process communication (IPC) that will allow the
processes to exchange data and information.
 There are two fundamental models of inter-process communication:
1. Shared memory
2. Message passing
1. Shared Memory:
o In the shared-memory model, a region of shared memory is established by cooperating
processes.
o Processes can then exchange information by reading and writing data to the shared
region.
o The shared memory is illustrated in below figure:

o Producer-Consumer Problem:
 Paradigm for cooperating processes, producer process produces information that
is consumed by a consumer process.
 For example,
1. A compiler may produce assembly code, which is consumed by an
assembler.
2. The assembler may produce object modules, which are consumed by the
loader.
3. In client–server paradigm, A Web server produces HTML files, images
consumed by client.
o In IPC, shared memory is the solution for producer-consumer problem.
o A producer can produce one item while the consumer is consuming another item.
o The producer and consumer must be synchronized, so that the consumer does not try to
consume an item that has not yet been produced.
o Two types of buffers can be used for producer-consumer problem:
1. Unbounded buffer:
 Unbounded buffer has no limit on the size of the buffer.
 The consumer may have to wait for new items, but the producer can
always produce new items.
2. Bounded buffer:
 Bounded buffer has limit on the size of the buffer, which is fixed buffer
size.
 In this case, the consumer must wait if the buffer is empty, and the
producer must wait if the buffer is full.
2. Message Passing:
o In the message passing model, communication takes place by means of messages
exchanged between the cooperating processes.
o The message passing is illustrated in below figure:

o This model is useful in distributed environment, where the communicating processes


may reside on different computers connected by a network.
o For example, a chat program used on the World Wide Web could be designed so that
chat participants communicate with one another by exchanging messages.
o A message-passing facility provides at least two operations:
1. Send message
2. Receive message
o Messages sent by a process can be of either fixed or variable size.
o In fixed-sized messages, the system-level implementation is straightforward, but the
programming task becomes complex.
o In Variable-sized messages, the system-level implementation is more complex, but the
programming task becomes simpler.
o A communication link must exist between two processes to send and receive messages
from each other.
o A communication link is implemented using following methods:
1. Naming
2. Synchronization
3. Buffering
1. Naming:
 Processes must name each other explicitly to communicate.
 Direct Communication:
 Symmetric scheme:
 In this, each process that wants to communicate must explicit
name of the recipient or sender of the communication.
 In Symmetric scheme, the send() and receive() primitives are
defined as:
 send(P, message)—Send a message to process P.
 receive(Q, message)—Receive a message from process Q.
 Asymmetric scheme:
 In this, only the sender names the recipient; the recipient is not
required to name the sender.
 In Asymmetric scheme, the send() and receive() primitives are
defined as follows:
 send(P, message)—Send a message to process P.
 receive(id, message)—Receive a message from any
process; the variable id is set to the name of the process
with which communication has taken place.
 A communication link in this scheme has the following properties:
 A link is established automatically between every pair of
processes that want to communicate. The processes need to know
only each other’s identity to communicate.
 A link is associated with exactly two processes.
 Between each pair of processes, there exists exactly one link.
 The drawback of both symmetric and asymmetric schemes is that:
Changing the identifier of a process may examine all other process
definitions.

 Indirect Communication:
 In indirect communication, the messages are sent to and received from
mailboxes, or ports.
 The send() and receive() primitives are defined as:
 send(A, message)—Send a message to mailbox A.
 receive(A, message)—Receive a message from mailbox A.
 A communication link in this scheme has the following properties:
 Link is established between a pair of processes only if both
members of the pair have a shared mailbox.
 A link may be associated with more than two processes.
 Between each pair of communicating processes, there may be a
number of different links, with each link corresponding to one
mailbox.
2. Synchronization:
 Message passing may be blocking or non-blocking — also known as
synchronous and asynchronous.
 Blocking send: The sending process is blocked until the message is received by
the receiving process or by the mailbox.
 Non-blocking send: The sending process sends the message and resumes
operation.
 Blocking receive: The receiver blocks until a message is available.
 Non-blocking receive: The receiver retrieves either a valid message or a null.
3. Buffering:
 Whether communication is direct or in-direct, messages exchanged by
communicating processes reside in a temporary queue.
 Basically, such queues can be implemented in three ways:
1. Zero capacity: The queue has a maximum length of zero; thus, the link
cannot have any messages waiting in it. In this case, the sender must
block until the recipient receives the message.
2. Bounded capacity. The queue has finite length n; thus, at most n
messages can reside in it. If the queue is not full when a new message is
sent, the message is placed in the queue, and the sender can continue
execution without must block until space is available in the queue.
3. Unbounded capacity. The queue’s length is potentially infinite; thus, any
number of messages can wait in it. The sender never blocks.
 The zero-capacity case is sometimes referred to as a message system with no
buffering; the other cases are referred to as systems with automatic buffering.

5. Communication in client server systems


5.1. Communication in client server systems:
 Client – Server communication is an example of IPC: Shared memory model.
 In this section, we discuss two other strategies for communication in client–server systems:
1. Sockets
2. Remote Procedure Calls
1. Sockets:
o A socket is defined as an endpoint for communication.
o A pair of processes communicating over a network employ a pair of sockets — one for
each process.
o A socket is identified by an IP address concatenated with a port number.
o The server waits for incoming client requests by listening to a specified port.
o Once a request is received, the server accepts a connection from the client socket to
complete the connection.
o Servers implementing specific services (such as telnet, FTP, and HTTP) listen to well-
known ports (a telnet server listens to port 23; an FTP server listens to port 21; and a
Web, or HTTP, server listens to port 80).
o All ports below 1024 are considered well known; we can use them to implement
standard services.
o When a client process initiates a request for a connection, it is assigned a port by its host
computer. This port is some arbitrary number greater than 1024.
o For example, if a client on host X with IP address 146.86.5.20 wishes to establish a
connection with a Web server (which is listening on port 80) at address 161.25.19.8, host
X may be assigned port 1625.
o The connection will consist of a pair of sockets: (146.86.5.20:1625) on host X and
(161.25.19.8:80) on the Web server. This situation is illustrated in below figure.
o The packets travelling between the hosts are delivered to the appropriate process based
on the destination port number.

o All connections must be unique. Therefore, if another process also on host X wished to
establish another connection with the same Web server, it would be assigned a port
number greater than 1024 and not equal to 1625.

2. Remote Procedure Calls (RPC):


o Remote procedure calls (RPC) are abstracts procedure calls between processes on
networked systems.
o RPCs allow a client to invoke a procedure on a remote host because it invokes a
procedure locally.
o The RPC system hides the detail that allows communication to take place by providing a
stub on the client side.
o When the client invokes a remote procedure, the RPC system calls the appropriate stub.
This stub locates the port on the server and marshals the parameters.
o Parameter marshalling involves packaging the parameters into a form that can be
transmitted over a network.
o The stub then transmits a message to the server using message passing.
o A similar stub on the server side receives this message and invokes the procedure on the
server. If necessary, return values are passed back to the client using the same technique.
o To identify the port number of the server the client uses matchmaker daemon on a fixed
RPC port.
o The RPC is illustrated in below figure:

Multithreaded Programming
Overview:
Thread - Definition:
 A thread is a basic unit of CPU utilization; it comprises a thread ID, a program counter, a
register set, and a stack.
Single Thread and Multithreaded Process:
 A traditional (or heavyweight) 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.
 Below figure illustrates the difference between a traditional single-threaded process and a
multithreaded process.

Multithreaded Server Architecture:


 Let Consider, a Web server accepts client requests for Web pages, images, sound, and so forth.
 A busy Web server may have several (perhaps thousands of) clients concurrently accessing it.
 If the Web server ran as a traditional single-threaded process, it would be able to service only
one client at a time, and a client might have to wait a very long time for its request to be
serviced.
 If the server run as a single process that accepts requests. When the server receives a request, it
creates a separate process to service that request. In fact, Process creation is time consuming and
resource intensive, however. The new process will perform the same tasks as the existing
process, this is an overhead?
 It is more efficient to use one process that contains multiple threads.
 If the Web-server process is multithreaded, the server will create a separate thread that listens
for client requests. When a request is made, rather than creating another process, the server will
create a new thread to service the request and resume listening for additional requests. This is
illustrated in below figure.
Benefits of Multithreading:
1. Responsiveness
2. Resource sharing
3. Economy
4. Scalability
Single core VS Multi-core system:

Process VS Thread

S.NO Process Thread

Process means any program is in


1. Thread means a segment of a process.
execution.

2. The process takes more time to terminate. The thread takes less time to terminate.

3. It takes more time for creation. It takes less time for creation.

It also takes more time for context


4. It takes less time for context switching.
switching.

The process is less efficient in terms of Thread is more efficient in terms of


5.
communication. communication.
S.NO Process Thread

We don’t need multi programs in action for


Multiprogramming holds the concepts of
6. multiple threads because a single process consists
multi-process.
of multiple threads.

7. The process is isolated. Threads share memory.

The process is called the heavyweight A Thread is lightweight as each thread in a


8.
process. process shares code, data, and resources.

Thread switching does not require calling an


Process switching uses an interface in an
9. operating system and causes an interrupt to the
operating system.
kernel.

If one process is blocked then it will not If a user-level thread is blocked, then all other
10.
affect the execution of other processes user-level threads are blocked.

The process has its own Process Control Thread has Parents’ PCB, its own Thread Control
11.
Block, Stack, and Address Space. Block, and Stack and common Address space.

Since all threads of the same process share address


Changes to the parent process do not space and other resources so any changes to the
12.
affect child processes. main thread may affect the behavior of the other
threads of the process.

1. Multithreading models
1.1. Multithreaded Programming:
 User Threads:
o User threads supported at user level, and are managed without kernel support.
 Kernel Threads:
o Kernel threads are supported and managed directly by the operating system.
o Example:
 Operating systems—including Windows, Linux, Mac OS X, Solaris, and Tru64
UNIX (formerly Digital UNIX)—support kernel threads.

 There are 3 ways of establishing relationship between user threads and kernel threads.
1. Many-to-One Model
2. One-to-One Model
3. Many-to-Many Model

1. Many-to One Model:


o Many-to-One model is illustrated in below figure:

o The many-to-one model maps many user-level threads to one kernel thread.
o Thread management is done by the thread library in user space, so it is efficient; but the
entire process will block if a thread makes a blocking system call.
o Also, because only one thread can access the kernel at a time, multiple threads are unable
to run in parallel on multiprocessors.
o Green threads — a thread library available for Solaris—uses this model.

2. One-to-One Model:
o One-to-One model is illustrated in below figure:
o The one-to-one model maps each user thread to a kernel thread.
o It provides more concurrency than the many-to-one model by allowing another thread to
run when a thread makes a blocking system call; it also allows multiple threads to run in
parallel on multiprocessors.
o The only drawback to this model is that creating a user thread requires creating the
corresponding kernel thread.
 Because the overhead of creating kernel threads can burden the performance of
an application.
o Linux, along with the family of Windows operating systems, implement the one-to-one
model.
3. Many-to-Many Model:
o Many-to-Many model is illustrated in below figure:

o The many-to-many model multiplexes many user-level threads to a smaller or equal


number of kernel threads.
o The number of kernel threads may be specific to either a particular application or a
particular machine.
o Whereas the many-to-one model allows the developer to create as many user threads as
she wishes, true concurrency is not gained because the kernel can schedule only one
thread at a time.
o Solaris prior to version 9 uses this model.
o Two Level Model:
 Similar to Many to Many, except that it allows a user thread to be bound to
kernel thread.
 IRIX, HP-UX, Tru64 UNIX are uses this model.
 This model is illustrated in below figure:

2. Thread libraries
2.1. Thread Libraries:
 A thread library provides the programmer with an API for creating and managing threads.
 There are two ways of implementing a thread library.
 First approach:
o It is to provide a library entirely in user space with no kernel support.
o All code and data structures for the library exist in user space.
o Invoking a function in the library results in a local function call in user space and not a
system call.
 Second approach:
o It is to implement a kernel-level library supported directly by the operating system.
o All code and data structures for the library exist in kernel space.
o Invoking a function in the API for the library results in a system call to the kernel.
 Three main thread libraries are in use today:
1. POSIX Pthreads
2. Win32 threads
3. Java threads
1. POSIX Pthreads:
o May be provided either as user-level or kernel-level.
o A POSIX standard (IEEE 1003.1c) defining API for thread creation and synchronization.
o API specifies behaviour of the thread library, implementation is up to development of
the library Common in Solaris, Linux, Mac OS X andTru64 UNIX.
o pthread_create( ) function is used to create pthreads.
2. Win32 threads:
o The technique for creating threads using the Win32 thread library is similar to the
Pthreads technique in several ways.
o We must include the windows.h header file when using the Win32 API.
o Threads are created in theWin32 API using the CreateThread() function.
3. Java threads:
o Threads are the fundamental model of program execution in a Java program,
o The Java language and its API provide a rich set of features for the creation and
management of threads.
o All Java programs comprise at least a single thread of control—even a simple Java
program consisting of only a main() method runs as a single thread in the JVM.
o There are two techniques for creating threads in a Java program.
1. One approach is to create a new class that is derived from the Thread
class and to override its run() method.
2. The Second Approach is —to define a class that implements the Runnable
interface.
o Creating a Thread object in java does not specifically create the new thread; rather, it is
the start() method that creates the new thread.
o Calling the start() method for the new object does two things:
1. It allocates memory and initializes a new thread in the JVM.
2. It calls the run() method, making the thread eligible to be run by the JVM.
o When the summation java program runs, two threads are created by the JVM.
1. The first is the parent thread, which starts execution in the main() method.
2. The second thread is created when the start() method on the Thread object
is invoked.
Process Scheduling
1. Basic concepts
Single-Programming Systems (or) Single -Processing Systems (or) Single-Tasking Systems:
 In a single- tasking system, only one process can run at a time; any others must wait until the
CPU is free and can be rescheduled.
 A process is executed until it must wait, typically for the completion of some I/O request. In a
simple computer system, the CPU then just sits idle. All this waiting time is wasted; no useful
work is accomplished. This is overcome in multi-programming systems.
Multi-Programming Systems (or) Multi-Processing Systems (or) Multi-Tasking Systems:
 In multi-programming, some process running at all times, in order to maximize CPU utilization.
 In this, several processes are kept in memory at one time. Every time a process has to wait,
another process can capture CPU usage.
1.1. CPU–I/O Burst Cycle:
 The below figure illustrates CPU-I/O Burst Cycle.

 Process execution consists of a cycle of CPU execution and I/O wait.


 Processes alternate between these two states.
 Process execution begins with a CPU burst. That is followed by an I/O burst, which is followed
by another CPU burst, then another I/O burst, and so on.
 Eventually, the final CPU burst ends with a system request to terminate execution.
1.2. CPU Scheduler:
 Whenever the CPU becomes idle, the operating system must select one of the processes in the
ready queue to be executed. The selection process is carried out by the short-term scheduler or
CPU scheduler.
1.3. Preemptive and Non-Preemptive Scheduling:
 CPU scheduling decisions may take place, when a process:
1. Switches from running to waiting state
2. Switches from running to ready state
3. Switches from waiting to ready
4. Terminates
 When scheduling takes place only under 1 and 4, we say that the scheduling scheme is non-pre-
emptive or cooperative; otherwise, it is pre-emptive.
1.4. Dispatcher:
 The dispatcher is the module that gives control of the CPU to the process selected by the short-
term scheduler.
 This function involves the following:
o Switching context
o Switching to user mode
o Jumping to the proper location in the user program to restart the program
 The time it takes for the dispatcher to stop one process and start another running is known as the
dispatch latency.

2. Scheduling criteria
 Different CPU-scheduling algorithms have different properties.
 The following characteristics are used for comparison of scheduling algorithms.
1. CPU utilization:
o We want to keep the CPU as busy as possible.
o In a real system, it should range from 40% (for a lightly loaded system) to 90% (for a
heavily used system).
2. Throughput:
o The number of processes that are completed per time unit, called throughput.
o For long processes, this rate may be one process per hour; for short transactions, it may
be ten processes per second.
3. Turnaround time:
o The interval from the time of submission of a process to the time of completion is the
turnaround time.
o Turnaround time is the sum of the periods spent waiting to get into memory, waiting in
the ready queue, executing on the CPU, and doing I/O.
4. Waiting time:
o Waiting time is the total time spent in the ready queue.
5. Response time:
o Response time is an amount of time it takes from when a request was submitted until the
first response is produced, not output

Note:
 The response time is better than turnaround time, because turnaround time is generally limited
by the speed of the output device.
 The best scheduling algorithm is to maximize CPU utilization and throughput and to
minimize turnaround time, waiting time, and response time.
 Gantt chart, which is a bar chart that illustrates a particular schedule, including the start and
finish times of each of the participating processes.

3. Scheduling algorithms
 CPU scheduling deals with the problem of deciding which of the processes in the ready queue is
to be allocated the CPU.
 There are many different CPU-scheduling algorithms. They are:
 First-Come, First-Served Scheduling
 Shortest-Job-First Scheduling
 Priority Scheduling
 Round-Robin Scheduling
 Multilevel Queue Scheduling
 Multilevel Feedback Queue Scheduling

1. First-Come, First-Served Scheduling (FCFS):


o The simplest CPU-scheduling algorithm is the first-come, first-served (FCFS) scheduling
algorithm.
o With this scheme, the CPU is allocated to the process that request CPU first.
o FIFO Queue is used in implementation of the FCFS.
o When a process enters the ready queue, its PCB is linked onto the tail of the queue. When
the CPU is free, it is allocated to the process at the head of the queue. The running process
is then removed from the queue.
o Example:
1. Consider the following set of processes that arrive at time 0, with the length of the
CPU burst given in milliseconds:
Process Burst Time
P1 24
P2 3
P3 3
2. Suppose that the processes arrive in the order: P1, P2, and P3. The Gantt Chart for
the schedule is:

o Waiting time for P1 = 0; P2 = 24; P3 = 27.


o Average waiting time: (0 + 24 + 27)/3 = 17 milliseconds
3. Suppose that the processes arrive in the order P2, P3, P1. The Gantt chart for the
schedule is:

o Waiting time for P1 = 6; P2 = 0; P3 = 3


o Average waiting time: (6 + 0 + 3)/3 = 3 milliseconds
o Advantages of FCFS
1. Simple
2. Easy
3. First come, First serve
o Disadvantages of FCFS
1. The scheduling method is non-pre-emptive; the process will run to the
completion.
2. Due to the non-pre-emptive nature of the algorithm, the problem of starvation
(Convoy Effect) may occur.
3. Although it is easy to implement, but it is poor in performance since the average
waiting time is higher as compare to other scheduling algorithms.
o Problems:
1. Consider the following set of processes that arrive at time 0, with the length of the
CPU burst given in milliseconds:
Process Burst Time
P1 24
P2 3
P3 3
Calculate Completion Time, Turn Around Time, Waiting Time and Average
Waiting Time using FCFS Algorithm.
2. Consider the following set of processes with different arrival times and with the length
of the CPU burst given in milliseconds:
Process Arrival Time Burst Time
P1 0 2
P2 1 6
P3 2 4
P4 3 9
P5 6 12
Calculate Completion Time, Turn Around Time, Waiting Time and Average
Waiting Time using FCFS Algorithm.
Note:
Turn Around Time = Completion Time - Arrival Time
Waiting Time = Turnaround time - Burst Time

o Convoy Effect:
1. If the CPU gets the processes of the higher burst time at the front end of the ready
queue then the processes of lower burst time may get blocked which means they
may never get the CPU if the job in the execution has a very high burst time. This
is called convoy effect or starvation.
2. Shortest-Job-First Scheduling (SJFS):
o SJF scheduling algorithm, schedules the processes according to their burst time.
o In SJF scheduling, the process with the lowest burst time, among the list of available
processes in the ready queue, is going to be scheduled next.
o However, it is very difficult to predict the burst time needed for a process hence this
algorithm is very difficult to implement in the system.
o Example:
1. Consider the following set of processes that arrive at time 0, with the length of the
CPU burst given in milliseconds:
Process Burst Time
P1 6
P2 8
P3 7
P4 3
2. The Gantt Chart for the SJFS is:

o Waiting time for P1 = 3; P2 = 16; P3 = 9; P4 = 0


o Average waiting time: (3 + 16 + 9 + 0)/4 = 7 milliseconds
o Advantages:
1. Maximum throughput
2. Minimum average waiting and turnaround time
o Disadvantages:
1. May suffer with the problem of starvation
2. It is not implementable because the exact Burst time for a process can't be known
in advance.
o Problems:
1. Consider the following set of processes that arrive at time 0, with the length of the
CPU burst given in milliseconds:
Process Burst Time
P1 24
P2 3
P3 3
Calculate Completion Time, Turn Around Time, Waiting Time and Average
Waiting Time using SJFS Algorithm.

2. Consider the following set of processes with different arrival times and with the length
of the CPU burst given in milliseconds:
Process Arrival Time Burst Time
P1 0 2
P2 1 6
P3 2 4
P4 3 9
P5 6 12
Calculate Completion Time, Turn Around Time, Waiting Time and Average
Waiting Time using SJFS Algorithm.
3. Consider the following set of processes with different arrival times and with the length
of the CPU burst given in milliseconds:
Process Arrival Time Burst Time
P1 1 7
P2 3 3
P3 6 2
P4 7 10
P5 9 8
Calculate Completion Time, Turn Around Time, Waiting Time and Average
Waiting Time using SJFS Algorithm

4. Consider the following set of processes with different arrival times and with the length
of the CPU burst given in milliseconds:
Process Arrival Time Burst Time
P1 0 8
P2 1 4
P3 2 9
P4 3 5
Calculate Completion Time, Turn Around Time, Waiting Time and Average
Waiting Time using
1. Pre-emptive SJFS (shortest-remaining-time-first
scheduling) Algorithm
2. Non-Pre-emptive SJFS Algorithm
3. Priority Scheduling:
o In Priority scheduling, there is a priority number assigned to each process.
o In some systems, the lower number has higher priority. While, in the others, the higher
number has higher priority.
o The Process with the higher priority among the available processes is given the CPU.
o There are two types of priority scheduling algorithm:
 Non Pre-emptive priority scheduling
 Pre-emptive Priority scheduling.
1. Non Pre-emptive priority scheduling:
2. In the Non Preemptive Priority scheduling, The Processes are scheduled according to the
priority number assigned to them.
3. Once the process gets scheduled, it will run till the completion.
4. Example
 In the Example, there are 7 processes P1, P2, P3, P4, P5, P6 and P7. Their priorities,
Arrival Time and burst time are given in the table.
Solution:
 We can prepare the Gantt chart according to the Non Pre-emptive priority
scheduling.
 The Process P1 arrives at time 0 with the burst time of 3 units and the priority
number 2. Since No other process has arrived till now hence the OS will schedule
it immediately.
 Meanwhile the execution of P1, two more Processes P2 and P3 are arrived. Since
the priority of P3 is 3 hence the CPU will execute P3 over P2.
 Meanwhile the execution of P3, All the processes get available in the ready
queue. The Process with the lowest priority number will be given the priority.
Since P6 has priority number assigned as 4 hence it will be executed just after P3.
 After P6, P4 has the least priority number among the available processes; it will
get executed for the whole burst time.
 Since all the jobs are available in the ready queue hence All the Jobs will get
executed according to their priorities. If two jobs have similar priority number
assigned to them, the one with the least arrival time will be executed.

Process ID Priority Arrival Time Burst Time


1 2 0 3
2 6 2 5
3 3 1 4
4 5 4 2
5 7 6 9
6 4 5 4
7 10 7 10
 Gantt chart:

 From the Gantt chart prepared, we can determine the completion time of every
process. The turnaround time, waiting time and response time will be determined.
 Turn Around Time = Completion Time - Arrival Time
 Waiting Time = Turn Around Time - Burst Time
Process Arrival Burst Completion Turnaround Waiting Response
Priority
Id Time Time Time Time Time Time
1 2 0 3 3 3 0 0
2 6 2 5 18 16 11 13
3 3 1 4 7 6 2 3
4 5 4 2 13 9 7 11
5 7 6 9 27 21 12 18
6 4 5 4 11 6 2 7
7 10 7 10 37 30 20 27
 Average Waiting Time = (0+11+2+7+12+2+20)/7 = 54/7 = 7.71 units

2. Pre-emptive Priority scheduling:


5. In Pre-emptive Priority Scheduling, at the time of arrival of a process in the ready queue,
its Priority is compared with the priority of the other processes present in the ready
queue as well as with the one which is being executed by the CPU at that point of time.
6. The One with the highest priority among all the available processes will be given the
CPU next.
7. The difference between pre-emptive priority scheduling and non preemptive priority
scheduling is that, in the preemptive priority scheduling, the job which is being executed
can be stopped at the arrival of a higher priority job.
8. Example
 There are 7 processes P1, P2, P3, P4, P5, P6 and P7 given. Their respective
priorities, Arrival Times and Burst times are given in the table below.
Process Id Priority Arrival Time Burst Time
1 2 0 1
2 6 1 7
3 3 2 3
4 5 3 6
5 4 4 5
6 10 5 15
7 9 15 8

Solution
GANTT chart Preparation
 At time 0, P1 arrives with the burst time of 1 units and priority 2. Since no other
process is available hence this will be scheduled till next job arrives or its
completion (whichever is lesser).

 At time 1, P2 arrives. P1 has completed its execution and no other process is


available at this time hence the Operating system has to schedule it regardless of
the priority assigned to it.
 The Next process P3 arrives at time unit 2, the priority of P3 is higher to P2.
Hence the execution of P2 will be stopped and P3 will be scheduled on the CPU.

 During the execution of P3, three more processes P4, P5 and P6 becomes
available. Since, all these three have the priority lower to the process in execution
so PS can't preempt the process. P3 will complete its execution and then P5 will
be scheduled with the priority highest among the available processes.

 Meanwhile the execution of P5, all the processes got available in the ready
queue. At this point, the algorithm will start behaving as Non Preemptive Priority
Scheduling. Hence now, once all the processes get available in the ready queue,
the OS just took the process with the highest priority and execute that process till
completion. In this case, P4 will be scheduled and will be executed till the
completion.

 Since P4 is completed, the other process with the highest priority available in the
ready queue is P2. Hence P2 will be scheduled next.

 P2 is given the CPU till the completion. Since its remaining burst time is 6 units
hence P7 will be scheduled after this.
 The only remaining process is P6 with the least priority, the Operating System
has no choice unless of executing it. This will be executed at the last.

 The Completion Time of each process is determined with the help of GANTT
chart. The turnaround time and the waiting time can be calculated by the
following formula.
 Turnaround Time = Completion Time - Arrival Time
 Waiting Time = Turn Around Time - Burst Time
Process Priority Arrival Burst Completion Turnaround Waiting
Id Time Time Time Time Time
1 2 0 1 1 1 0
2 6 1 7 22 21 14
3 3 2 3 5 3 0
4 5 3 6 16 13 7
5 4 4 5 10 6 1
6 10 5 15 45 40 25
7 9 15 8 30 20 12
 Average Waiting Time = (0+14+0+7+1+25+12)/7 = 8.43 units
Drawback with priority scheduling algorithms:
 A major problem with priority scheduling algorithms is indefinite blocking, or
starvation.
o A process that is ready to run but waiting for the CPU can be considered blocked.
A priority scheduling algorithm can leave some low priority processes waiting
indefinitely.

4. Round-Robin (RR) Scheduling:


o The round-robin (RR) scheduling algorithm is designed especially for timesharing systems.
o It is similar to FCFS scheduling, but pre-emption is added to enable the system to switch
between processes.
o A small unit of time, called a time quantum or time slice, is defined.
o A time quantum is generally from 10 to 100 milliseconds in length.
o The ready queue is treated as a circular queue.
o The CPU scheduler goes around the ready queue, allocating the CPU to each process for a
time interval of up to 1 time quantum.
o Implementation of RR Scheduling:
1. To implement RR scheduling, we keep the ready queue as a FIFO queue of processes.
2. New processes are added to the tail of the ready queue.
3. The CPU scheduler picks the first process from the ready queue, sets a timer to
interrupt after 1 time quantum, and dispatches the process.
4. One of two things will then happen.
 The process may have a CPU burst of less than 1 time quantum. In this case,
the process itself will release the CPU voluntarily. The scheduler will then
proceed to the next process in the ready queue.
 Otherwise, if the CPU burst of the currently running process is longer than 1
time quantum, the timer will go off and will cause an interrupt to the operating
system. A context switch will be executed, and the process will be put at the
tail of the ready queue. The CPU scheduler will then select the next process in
the ready queue.
o Advantages:
1. It can be actually implementable in the system because it is not depending on the
burst time.
2. It doesn't suffer from the problem of starvation or convoy effect.
3. All the jobs get a fare allocation of CPU.
o Disadvantages:
1. The higher the time quantum, the higher the response time in the system.
2. The lower the time quantum, the higher the context switching overhead in the
system.
3. Deciding a perfect time quantum is really a very difficult task in the system.
o Example:
1. In the following example, there are six processes named as P1, P2, P3, P4, P5 and P6.
Their arrival time and burst time are given below in the table. The time quantum of the
system is 4 units.

Process ID Arrival Time Burst Time


1 0 5
2 1 6
3 2 3
4 3 1
5 4 5
6 6 4
Solution:
 According to the algorithm, we have to maintain the ready queue and the Gantt
chart. The structure of both the data structures will be changed after every
scheduling.
Ready Queue:
 Initially, at time 0, process P1 arrives which will be scheduled for the time slice 4
units. Hence in the ready queue, there will be only one process P1 at starting with
CPU burst time 5 units.
P1
5

GANTT chart
 The P1 will be executed for 4 units first.

Ready Queue
 Meanwhile the execution of P1, four more processes P2, P3, P4 and P5 arrives in
the ready queue. P1 has not completed yet, it needs another 1 unit of time hence
it will also be added back to the ready queue.
Process P2 P3 P4 P5 P1
Remaining Burst Time 6 3 1 5 1

GANTT chart
 After P1, P2 will be executed for 4 units of time which is shown in the Gantt
chart.

Ready Queue
 During the execution of P2, one more process P6 is arrived in the ready queue.
Since P2 has not completed yet hence, P2 will also be added back to the ready
queue with the remaining burst time 2 units.
Process P3 P4 P5 P1 P6 P2
Remaining Burst Time 3 1 5 1 4 2

GANTT chart
 After P1 and P2, P3 will get executed for 3 units of time since its CPU burst time
is only 3 seconds.

Ready Queue
 Since P3 has been completed, hence it will be terminated and not be added to the
ready queue. The next process will be executed is P4.

Process P4 P5 P1 P6 P2
Remaining Burst Time 1 5 1 4 2

GANTT chart
 After, P1, P2 and P3, P4 will get executed. Its burst time is only 1 unit which is
lesser then the time quantum hence it will be completed.

Ready Queue
 The next process in the ready queue is P5 with 5 units of burst time. Since P4 is
completed hence it will not be added back to the queue.
Process P5 P1 P6 P2
Remaining Burst Time 5 1 4 2

GANTT chart
 P5 will be executed for the whole time slice because it requires 5 units of burst
time which is higher than the time slice.
Ready Queue
 P5 has not been completed yet; it will be added back to the queue with the
remaining burst time of 1 unit.
Process P1 P6 P2 P5
Remaining Burst Time 1 4 2 1

GANTT Chart
 The process P1 will be given the next turn to complete its execution. Since it only
requires 1 unit of burst time hence it will be completed.

Ready Queue
 P1 is completed and will not be added back to the ready queue. The next process
P6 requires only 4 units of burst time and it will be executed next.
Process P6 P2 P5
Remaining Burst Time 4 2 1

GANTT chart
 P6 will be executed for 4 units of time till completion.

Ready Queue
 Since P6 is completed, hence it will not be added again to the queue. There are
only two processes present in the ready queue. The Next process P2 requires only
2 units of time.
Process P2 P5
Remaining Burst Time 2 1
GANTT Chart
 P2 will get executed again, since it only requires only 2 units of time hence this
will be completed.

Ready Queue
 Now, the only available process in the queue is P5 which requires 1 unit of burst
time. Since the time slice is of 4 units hence it will be completed in the next
burst.
Process P5
Remaining Burst Time 1

GANTT chart
 P5 will get executed till completion.

 The completion time, Turnaround time and waiting time will be calculated as
shown in the table below.
 Turn Around Time = Completion Time - Arrival Time
 Waiting Time = Turn Around Time - Burst Time

Process Arrival Burst Completion Turnaround Waiting


ID Time Time Time Time Time
1 0 5 17 17 12
2 1 6 23 22 16
3 2 3 11 9 6
4 3 1 12 9 8
5 4 5 24 20 15
6 6 4 21 15 11
 Average Waiting Time = (12+16+6+8+15+11)/6 = 76/6 = 11.33 units

5. Multilevel Queue Scheduling:


o Another class of scheduling algorithms has been created for situations in which processes
are easily classified into different groups.
o For example, a common distribution is made between foreground (interactive) processes and
background (batch) processes. These two types of processes have different response-time
requirements and so may have different scheduling needs.
o In addition, foreground processes may have priority (externally defined) over background
processes.
o A multilevel queue scheduling algorithm partitions the ready queue into several separate
queues as shown in below figure:

o The processes are permanently assigned to one queue, generally based on some property of
the process, such as memory size, process priority, or process type.
o Each queue has its own scheduling algorithm.
o For example, separate queues might be used for foreground and background processes. The
foreground queue might be scheduled by an RR algorithm, while the background queue is
scheduled by an FCFS algorithm.
o In addition, there must be scheduling among the queues, which is commonly implemented as
fixed-priority pre-emptive scheduling.
o For example, the foreground queue may have absolute priority over the background queue.
o Let’s look at an example of a multilevel queue scheduling algorithm with five queues, listed
below in order of priority:

1. System processes
2. Interactive processes
3. Interactive editing processes
4. Batch processes
5. Student processes
o Each queue has absolute priority over lower-priority queues.
o There is a time-slice among the queues. Here, each queue gets a certain portion of the
CPU time, which it can then schedule among its various processes.
o For instance, in the foreground–background queue example, the foreground queue can be
given 80 percent of the CPU time for RR scheduling among its processes, whereas the
background queue receives 20 percent of the CPU to give to its processes on an FCFS
basis.
o In multilevel queue scheduling algorithm processes do not move from one queue to
the other, since processes do not change their foreground or background nature.
This setup has the advantage of low scheduling overhead, but it is inflexible.
6. Multilevel Feedback Queue Scheduling:
o The multilevel feedback queue scheduling algorithm allows a process to move between
queues.
o If a process uses too much CPU time, it will be moved to a lower-priority queue.
o This scheme leaves I/O-bound and interactive processes in the higher-priority queues.
o In addition, a process that waits too long in a lower-priority queue may be moved to a
higher-priority queue.
o This form of aging prevents starvation.
o For example, consider a multilevel feedback queue scheduler with three queues,
numbered from 0 to 2. See the below figure:

o The scheduler first executes all processes in queue 0. Only when queue 0 is empty will it
execute processes in queue 1.
o Similarly, processes in queue 2 will only be executed if queues 0 and 1 are empty.
o A process that arrives for queue 1 will pre-empt a process in queue 2. A process in queue
1 will in turn be preempted by a process arriving for queue 0.
o In general, a multilevel feedback queue scheduler is defined by the following
parameters:
1. The number of queues
2. The scheduling algorithm for each queue
3. The method used to determine when to upgrade a process to a higher priority
queue
4. The method used to determine when to demote a process to a lower priority
queue
5. The method used to determine which queue a process will enter when that
process needs service

4. Multiple processor scheduling


 If multiple CPUs are available, load sharing Is possible; However, the schedule problem
becomes more complicated.
 Many possibilities have been tried and as we have seen with a single processor CPU scheduling,
there is no best solution.
 Here, we discuss many concerns in multiprocessor scheduling.
 We focus on systems where the processors are homogeneous in terms of functionality; We can
use any available processor to execute any process in the queue.
1. Approaches to Multiple-Processor Scheduling:
o Two approaches for multiple-processor scheduling:
1. Asymmetric Multiprocessing:
 In this approach, all scheduling decisions, I/O processing, and other
system activities handled by a single processor—the master server. The
other processors execute only user code.
 This is simple because only one processor accesses the system data
structures, reducing the need for data sharing.
2. Symmetric Multiprocessing (SMP):
 In this approach, each processor is self-scheduling.
 All processes may be in a common ready queue, or each processor may
have its own private queue of ready processes.
o If we have multiple processors trying to access and update a common data structure, the
scheduler must be programmed carefully.
o We must ensure that two processors do not choose the same process and that processes
are not lost from the queue.
o Virtually all modern operating systems support SMP, including Windows, Solaris,
Linux, and Mac OS X.
o
2. Processor Affinity:
o When a processor running on a processor, the data most recently accessed by the process
is populate the cache of that respective processor.
o Now consider, if the process migrates to another processor. The contents of cache
memory must be invalidated for the first processor, and the cache for the second
processor must be repopulated.
o Because of the high cost of invalidating and repopulating caches, most SMP systems try
to avoid migration of processes from one processor to another and instead attempt to
keep a process running on the same processor. This is known as processor affinity.
o Soft affinity:
1. When an operating system has a policy of attempting to keep a process running
on the same processor, this situation is known as soft affinity.
2. Example: Solaris allows processes to be assigned to processor sets, limiting
which processes can run on which CPUs.
o Hard affinity:
1. Here, it is possible to migrate a process between processors. Some systems —
Like Linux — also provide system calls that support hard affinity, thereby
allowing a process to specify that it is not to migrate to other processors.
o The main-memory architecture of a system can affect processor affinity issues.
o Below Figure illustrates an architecture featuring non-uniform memory access (NUMA),
in which a CPU has faster access to some parts of main memory than to other parts.
o Typically, this occurs in systems containing combined CPU and memory boards.
o The CPUs on a board can access the memory on that board with less delay than they can
access memory on other boards in the system.
o If the operating system’s CPU scheduler and memory-placement algorithms work
together, then a process that is assigned affinity to a particular CPU can be allocated
memory on the board where that CPU resides.
3. Load Balancing:
o Load balancing attempts to keep the workload evenly distributed across all processors in
an SMP system.
o Load balancing is required in the systems where each processor has its own private
queue.
o There are two approaches to load balancing:
1. Push Migration
2. Pull Migration
1. Push Migration:
 In push migration, a specific task periodically checks the load on each processor
and—if it finds an imbalance—evenly distributes the load by moving (or
pushing) processes from overloaded to idle or less-busy processors
2. Pull Migration:
 Pull Migration occurs when an idle processor pulls a waiting from a busy
processor.
o Push and pull migration need not be mutually exclusive and are in fact often
implemented in parallel on load-balancing systems.
o For example, the Linux scheduler and the ULE scheduler available for FreeBSD systems
implement both techniques. Linux runs its load balancing algorithm every 200
milliseconds (push migration) or whenever the run queue for a processor is empty (pull
migration).
4. Multicore Processors:
o Placing multiple processor cores on the same physical chip, yields a multicore processor.
o Each core has a register set to maintain its architectural state and thus appears to the
operating system to be a separate physical processor.
o SMP systems that use multicore processors are faster and consume less power than
systems in which each processor has its own physical chip.
o One problem with multicore processors is memory stall.
o A memory stall may occur for various reasons, such as a cache misses (accessing
data that is not in cache memory).
o Below Figure illustrates a memory stall.

o In the above figure, the processor can spend up to 50 percent of its time waiting for data
to become available from memory.
o
5. Virtualization and Scheduling:
o A system with virtualization, even a single-CPU system, frequently acts like a
multiprocessor system.
o The virtualization software presents one or more virtual CPUs to each of the virtual
machines running on the system and then schedules the use of the physical CPUs among
the virtual machines.
o In general, most virtualized environments have one host operating system and many
guest operating systems.
o The host operating system creates and manages the virtual machines, and each virtual
machine has a guest operating system installed and applications running within that
guest.
o Depending on how busy the system is, the time slice may take a second or more,
resulting in very poor response times for users logged into that virtual machine.
o Commonly, virtual machines are incorrect because timers take longer to trigger than they
would on dedicated CPUs.
o Virtualization can thus undo the good scheduling-algorithm efforts of the operating
systems within virtual machines.

You might also like