Os Unit 1
Os Unit 1
An operating system (OS) is the program that, after being initially loaded into the computer
by a boot program, manages all of the other application programs in a computer. The
application programs make use of the operating system by making requests for services
through a defined application program interface (API)
● Convenient to use: One of the objectives is to make the computer system more
convenient to use in an efficient manner.
● User Friendly: To make the computer system more interactive with a more
convenient interface for the users.
● Easy Access: To provide easy access to users for using resources by acting as an
intermediary between the hardware and its users.
● Management of Resources: For managing the resources of a computer in a better
and faster way.
● Controls and Monitoring: By keeping track of who is using which resource,
granting resource requests, and mediating conflicting requests from different
programs and users.
● Fair Sharing of Resources: Providing efficient and fair sharing of resources
between the users and programs.
In Batch operating system, access is given to more than one person; they submit their
respective jobs to the system for the execution.
The system put all of the jobs in a queue on the basis of first come first serve and then
executes the jobs one by one. The users collect their respective output when all the jobs get
executed.
The purpose of this operating system was mainly to transfer control from one job to another
as soon as the job was completed. It contained a small set of programs called the resident
monitor that always resided in one part of the main memory. The remaining part is used for
servicing jobs.
Advantages of Batch OS
○ The use of a resident monitor improves computer efficiency as it eliminates CPU time
between two jobs.
Disadvantages of Batch OS
1. Starvation
For Example:
There are five jobs J1, J2, J3, J4, and J5, present in the batch. If the execution time of J1 is
very high, then the other four jobs will never be executed, or they will have to wait for a very
long time. Hence the other processes get starved.
2. Not Interactive
Batch Processing is not suitable for jobs that are dependent on the user's input. If a job
requires the input of two numbers from the console, then it will never get it in the batch
processing scenario since the user is not present at the time of execution.
Multiprogramming is an extension to batch processing where the CPU is always kept busy.
Each process needs two types of system time: CPU time and IO time.
In a multiprogramming environment, when a process does its I/O, The CPU can start the
execution of other processes. Therefore, multiprogramming improves the efficiency of the
system.
Advantages of Multiprogramming OS
○ Throughout the system, it increased as the CPU always had one program to execute.
○ Response time can also be reduced.
Disadvantages of Multiprogramming OS
In Multiprocessing, Parallel computing is achieved. There are more than one processors
present in the system which can execute more than one process at the same time. This will
increase the throughput of the system.
In Multiprocessing, Parallel computing is achieved. More than one processor present in the
system can execute more than one process simultaneously, which will increase the
throughput of the system.
Advantages of Multiprocessing operating system:
○ The multiple processors are busier at the same time to complete any task in a
multitasking environment, so the CPU generates more heat.
○ In this type of operating system, network traffic reduces due to the division between
clients and the server.
○ This type of system is less expensive to set up and maintain.
In Real-Time Systems, each job carries a certain deadline within which the job is supposed to
be completed, otherwise, the huge loss will be there, or even if the result is produced, it will
be completely useless.
The Application of a Real-Time system exists in the case of military applications, if you want
to drop a missile, then the missile is supposed to be dropped with a certain precision.
○ Easy to layout, develop and execute real-time applications under the real-time
operating system.
○ In a Real-time operating system, the maximum utilization of devices and systems.
In the Time Sharing operating system, computer resources are allocated in a time-dependent
fashion to several programs simultaneously. Thus it helps to provide a large number of user's
direct access to the main computer. It is a logical extension of multiprogramming. In time-
sharing, the CPU is switched among multiple programs given by different users on a
scheduled basis.
The Distributed Operating system is not installed on a single machine, it is divided into parts,
and these parts are loaded on different machines. A part of the distributed Operating system is
installed on each machine to make their communication possible. Distributed Operating
systems are much more complex, large, and sophisticated than Network operating systems
because they also have to take care of varying networking protocols.
When the first electronic computer was developed in 1940, it was created without any
operating system. In early times, users have full access to the computer machine and write a
program for each task in absolute machine language. The programmer can perform and solve
only simple mathematical calculations during the computer generation, and this calculation
does not require an operating system.
The first operating system (OS) was created in the early 1950s and was known as GMOS.
General Motors has developed OS for the IBM computer. The second-generation operating
system was based on a single stream batch processing system because it collects all similar
jobs in groups or batches and then submits the jobs to the operating system using a punch
card to complete all jobs in a machine. At each completion of jobs (either normally or
abnormally), control transfer to the operating system that is cleaned after completing one job
and then continues to read and initiates the next job in a punch card. After that, new machines
were called mainframes, which were very big and used by professional operators.
During the late 1960s, operating system designers were very capable of developing a new
operating system that could simultaneously perform multiple tasks in a single computer
program called multiprogramming. The introduction of multiprogramming plays a very
important role in developing operating systems that allow a CPU to be busy every time by
performing different tasks on a computer at the same time. During the third generation, there
was a new development of minicomputer's phenomenal growth starting in 1961 with the DEC
PDP-1. These PDP's leads to the creation of personal computers in the fourth generation.
SIMPLE STRUCTURE
It is the most straightforward operating system structure, but it lacks definition and is only
appropriate for usage with tiny and restricted systems. Since the interfaces and degrees of
functionality in this structure are clearly defined, programs are able to access I/O routines,
which may result in unauthorized access to I/O procedures.
○ There are four layers that make up the MS-DOS operating system, and each has its
own set of features.
○ These layers include ROM BIOS device drivers, MS-DOS device drivers, application
programs, and system programs.
○ The MS-DOS operating system benefits from layering because each level can be
defined independently and, when necessary, can interact with one another.
○ If the system is built in layers, it will be simpler to design, manage, and update.
Because of this, simple structures can be used to build constrained systems that are
less complex.
○ When a user program fails, the operating system as whole crashes.
○ Because MS-DOS systems have a low level of abstraction, programs and I/O
procedures are visible to end users, giving them the potential for unwanted access.
○ Because there are only a few interfaces and levels, it is simple to develop.
○ Because there are fewer layers between the hardware and the applications, it offers
superior performance.
○ The entire operating system breaks if just one user program malfunctions.
○ Since the layers are interconnected, and in communication with one another, there is
no abstraction or data hiding.
○ The operating system's operations are accessible to layers, which can result in data
tampering and system failure.
MONOLITHIC STRUCTURE
The monolithic operating system controls all aspects of the operating system's operation,
including file management, memory management, device management, and operational
operations.
The core of an operating system for computers is called the kernel (OS). All other System
components are provided with fundamental services by the kernel. The operating system and
the hardware use it as their main interface. When an operating system is built into a single
piece of hardware, such as a keyboard or mouse, the kernel can directly access all of its
resources.
The monolithic operating system is often referred to as the monolithic kernel. Multiple
programming techniques such as batch processing and time-sharing increase a processor's
usability. Working on top of the operating system and under complete command of all
hardware, the monolithic kernel performs the role of a virtual computer. This is an old
operating system that was used in banks to carry out simple tasks like batch processing and
time-sharing, which allows numerous users at different terminals to access the Operating
System.
○ Because layering is unnecessary and the kernel alone is responsible for managing all
operations, it is easy to design and execute.
○ Due to the fact that functions like memory management, file management, process
scheduling, etc., are implemented in the same address area, the monolithic kernel runs
rather quickly when compared to other systems. Utilizing the same address speeds up
and reduces the time required for address allocation for new processes.
○ The monolithic kernel's services are interconnected in address space and have an
impact on one another, so if any of them malfunctions, the entire system does as well.
○ It is not adaptable. Therefore, launching a new service is difficult.
LAYERED STRUCTURE
The OS is separated into layers or levels in this kind of arrangement. Layer 0 (the lowest
layer) contains the hardware, and layer 1 (the highest layer) contains the user interface
(layer N). These layers are organized hierarchically, with the top-level layers making use of
the capabilities of the lower-level ones.
The functionalities of each layer are separated in this method, and abstraction is also an
option. Because layered structures are hierarchical, debugging is simpler, therefore all lower-
level layers are debugged before the upper layer is examined. As a result, the present layer
alone has to be reviewed since all the lower layers have already been examined.
○ Work duties are separated since each layer has its own functionality, and there is
some amount of abstraction.
○ Debugging is simpler because the lower layers are examined first, followed by the top
layers.
MICRO-KERNEL STRUCTURE
The operating system is created using a micro-kernel framework that strips the kernel of any
unnecessary parts. Systems and user applications are used to implement these optional kernel
components. So, Micro-Kernels is the name given to these systems that have been developed.
Each Micro-Kernel is created separately and is kept apart from the others. As a result, the
system is now more trustworthy and secure. If one Micro-Kernel malfunctions, the remaining
operating system is unaffected and continues to function normally.
1. process management,
2. memory management,
4. file management
Process Management
The Operating system manages the processes, it assigns the processor to process the task ata
time,which is termed as process scheduling. To do this it uses different types of algorithms
like
● priority scheduling,
1. As the processes enter the system, they are queued under the job queues.
2. When the processes are ready to execute in the main memory, they are sent to ready
queue.
3. Processes that needs I/O device are sent to device queue.
Memory Management
The role of Memory Management is very important in operating system. It allocates memory
and switches the processes between disk and primary memory for execution whenever
required.
● it allocates memory to the processes by using some algorithms like best fit, first fit,
and worst fit
● All memory allocations are being tracked by the operating system, means what part of
memory are in use and what parts are empty.
● Operating System also deallocate memory from processes when process gets
terminated or if process does not require it.
Device Management
operating system handles many I/O devices such as mouse, keyboard, disk drive etc. To
handle a specific device, operating system can be connected to different device drivers.
Interface of the device and the device driver is device controller. All the I/O devices such as
mouse, keyboard, disk drive etc. can be accessed by user applications by using device
drivers.
File Management
To provide a uniform and systematic view of data storage, Files are used by the operating
system.
All the files are mapped to non volatile physical devices, in order to protect data loss in case
of system failure.
2. direct access
Sequential Access
In this Data in files are accessed or processed in serialised/sequential order.
some examples of file systems that uses sequential access are editors, compilers etc.
Direct Access
It is a Relative access ie. in this files are accessed in random order for read and write
operations.
an operating system will sit quietly, waiting for something to happen. Events are almost
always signalled by the occurrence of an interrupt or a trap.
● An interrupt service routine is provided that is responsible for dealing with the
interrupt. Since the operating system and the users share the hardware and software
resources of the computer system, we need to make sure that an error in a user
program could cause problems only for the one program that was running. With
sharing, many processes could be adversely affected by a bug in one program. For
example, if a process gets stuck in an infinite loop, this loop could prevent the correct
operation of many other processes.
● More subtle errors can occur in a multiprogramming system, where one erroneous
program might modify another program, the data of another program, or even the
operating system itself. Without protection against these sorts of errors, either the
computer must execute only one process at a time or all output must be suspect. A
properly designed operating system must ensure that an incorrect (or malicious)
program cannot cause other programs to execute incorrectly.
Dual-Mode Operation
In order to ensure the proper execution of the operating system, we must be able to
distinguish between the execution of operating-system code and user defined code. The
approach taken by most computer systems is to provide hardware support that allows us to
differentiate among various modes of execution. At the very least, we need two separate
modes of operation: user mode and kernel mode (also called supervisor mode, system
mode, or privileged mode). A bit, called the 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.k
● When the computer system is executing on behalf of a user application, the system is
in user mode.
● However, when a user application requests a service from the operating system (via a
system call), it must transition from user to kernel mode to fulfill the request. As
we shall see, this architectural enhancement is useful for many other aspects of system
operation as well.
● 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.
The dual mode of operation provides us with the means for protecting the operating system
from errant users—and errant users from one another. We accomplish this protection by
designating some of the machine instructions that may cause harm as privileged instructions.
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. The
instruction to switch to user mode is an example of a privileged instruction. Some other
examples include I/O control, timer management, and interrupt management. As we shall
see throughout the text, there are many additional privileged instructions.
We can now see the life cycle of instruction execution in a computer system. Initial control
is within 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. System calls
provide the means for a user program to ask the operating system to perform tasks reserved
for the operating system on the user program's behalf. A system call is invoked in a variety
of ways, depending on the functionality provided by the underlying processor. In all forms,
it is the method used by a process to request action by the operating system. A system call
usually takes the form of a trap to a specific location in the interrupt vector. This trap can be
executed by a generic trap instruction, although some systems (such as the MIPS R2000
family) have a specific syscall instruction.
When a system call is executed, it is treated by the hardware as a software interrupt. Control
passes through the interrupt vector to a service routine in the operating system, and the mode
bit is set to kernel mode. The system call service routine is a part of the operating system.
The kernel examines the interrupting instruction to determine what system call has occurred;
a parameter indicates what type of service the user program is requesting. Additional
information needed for the request may be passed in registers, on the stack, or in memory
(with pointers to the memory locations passed in registers). The kernel verifies that the
parameters are correct and legal, executes the request, and returns control to the instruction
following the system call. The lack of a hardware-supported dual mode can cause serious
shortcomings in an operating system.
For instance, MS-DOS was written for the Intel 8088 architecture, which has no mode bit
and therefore no dual mode. A user program running awry can wipe out the operating system
by writing over it with data; and multiple programs are able to write to a device at the same
time, with possibly disastrous results. Recent versions of the Intel CPU, such is the Pentium,
do provide dual-mode operation. Accordingly, most contemporary operating systems, such
as Microsoft Windows 2000 and Windows XP, and Linux and Solaris for x86 systems, take
advantage of this feature and provide greater protection for the operating system. Once
hardware protection is in place, errors violating modes are detected by the hardware. These
errors are normally handled by the operating system. If a user program fails in some
way—such as by making an attempt either to execute an illegal instruction or to access
memory that is not in the user's address space—then the hardware will trap to the operating
system. The trap transfers control through the interrupt vector to the operating system, just as
an interrupt does.
When a program error occurs, the operating system must terminate the program abnormally.
This situation is handled by the same code as is a user-requested abnormal termination. An
appropriate error message is given, and the memory of the program may be dumped. The
memory dump is usually written to a file so that the user or programmer can examine it and
perhaps correct it and restart the program.
Timer
We must ensure that the operating system maintains control over the CPU. We must
prevent a user program from getting stuck in an infinite loop or not calling system
services and never returning control to the operating system. To accomplish this goal, we
can use a timer. 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. For instance, a 10-bit counter
with a 1-millisecond clock allows interrupts at intervals from 1 millisecond to 1,024
milliseconds, in steps of 1 millisecond. Before turning over control to the user, the operating
system ensures that the timer is set to interrupt. If the timer interrupts, control transfers
automatically to the operating system, which may treat the interrupt as a fatal error or may
give the program more time. Clearly, instructions that modify the content of the timer are
privileged. Thus, we can use the timer to prevent a user program from running too long. A
simple technique is to initialize a counter with the amount of time that a program is allowed
to run. A program with a 7-minute time limit, for example, would have its counter initialized
to 420.
Every second, the timer interrupts and the counter is decremented by 1. As long as the
counter is positive, control is returned to the user program. When the counter becomes
negative, the operating system terminates the program for exceeding the assigned time limit.
Modes User Mode is considered as the Kernel mode is the system mode,
slave mode or the restricted master mode or the privileged
mode. mode.
Address In User mode, a process gets its In Kernel Mode, processes get a
Space own address space. single address space.
A process is defined as an entity which represents the basic unit of work to be implemented in
the system.
To put it in simple terms, we write our computer programs in a text file and when we execute
this program, it becomes a process which performs all the tasks mentioned in the program.
1 Stack
The process Stack contains the temporary data such as method/function parameters,
return address and local variables.
2 Heap
This is dynamically allocated memory to a process during its run time.
3 Text
This includes the current activity represented by the value of Program Counter and
the contents of the processor's registers.
4 Data
This section contains the global and static variables.
Program
#include <stdio.h>
int main() {
return 0;
}
A computer program is a collection of instructions that performs a specific task when
executed by a computer. When we compare a program with a process, we can conclude that a
process is a dynamic instance of a computer program.
When a process executes, it passes through different states. These stages may differ in
different operating systems, and the names of these states are also not standardized.
In general, a process can have one of the following five states at a time.
1 Start
This is the initial state when a process is first started/created.
2 Ready
The process is waiting to be assigned to a processor. Ready processes are waiting
to have the processor allocated to them by the operating system so that they can
run. Process may come into this state after Start state or while running it by but
interrupted by the scheduler to assign CPU to some other process.
3 Running
Once the process has been assigned to a processor by the OS scheduler, the process
state is set to running and the processor executes its instructions.
4 Waiting
Process moves into the waiting state if it needs to wait for a resource, such as
waiting for user input, or waiting for a file to become available.
5 Terminated or Exit
Once the process finishes its execution, or it is terminated by the operating system,
it is moved to the terminated state where it waits to be removed from main
memory.
1 Process State
The current state of the process i.e., whether it is ready, running, waiting, or
whatever.
2 Process privileges
This is required to allow/disallow access to system resources.
3 Process ID
Unique identification for each of the process in the operating system.
4 Pointer
A pointer to parent process.
5 Program Counter
Program Counter is a pointer to the address of the next instruction to be executed
for this process.
6 CPU registers
Various CPU registers where process need to be stored for execution for running
state.
9 Accounting information
This includes the amount of CPU used for process execution, time limits, execution
ID etc.
10 IO status information
This includes a list of I/O devices allocated to the process.
File System
A file is a collection of correlated information which is recorded on secondary or non-volatile
storage like magnetic disks, optical disks, and tapes. It is a method of data collection that is
used as a medium for giving input and receiving output from that program.
In general, a file is a sequence of bits, bytes, or records whose meaning is defined by the file
creator and user. Every File has a logical location where they are located for storage and
retrieval.
File structure
A File Structure needs to be predefined format in such a way that an operating system
understands. It has an exclusively defined structure, which is based on its type.
Three types of files structure in OS:
● A text file: It is a series of characters that is organized in lines.
File Attributes
A file has a name and data. Moreover, it also stores meta information like file creation date
and time, current size, last modified date, etc. All this information is called the attributes of a
file system.
Here, are some important File attributes used in OS:
● Identifier: Every file is identified by a unique tag number within a file system known
as an identifier.
● Type: This attribute is required for systems that support various types of files.
● Protection. This attribute assigns and controls the access rights of reading, writing,
and executing the file.
● Time, date and security: It is used for protection, security, and also used for
monitoring
File Type
It refers to the ability of the operating system to differentiate various types of files like text
files, binary, and source files. However, Operating systems like MS_DOS and UNIX has the
following type of files:
Character Special File
It is a hardware file that reads or writes data character by character, like mouse, printer, and
more.
Ordinary files
Directory Files
● Directory contains files and other related information about those files. Its basically a
folder to hold and organize multiple files.
Special Files
● These files are also called device files. It represents physical devices like printers,
disks, networks, flash drive, etc.
Functions of File
● Create file, find space on disk, and make an entry in the directory.
Field
This element stores a single value, which can be static or variable length.
DATABASE
Collection of related data is called a database. Relationships among elements of data are
explicit.
FILES
Files is the collection of similar record which is treated as a single entity.
RECORD
A Record type is a complex data type that allows the programmer to create a new data type
with the desired column structure. Its groups one or more columns to form a new data type.
These columns will have their own names and data type.
File Access Methods
File access is a process that determines the way that files are accessed and read into memory.
Generally, a single access method is always supported by operating systems. Though there
are some operating system which also supports multiple access methods.
Three file access methods are:
● Sequential access
Sequential Access
In this type of file access method, records are accessed in a certain pre-defined sequence. In
the sequential access method, information stored in the file is also processed one by one.
Most compilers access files using this access method.
Random Access
The random access method is also called direct random access. This method allow accessing
the record directly. Each record has its own address on which can be directly accessed for
reading and writing.
Sequential Access
This type of accessing method is based on simple sequential access. In this access method, an
index is built for every file, with a direct pointer to different memory blocks. In this method,
the Index is searched sequentially, and its pointer can access the file directly. Multiple levels
of indexing can be used to offer greater efficiency in access. It also reduces the time needed
to access a single record.
Space Allocation
● Linked Allocation
● Indexed Allocation
● Contiguous Allocation
Contiguous Allocation
In this method,
● Every file users a contiguous address space on memory.
Linked Allocation
In this method,
Indexed Allocation
In this method,
● An index block is created, having all the pointers for specific files.
● All files should have individual index blocks to store the addresses for disk space.
INTERPROCESS COMMUNICATION
2. Cooperating processes: Any process that does not share data with any other process is
independent. A process is cooperating if it can affect or be affected by the other processes
executing in the system. Clearly, any process that shares data with other processes is a cooperating
process.
There are several reasons for providing an environment that allows process cooperation:
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.
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).
Modularity. We may want to construct the system in a modular fashion, dividing the system
functions into separate processes or threads
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.
Cooperating processes require an interprocess communication (IPC) mechanism that will allow
them to exchange data and information. There are two fundamental models of interprocess
communication:
(1) shared memory
(2) message passing.
One solution to the producer-consumer problem uses shared memory. To allow producer and
consumer processes to run concurrently, we must have available a buffer of items that can be
filled by the producer and emptied by the consumer. This buffer will reside in a region of memory
that is shared by the producer and consumer processes. A producer can produce one item while the
consumer is consuming another item. The producer and consumer must be synchronized, so that
the consumer does not try to consume an item
that has not yet been produced.
Two types of buffers can be used. The UNBOUNDED BUFFER places no practical limit on the
size of the buffer. The consumer may have to wait for new items, but the producer can always
produce new items. The BOUNDED BUFFER assumes a 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.
MUTUAL EXCLUSION:
we assume that processes are numbered uniquely from 1 to n and that a one-to-one mapping exists
between processes and processors (that is, each process has its own processor).
2. Fully Distributed Approach: When a process g wants to enter its critical section, it
generates a new timestamp, TS, and sends the message request(P;, TS) to all processes in
the system (including itself). On receiving a request message, a process may reply
immediately (that is, send a reply message back to P; ), or it may defer sending a reply
back (because it is already in its critical section, for example). A process that has received
a reply message from all other processes in the system can enter its critical section,
queueing incmning requests and deferring them. After exiting its critical section, the
process sends reply messages to all its deferred requests.
● If process Pi is in its critical section, then it defers its reply to Pj. If process Pi does
not want to enter its critical section, then it sends a reply immediately to P j.
● If process P; wants to enter its critical section but has not yet entered it, then it
compares its own request timestamp with the timestamp of the incoming request
made by process Pj.
● If its own request timestamp is greater than that of the incoming request, then it
sends a reply immediately to Pj (Pj asked first). Otherwise, the reply is deferred.
We assume that the processes in the system are logically organized in a ring structure. The
physical communication network need not be a ring. As long as the processes are
connected to one another, it is possible to implement a logical ring. To implement mutual
exclusion, we pass the token around the ring. When a process receives the token, it may
enter its critical section, keeping the token. After the process exits its critical section, the
token is passed around again. If the process receiving the token does not want to enter its
critical section, it passes the token to its neighbor., but a token is substituted for a shared
variable. If the ring is unidirectional, freedom from starvation.
Two types of failure must be considered. First, if the token is lost an election must be
called to generate a new token. Second, if a process fails, a new logical ring must be
established.
SEMAPHORES:
The hardware-based solutions to the critical-section problem presented are complicated for
application programmers to use. To overcome this difficulty, we can use a synchronisation tool
called a SEMAPHORE.
A semaphore S is an integer variable that, apart from initialization, is accessed only through two
standard atomic operations: wait () and signal (). The wait () operation was originally termed P
(from the Dutch proberen, "to test"); signal() was originally called V (from verhogen, "to
increment"). The definition of wait () is as follows:
wait(S)
{
while S <= 0 II no-op:
s--·
}'
signal(S)
{
S++;
}
All modifications to the integer value of the semaphore in the wait () and signal() operations must
be executed indivisibly. That is, when one process modifies the semaphore value, no other process
can simultaneously modify that same semaphore value. In addition, in the case of wait (S), the
testing of the integer value of S (S :S 0), as well as its possible modification (S--), must be
executed without interruption
Operating systems often distinguish between counting and binary semaphores. The value of a
counting semaphore can range over an unrestricted domain. The value of a binary semaphore can
range only between 0 and 1.
We can use binary semaphores to deal with the critical-section problem £or multiple processes.
Then processes share a semaphore, mutex, initialised to 1. Each process Pi is organised. Counting
semaphores can be used to control access to a given resource consisting of a finite number o£
instances. The semaphore is initialised to the number of resources available. Each process that
wishes to use a resource performs a wait() operation on the semaphore (thereby decrementing the
count). When a process releases a resource, it performs a signal() operation (incrementing the
count). When the count for the semaphore goes to 0, all resources are being used. After that,
processes that wish to use a resource will block until the count becomes greater than 0.
The main disadvantage of the semaphore definition given here is thatit requires While a process
is in its critical section, any other process that tries to enter its critical section must loop
continuously in the entry code. This continual looping is clearly a problem in a real
multiprogramming system,where a single CPU is shared among ncany processes. Busy waiting
wastes CPU cycles that some other process might be able to use productively. This type of
semaphore is also called a SPINLOCK.because the process "spins" while waiting for the lock.
(Spinlocks do have an advantage in that no context switch is required when a process must wait on
a lock, and a context switch may take considerable time. Thus, when locks are expected to be held
for short times, spinlocks are useful; they are often employed on multiprocessor systems where
one thread can "spin" on one processor while another thread performs its critical section on
another processor.)
The block operation places a process into a waiting queue associated with the semaphore, and the
state of the process is switched to the waiting state. Then control is transferred to the CPU
scheduler, which selects another process to execute. A process that is blocked, waiting on a
semaphore S, should be restarted when some other process executes a signal() operation. The
process is restarted by a wakeup () operation, which changes the process from the waiting state to
the ready state. The process is then placed in the ready queue. (The CPU may or may not be
switched from the running process to the newly ready process, depending on the CPU-scheduling
algorithm.)
Each semaphore has an integer value and a list of processes list. When a process must wait on a
semaphore, it is added to the list of processes. A signal() operation removes one process from the
list of waiting processes and awakens that process. The wait() semaphore operation can now be
defined as
The block() operation suspends the process that invokes it. The wakeup(P) operation resumes the
execution of a blocked process P. These two operations are provided by the operating system as
basic system calls. It is critical that semaphores be executed atomically. We must guarantee that
no two processes can execute wait() and signal() operations on the same semaphore at the same
time. This is a critical-section problem; and in a single-processor environment (that is, where only
one CPU exists), we can solve it by simply inhibiting interrupts during the time the wait() and
signal() operations are executing. This scheme works in a single-processor environment because,
once interrupts are inhibited, instructions from different processes cannot be interleaved. Only the
currently running process executes until interrupts are reenabled and the scheduler can regain
control.
The entry section and exit section are enclosed in boxes to highlight these important segments of
code. A solution to the critical-section problem must satisfy the following three requirements:
1. Mutual exclusion. If process Pi is executing in its critical section, then no other processes can be
executing in their critical sections.
2. Progress. If no process is executing in its critical section and some processes wish to enter their
critical sections, then only those processes that are not executing in their remainder sections can
participate in deciding which will enter its critical section next, and this selection carmot be
postponed indefinitely.
3.Bounded waiting. There exists a bound, or limit, on the number of times that other processes are
allowed to enter their critical sections after a process has made a request to enter its critical section
and before that request is granted.
Two general approaches are used to handle critical sections in operating systems:
(1) preemptive kernels
(2) nonpreemptive kernels.
A preemptive kernel allows a process to be preempted while it is running in kernel mode. A
nonpreemptive kernel does not allow a process running in kernel mode to be preempted; a kernel-
mode process will run until it exits kernel mode, blocks, or voluntarily yields control of the CPU.
Obviously, a nonpreemptive kernel is essentially free from race conditions on kernel data
structures, as only one process is active in the kernel at a time. We cannot say the same about
preemptive kernels, so they must be carefully designed to ensure that shared kernel data are free
from race conditions. Preemptive kernels are especially difficult to design for SMP architectures,
since in these environments it is possible for two kernel-mode processes to run simultaneously on
different processors.
PROCESS SCHEDULING:
The process scheduler selects an available process (possibly from a set of several available
processes) for program execution on the CPU. For a single-processor system, there will never
be more than one running process. If there are more processes, the rest will have to wait until
the CPU is free and can be rescheduled.
Scheduling Queues:
● As processes enter the system, they are put into a job queue, which consists of all
processes in the system.
● The processes that are residing in main memory and are ready and waiting to execute
are kept on a list called the ready queue.This queue is generally stored as a linked
list. A ready-queue header contains pointers to the first and final PCBs in the list.
Each PCB includes a pointer field that points to the next PCB in the ready queue.
● When a process is allocated the CPU, it executes for a while and eventually quits, is
interrupted, or waits for the occurrence of a particular event, such as the completion of
an I/0 request. Suppose the process makes an I/O request to a shared device, such as a
disk. Since there are many processes in the system, the disk may be busy with the I/0
request of some other process. The process therefore may have to wait for the disk.
The list of processes waiting for a particular I/0 device is called a device queue. Each
device has its own device queue .
A new process is initially put in the ready queue. It waits there until it is selected for
execution, or is dispatched. Once the process is allocated the CPU and is executing, one of
several events could occur:
● The process could issue an I/0 request and then be placed in an I/0 queue.
● The process could create a new subprocess and wait for the subprocess's termination.
● The process could be removed forcibly from the CPU, as a result of an interrupt, and
be put back in the ready queue.
SCHEDULER:
A process migrates among the various scheduling queues throughout its lifetime. The
operating system must select, for scheduling purposes, processes from these queues in some
fashion. The selection process is carried out by the appropriate scheduler.
● The long-term scheduler, or job scheduler, selects processes from this pool and loads
them into memory for execution.The long-term scheduler executes much less
freqvently; minutes may separate the creation of one new process and the next. The
long-term scheduler
controls the degree of multiprogramming (the number of processes in memory). If the
degree of multiprogramming is stable, then the average rate of process creation must
be equal to the average departure rate of processes leaving the system. Thus, the long-
term scheduler may need to be invoked only when a process leaves the system.
Because of the longer interval between executions, the long-term scheduler can afford
to take more tin<e to decide which process should be selected for execution.
● The short-term scheduler, or CPU scheduler, selects from among the processes that
are ready to execute and allocates the CPU to one of them.The short-term scheduler
must select a new process for the CPU frequently. A process may execute for only a
few milliseconds before waiting for an I/0 request. Often, the short-term scheduler
executes at least once every 100 milliseconds. Because of the short time between
executions, the short-term scheduler must be fast. If it takes 10 milliseconds to decide
to execute a process for 100 milliseconds, then 10 I (100 + 10) = 9 percent of the CPU
is being used (wasted) simply for scheduling the work.
● The key idea behind a medium-term scheduler is that sometimes it can be advantageous to
remove processes from memory (and from active contention for the CPU) and thus reduce
the degree of multiprogramrning. 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. Swapping
may be necessary to improve the process mix or because a change in memory requirements
has overcommitted available memory, requiring memory to be freed up.
Context Switch:
When an interrupt occurs, the system needs to save the current CONTEXT of the process
running on the CPU so that it can restore that context when its processing is done,
essentially suspending the process and then resuming it. The context is represented in the
PCB of the process; it includes the value of the CPU registers, the process state and
memory-management information.
Switching the CPU to another process requires performing a state save of the current
process and a state restore of a different process. This task is known as a CONTEXT
SWITCH.When a context switch occurs, the kernel saves the context 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. Its speed
varies from machine to machine, depending on the memory speed, the number of registers
that must be copied, and the existence of special instructions (such as a single instruction
to load or store all registers). Typical speeds are a few milliseconds.
Scheduling criteria:
CPU utilization.
We want to keep the CPU as busy as possible. Conceptually, CPU utilization can range
from 0 to 100 percent. In a real system, it should range from 40 percent (for a lightly
loaded system) to 90 percent (for a heavily used system).
Throughput.
If the CPU is busy executing processes, then work is being done. One measure of work is
the number of processes that are completed per time unit, called throughput. For long
processes, this rate may be one process per hour; for short transactions, it may be ten
processes per second.
Turnaround time. From the point of view of a particular process, the an important
criterion is how long it takes to execute that process. The interval from the time of
submission of a process to the time of completion is the turnaround time. 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/0.
Waiting time. The CPU-scheduling algorithm does not affect the amount of time during
which a process executes or does I/0; it affects only the an1.ount of time that a process
spends waiting in the ready queue. Waiting time is the sum of the periods spent waiting in
the ready queue.
SCHEDULING ALGORITHM:
CPU scheduling deals with the problem of deciding which of the processes in the ready queue is
to be allocated the CPU.
On the negative side, the average waiting time under the FCFS policy is often quite long. 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
the FCFS scheduling algorithm is nonpreemptive. Once the CPU has been allocated to a process,
that process keeps the CPU until it releases the CPU, either by terminating or by requesting I/0.
The FCFS algorithm is thus particularly troublesome for time-sharing systems, where it is
important that each user get a share of the CPU at regular intervals. It would be disastrous to allow
one process to keep the CPU for an extended period.
Shortest-Job-First Scheduling:
A different approach to CPU scheduling is the shortest-job-first (SJF) scheduling algorithm. This
algorithm associates with each process the length of the process's next CPU burst. When the CPU
is available, it is assigned to the process that has the smallest next CPU burst. If the next CPU
bursts of two processes are the same, FCFS scheduling is used to break the tie. Note that a more
appropriate term for this scheduling method would be the shortest-next-CPU-burst algorithm,
because scheduling depends on the length of the next CPU burst of a process, rather than its total
length. We use the term SJF because most people and textbooks use this term to refer to this type
of scheduling.
The real difficulty with the SJF algorithm is knowing the length of the next CPU request. For
long-term (job) scheduling in a batch system, we can use as the length the process time limit that a
user specifies when he submits the job.
Priority Scheduling:
The SJF algorithm is a special case of the general priority scheduling algorithm. A priority is
associated with each process, and the CPU is allocated to the process with the highest priority.
Equal-priority processes are scheduled in FCFS order. An SJF algorithm is simply a priority
algorithm where the priority (p) is the inverse of the (predicted) next CPU burst. The larger the
CPU burst, the lower the priority, and vice versa.
In terms of high priority and low priority. Priorities are generally indicated by some fixed range
of numbers, such as 0 to 7 or 0 to 4,095. However, there is no general agreement on whether 0 is
the highest or lowest priority. Some systems use low numbers to represent low priority; others use
low numbers for high priority. This difference can lead to confusion. In this text, we assume that
low numbers represent high priority. As an example, consider the following set of processes,
assumed to have arrived at time 0 in the order P1, P2, · · ·, Ps, with the length of the CPU burst
given in milliseconds:
The average waiting time is 8.2 milliseconds.
Priorities can be defined either internally or externally. Internally defined priorities use some
measurable quantity or quantities to compute the priority of a process. For example, time limits,
memory requirements, the number of open files, and the ratio of average I/0 burst to average CPU
burst have been used in computing priorities. External priorities are set by criteria outside the
operating system, such as the importance of the process, the type and amount of funds being paid
for computer use, the department sponsoring the work, and other, often political factors
Round-Robin Scheduling:
The round-robin (RR) scheduling algorithm is designed especially for timesharing systems. It is
similar to FCFS scheduling, but preemption is added to enable the system to switch between
processes. A small unit of time, called a time quantum or time slice, is defined. A time quantum is
generally fronc 10 to 100 milliseconds in length. The ready queue is treated as a circular queue.
The CPU scheduler goes around the ready queue, allocating the CPU to each process for a time
interval of up to 1 time quantum.
To implement RR scheduling, we keep the ready queue as a FIFO queue o£ processes. New
processes are added to the tail of the ready queue. The CPU scheduler picks the first process from
the ready queue, sets a timer to interrupt after 1 time quantum, and dispatches the process. 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 o£ the ready
queue. The CPU scheduler will then select the next process in the ready queue.
The average waiting time under the RR policy is often long. Consider the following set of
processes that arrive at time 0, with the length of the CPU burst given in milliseconds:
of the process accordingly (Figure 5.4). Thus, we want the time quantum to be large with respect
to the context switch time. If the context-switch time is approximately 10 percent of the time
quantum, then about 10 percent of the CPU time will be spent in context switching. In practice,
most modern systems have time quanta ranging from 10 to 100 milliseconds. The time required
for a context switch is typically less than 10 microseconds; thus, the context-switch time is a small
fraction of the time quantum.
Multilevel Queue Scheduling:
A multilevel queue scheduling algorithm partitions the ready queue into several separate queues .
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. Each queue has its own
scheduling algorithm. 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.
Let's look at an example of a multilevel queue scheduling algorithm with five queues, listed below
in order of priority:
System processes
Interactive processes
Interactive editing processes
Batch processes
Student processes
Each queue has absolute priority over lower-priority queues. No process in the batch queue, for
example, could run unless the queues for system processes, interactive processes, and interactive
editing processes were all empty. If an interactive editing process entered the ready queue while a
batch process was running, the batch process would be preempted.
A process entering the ready queue is put in queue 0. A process in queue 0 is given a time
quantum of 8 milliseconds. If it does not finish within this time, it is moved to the tail of queue 1.
If queue 0 is empty, the process at the head of queue 1 is given a quantum of 16 milliseconds. If it
does not complete, it is preempted and is put into queue 2. Processes in queue 2 are run on an
FCFS basis but are run only when queues 0 and 1 are empty.
This scheduling algorithm gives highest priority to any process with a CPU burst of 8 milliseconds
or less. Such a process will quickly get the CPU, finish its CPU burst, and went off to its next I/0
burst. Processes that need more than 8 but less than 24 milliseconds are also served quickly,
although with lower priority than shorter processes. Long processes automatically sync to queue
2 and are served in FCFS order with any CPU cycles left over from queues 0 and 1.
In general, a multilevel feedback queue scheduler is defined by the
following parameters:
● The method used to determine which queue a process will enter when that process needs
service
Threads
The process model discussed so far has implied that a process is a program that performs a
single thread of execution. For example, when a process is running a word-processor
program, a single thread of instructions is being executed. This single thread of control allows
the process to perform only one task at one time. The user cannot simultaneously type in
characters and run the spell checker within the same process, for example. Many modern
operating systems have extended the process concept to allow a process to have multiple
threads of execution and thus to perform more than one task at a time. On a system that
supports threads, the PCB is expanded to include information for each thread. Other changes
throughout the system are also needed to support threads.
Multithreading
The term multithreading refers to an operating system's capacity to support execution among
fellow threads within a single process. The advantages of implementation of threads are
resource ownership (Address space and utilized file I/O) and execution (TCB and
scheduling). All threads inside a process will have to share compute resources such as code,
data, files, and memory space with its peer thread, but stacks and registers will not be shared,
and each new thread will have its own stacks and registers.
What is important to note here is that requests from one thread do not block requests from
other separate threads, which improves application responsiveness. The term 'multithreading'
also reduces the number of computing resources used and makes them more efficient. The
concept of multithreading is the event of a system executing many threads, with the execution
of these threads being of two types: Concurrent and Parallel multithread executions.
The concurrent process of threads is defined as the ability of a processor to move execution
resources between threads in a multithreaded process on a single processor. When each
thread in a multithreaded program executes on a separate processor at the same time, it is
referred to as parallel execution.
The threads of the same process share the following items during multithreading:
● Address space
● Global variables
● Accounting information
● Opened files, used memory and I/O
● Child processes
● Pending alarms
● Signal and signal handlers
The following things are private (not shared) to the individual thread of a process in
multithreading.
● Improves execution speed (by combining CPU machine and I/O wait times).
● Multithreading can be used to achieve concurrency.
● Reduces the amount of time it takes for context switching.
● Improves responsiveness.
● Make synchronous processing possible (separates the execution of independent
threads).
● Increases throughput.
● Performs foreground and background work in parallel.
Disadvantages of Multithreading
● Because the threads share the same address space and may access resources such as
open files, difficulties might arise in managing threads if they utilize incompatible
data structures.
● If we don't optimize the number of threads, instead of gaining performance, we may
lose performance.
● If a parent process requires several threads to function properly, the child processes
should be multithreaded as well, as they may all be required.