OS ALL UNITS MATxtdtc5ctc5cycgctERIAL.pptx

kanirudhsingh2357 334 views 178 slides Aug 30, 2025
Slide 1
Slide 1 of 274
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26
Slide 27
27
Slide 28
28
Slide 29
29
Slide 30
30
Slide 31
31
Slide 32
32
Slide 33
33
Slide 34
34
Slide 35
35
Slide 36
36
Slide 37
37
Slide 38
38
Slide 39
39
Slide 40
40
Slide 41
41
Slide 42
42
Slide 43
43
Slide 44
44
Slide 45
45
Slide 46
46
Slide 47
47
Slide 48
48
Slide 49
49
Slide 50
50
Slide 51
51
Slide 52
52
Slide 53
53
Slide 54
54
Slide 55
55
Slide 56
56
Slide 57
57
Slide 58
58
Slide 59
59
Slide 60
60
Slide 61
61
Slide 62
62
Slide 63
63
Slide 64
64
Slide 65
65
Slide 66
66
Slide 67
67
Slide 68
68
Slide 69
69
Slide 70
70
Slide 71
71
Slide 72
72
Slide 73
73
Slide 74
74
Slide 75
75
Slide 76
76
Slide 77
77
Slide 78
78
Slide 79
79
Slide 80
80
Slide 81
81
Slide 82
82
Slide 83
83
Slide 84
84
Slide 85
85
Slide 86
86
Slide 87
87
Slide 88
88
Slide 89
89
Slide 90
90
Slide 91
91
Slide 92
92
Slide 93
93
Slide 94
94
Slide 95
95
Slide 96
96
Slide 97
97
Slide 98
98
Slide 99
99
Slide 100
100
Slide 101
101
Slide 102
102
Slide 103
103
Slide 104
104
Slide 105
105
Slide 106
106
Slide 107
107
Slide 108
108
Slide 109
109
Slide 110
110
Slide 111
111
Slide 112
112
Slide 113
113
Slide 114
114
Slide 115
115
Slide 116
116
Slide 117
117
Slide 118
118
Slide 119
119
Slide 120
120
Slide 121
121
Slide 122
122
Slide 123
123
Slide 124
124
Slide 125
125
Slide 126
126
Slide 127
127
Slide 128
128
Slide 129
129
Slide 130
130
Slide 131
131
Slide 132
132
Slide 133
133
Slide 134
134
Slide 135
135
Slide 136
136
Slide 137
137
Slide 138
138
Slide 139
139
Slide 140
140
Slide 141
141
Slide 142
142
Slide 143
143
Slide 144
144
Slide 145
145
Slide 146
146
Slide 147
147
Slide 148
148
Slide 149
149
Slide 150
150
Slide 151
151
Slide 152
152
Slide 153
153
Slide 154
154
Slide 155
155
Slide 156
156
Slide 157
157
Slide 158
158
Slide 159
159
Slide 160
160
Slide 161
161
Slide 162
162
Slide 163
163
Slide 164
164
Slide 165
165
Slide 166
166
Slide 167
167
Slide 168
168
Slide 169
169
Slide 170
170
Slide 171
171
Slide 172
172
Slide 173
173
Slide 174
174
Slide 175
175
Slide 176
176
Slide 177
177
Slide 178
178
Slide 179
179
Slide 180
180
Slide 181
181
Slide 182
182
Slide 183
183
Slide 184
184
Slide 185
185
Slide 186
186
Slide 187
187
Slide 188
188
Slide 189
189
Slide 190
190
Slide 191
191
Slide 192
192
Slide 193
193
Slide 194
194
Slide 195
195
Slide 196
196
Slide 197
197
Slide 198
198
Slide 199
199
Slide 200
200
Slide 201
201
Slide 202
202
Slide 203
203
Slide 204
204
Slide 205
205
Slide 206
206
Slide 207
207
Slide 208
208
Slide 209
209
Slide 210
210
Slide 211
211
Slide 212
212
Slide 213
213
Slide 214
214
Slide 215
215
Slide 216
216
Slide 217
217
Slide 218
218
Slide 219
219
Slide 220
220
Slide 221
221
Slide 222
222
Slide 223
223
Slide 224
224
Slide 225
225
Slide 226
226
Slide 227
227
Slide 228
228
Slide 229
229
Slide 230
230
Slide 231
231
Slide 232
232
Slide 233
233
Slide 234
234
Slide 235
235
Slide 236
236
Slide 237
237
Slide 238
238
Slide 239
239
Slide 240
240
Slide 241
241
Slide 242
242
Slide 243
243
Slide 244
244
Slide 245
245
Slide 246
246
Slide 247
247
Slide 248
248
Slide 249
249
Slide 250
250
Slide 251
251
Slide 252
252
Slide 253
253
Slide 254
254
Slide 255
255
Slide 256
256
Slide 257
257
Slide 258
258
Slide 259
259
Slide 260
260
Slide 261
261
Slide 262
262
Slide 263
263
Slide 264
264
Slide 265
265
Slide 266
266
Slide 267
267
Slide 268
268
Slide 269
269
Slide 270
270
Slide 271
271
Slide 272
272
Slide 273
273
Slide 274
274

About This Presentation

4dtxyf


Slide Content

Presented by D. Sai Kiran OPERATING SYSTEMS

UNIT-I OPERATING SYSTEM Operating System - Introduction, Structures - Simple Batch, Multi-programmed, Time-shared, Personal Computer, Parallel, Distributed Systems, Real-Time Systems, System components, Operating System services, System Calls Process - Process concepts and scheduling, Operations on processes, Cooperating Processes, Threads UNIT-II CPU SCHEDULING CPU Scheduling - Scheduling Criteria, Scheduling Algorithms, Multiple -Processor Scheduling. System call interface for process management-fork, exit, wait, waitpid, exec Deadlocks - System Model, Deadlocks Characterization, Methods for Handling Deadlocks, Deadlock Prevention, Deadlock Avoidance, Deadlock Detection, and Recovery from Deadlock UNIT-III PROCESS MANAGEMENT AND SYNCHRONIZATION Process Management and Synchronization - The Critical Section Problem, Synchronization Hardware, Semaphores, and Classical Problems of Synchronization, Critical Regions, Monitors Inter process Communication Mechanisms: IPC between processes on a single computer system, IPC between processes on different systems, using pipes, FIFOs, message queues, shared memory.

UNIT-IV MEMORY MANAGEMENT AND VIRTUAL MEMORY Memory Management and Virtual Memory - Logical versus Physical Address Space, Swapping, Contiguous Allocation, Paging, Segmentation, Segmentation with Paging, Demand Paging, Page Replacement, Page Replacement Algorithms. UNIT-V FILE SYSTEM INTERFACE AND OPERATIONS File System Interface and Operations -Access methods, Directory Structure, Protection, File System Structure, Allocation methods, Free-space Management. Usage of open, create, read, write, close, lseek , stat, ioctl system calls.

UNIT - I UNIT-I OPERATING SYSTEM Operating System - Introduction, Structures - Simple Batch, Multi-programmed, Time-shared, Personal Computer, Parallel, Distributed Systems, Real-Time Systems, System components, Operating System services, System Calls Process - Process concepts and scheduling, Operations on processes, Cooperating Processes, Threads

An Operating System (OS) is a software that acts as an interface between computer hardware components and the user. An operating system manages the computer hardware. Every computer system must have at least one operating system to run other programs. User Operating System Hardware INTRODUCTION

The o perating system helps you to communicate with the computer without knowing how to speak the computer’s language. It is not possible for the user to use any computer or mobile device without having an operating system. Some examples of operating systems are UNIX, MS-DOS, MS-Windows, Windows/NT, MacOS etc. O perating system coordinates the individual components of the computer to make the programs execute properly. O perating system c controls the computer hardware, manages the system resources and supervises the interaction between the computer system and user. O perating system forms a base on which the application software can be developed.

OS perform different tasks like keeping track of files on directories on the disk, identifying the input from the keyboard and mouse, sending an output to the external devices such as printer and monitor etc. The goals of OS are: To use hardware in efficient manner. To make the computer system convenient to use.

Bootstrapping the OS OS sometimes described as first program, that allows you to run other programs. The OS is loaded through the bootstrapping process, known as booting. A bootstrap is the program that initializes the OS during startup. Bootstrap program is a part of ROM, OS is loaded into RAM by bootstrap program after the start of the computer system, the OS starts the device drivers OS Devices Bootstrap Program RAM Read OS OS ROM Load OS Start Device Drivers

Objectives and Functions Usability : Convenient to use. Efficiency : Efficient usage of system resource. Maintainability : Ability to evolve.

Usability OS must interacts with the hardware resources and provide services to the end users. The services provided are of two types: Services directly available for the end user through the I/O devices such as mouse, keyboard, printer.. Services for application programs. OS Computer Hardware Utilities App. Programs Users

Efficiency O perating system must utilize the computer system resource in efficient manner. O perating system needs CPU time to execute instructions. By utilizing the facilities provided by hardware , the O perating system may schedule different processes to run at a different moments.

Maintainability OS must evolve due to following reasons: Hardware upgrades or new types of hardware. New Services. Fixes.

Functions of OS The major functions of a typical OS are to: Load application programs for execution. Provide services. Enable time sharing, in which many application programs seem to be running simultaneously. Enable virtual memory operations for application programs, by coordinating with hardware operations. Maintain the file system. Manage I/O, including networking, protection and security.

Basic Elements Four main elements are: 1. Processor: It controls the operation of a computer and performs its data processing functions like arithmetic, logical and others. 2. Main Memory: It is called as volatile memory, primary memory, real memory or temporary memory, because it stores data and programs temporarily during the processing time only. 3. Input/Output modules: Move data between the computer and its external environment like memory communication equipment and terminals etc. 4. System Inter Connection: Provides some structure and mechanisms that provide for communication processors, main memory and Input/Output modules.

HISTORY OF OS Operating systems were first developed in the late 1950s to manage tape storage. The General Motors Research Lab implemented the first OS in the early 1950s for their IBM 701. In the mid-1960s, operating systems started to use disks. In the late 1960s, the first version of the Unix OS was developed. The first OS built by Microsoft was DOS. It was built in 1981 by purchasing the 86-DOS software from a Seattle company. The present-day popular OS Windows first came to existence in 1985 when a GUI was created and paired with MS-DOS.

Structures of Operating System S tructures of the operating system: Simple Structure Layered Approach Structure Micro-Kernel Structure Exo-Kernel Structure Virtual Machines

Simple Structure The interfaces and levels of functionality are not well separated. MS-DOS is an example of such operating system. such operating systems do not have well defined structure and are small, simple and limited systems. MS-DOS is an example of such operating system. In MS-DOS application programs are able to access the basic I/O routines. These types of operating system cause the entire system to crash if one of the user programs fails.

ADVANTAGES OF SIMPLE STRUCTURE: It delivers better application performance because of the few interfaces between the application program and the hardware. Easy for kernel developers to develop such an operating system. DISADVANTAGES OF SIMPLE STRUCTURE: The structure is very complicated as no clear boundaries exists between modules. It does not enforce data hiding in the operating system.

LAYERED STRUCTURE In this structure the OS 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. These layers are so designed that each layer uses the functions of the lower level layers only. This simplifies the debugging process. The main disadvantage of this structure is that at each layer, the data needs to be modified and passed on which adds overhead to the system.

ADVANTAGES OF LAYERED STRUCTURE: Layering makes it easier to enhance the operating system as implementation of a layer can be changed easily without affecting the other layers. It is very easy to perform debugging and system verification. DISADVANTAGES OF LAYERED STRUCTURE: In this structure the application performance is degraded as compared to simple structure. It requires careful planning for designing the layers as higher layers use the functionalities of only the lower layers.

MICRO-KERNEL STRUCTURE Micro-Kernel structure designs the Operating System by removing all non-essential components of the kernel. These non-essential components of kernels are implemented as systems and user programs. Hence these implemented systems are called as Micro-Kernels. Each Micro-Kernel is made independently and is isolated from other Micro-Kernels. So this makes the system more secure and reliable. If any Micro-Kernel fails, then the remaining operating System remains untouched and works fine.

ADVANTAGES OF MICRO-KERNEL STRUCTURE: It makes the operating system portable to various platforms. As microkernels are small so these can be tested effectively. DISADVANTAGES OF MICRO-KERNEL STRUCTURE: Increased level of inter module communication degrades system performance.

MONOLITHIC STRUCTURE The Monolithic operating System in which the kernel acts as a manager by managing all things like file management, memory management, device management, and operational processes of the Operating System. The kernel is the heart of a computer operating system (OS). Kernel delivers basic services to all other elements of the System. It serves as the primary interface between the Operating System and the hardware. In monolithic systems, kernels can directly access all the resources of the operating System like physical hardware, exp Keyboard, Mouse etc.

ADVANTAGES OF MONOLITHIC STRUCTURE: It is simple to design and implement because all operations are managed by kernel only, and layering is not needed. As services such as memory management, file management, process scheduling, etc., are implemented in the same address space, the execution of the monolithic kernel is relatively fast as compared to normal systems. Using the same address saves time for address allocation for new processes and makes it faster. DISADVANTAGES OF MONOLITHIC STRUCTURE: If any service in the monolithic kernel fails, the entire System fails because, in address space, the services are connected to each other and affect each other. It is not flexible, and to introduce a new service

TYPES OF OPERATING SYSTEM Following are the popular types of OS (Operating System): Batch Operating System Multi programming Operating System Time-sharing Operating System Personal Computer Operating System Parallel Operating System Distributed Operating System Real-Time Operating System Network Operating System Multi-Processing, Multi-Tasking and Multi-User Systems

Batch Operating System Some computer processes are very lengthy and time-consuming. To speed the same process, a job with a similar type of needs are batched together and run as a group. People were used to having a single computer which was called a mainframe. 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 central idea was to use apiece of software know as monitor .  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.

To process user programs, the monitor first had to loaded into memory. Then it read programs one at a time for the input devices. As each program was read, it would be placed in the user program area of main memory, and control passed to this program. When the execution of the program was completed it retuned control to the monitor, which moved on to process the next program. A punched card is a piece of card that stores digital data using punched holes. Punched Cards Card Reader Tape Driver CPU Linear Printer Input Tape Output Tape

Programmers submit punched cards containing their program. Card reader reads them onto a magnetic tape. Operator loads a special program called monitor and runs the first job. The Operating system automatically reads the next job and runs it. The output of every job is written onto the output tape. On completion od the entire batch, the output tape is taken away for printing offline.

Advantages of Batch OS Transfers work of the operator to the computer. Increased performance since it was possible for job to start as soon as the previous job finished. Simple scheduling. The use of a resident monitor improves computer efficiency as it eliminates CPU time between two jobs. Disadvantages of Batch OS Starvation, b atch processing suffers from starvation. Due to lack of protection scheme, one batch job can effect pending jobs. A job could corrupt the monitor, thus effecting pending jobs. Not Interactive.

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. 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.

Multi programming Operating System 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. One of the primary goals of multi programmed systems is resource sharing. In multi programming environment the OS rapidly switches the processor from one job to other job. 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 CPU never becomes idle. Efficient resources utilization. Response time can also be reduced. Disadvantages of Multiprogramming OS CPU scheduling is required. Long time jobs have to wait long. Requires efficient memory management. Tracking all processes sometimes difficult. User interaction not possible during program execution.

Time-sharing Operating System Time-sharing enables many people, located at various terminals, to use a particular computer system at the same time. Processor’s time is shared among multiple users simultaneously is termed as time-sharing. 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.

Example: Imagine you have a computer lab at college with only one computer. All your classmates want to use it to do their homework. Instead of waiting for each person to finish, the teacher decides to use a Time-Sharing system. Each student gets a turn to use the computer for 10 minutes. When the 10 minutes are up, the computer automatically switches to the next student's turn. This way, everyone gets a chance to use the computer, and nobody has to wait too long. So, in a Time-Sharing system, it's like everyone in the class gets a fair share of computer time, even though there's only one computer available

A time-sharing operating system allows many users to be served simultaneously, so sophisticated CPU scheduling schemes and Input/output management are required. Advantages of Time Sharing Operating System The time-sharing operating system provides effective utilization and sharing of resources. This system reduces CPU idle and response time. Disadvantages of Time Sharing Operating System Data transmission rates are very high in comparison to other methods. Time-sharing operating systems are very difficult and expensive to build.

Distributed Operating System Distributed Operating System is a type of model where applications are running on multiple computers linked by communications. It is an extension of the network operating system which supports higher levels of communication and integration of the machines on the network. Distributed OS runs on multiple CPUs but for an end-user, it is just an ordinary centralized operating system. It can share all resources like CPU, disk, network interface, nodes, computers, etc. from one site to another site, and it increases the data available on the entire system. All processors are connected by valid communication media such as high-speed buses and telephone lines, and in which every processor contains its own local memory along with other local processors. According to this nature, a distributed operating system is known as a loosely coupled system. This operating system involves multiple computers, nodes, and sites, and these components are linked to each other with LAN/WAN lines.

Advantages of Distributed Operating System The distributed operating system provides sharing of resources. This type of system is fault-tolerant. Disadvantages of Distributed Operating System Protocol overhead can dominate computation cost. Since the OS is complex, implementation is difficult. The main objective of Distributing OS: Resource sharing Load sharing Reliability Communication

Real Time Operating System 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. Therefore the main objective of a real time system is to provide quick response whereas resource utilization takes a back seat. Real-Time Operating Systems also commonly appear in embedded systems, which are a combination of hardware and software designed for a specific function. Real-Time Operating Systems are designed to handle multiple processes at one time, ensuring that these processes respond to events within a predictable time limit.

Real-Time Operating Systems are included in the following devices: Air traffic control systems Military applications Anti-lock brakes and air bags Cameras Medical systems and PCs.

Characteristics of real-time systems Memory management: In real time operating systems memory management is not so important since most of the time processes remain in main memory to provide quick response. Stability : Even when the load is very heavy, real-time systems respond in the time constraint i.e. real-time systems does not delay the result of tasks even when there are several task going on a same time. This brings the stability in real-time systems. Correctness : Correctness is one of the prominent part of real-time systems. Real-time systems produce correct result within the given time interval. If the result is not obtained within the given time interval then also result is not considered correct. In real-time systems, correctness of result is to obtain correct result in time constraint. Safety : Safety is necessary for any system but real-time systems provide critical safety. Real-time systems also can perform for a long time without failures. It also recovers very soon when failure occurs in the system and it does not cause any harm to the data and information.

Advantages Memory management is less demanding File management speeds up the access of files Resource management is less important. Disadvantages Real-time operating systems are very costly to develop. Real-time operating systems are very complex.

Personal Computer Operating System A personal computer's operating system provides a good interface for a single user for word processing, spreadsheets, and Internet access Laptops, computer systems, tablets, etc. are your personal computers, and the operating systems such as Windows, iOS , Android, Ubuntu etc. are your personal computer operating systems.

And you can use your personal computer operating system for your personal purposes, for example, chatting with your friends using some social media sites, reading some articles from the internet, making some projects through Microsoft PowerPoint or any other program, designing your website, programming something, watching some videos and movies, listening to some songs, and many more. A dvantages of Personal Computers Education Entertainment Communication Information Disadvantages of Personal Computers Physical Side Effects Internet Addiction

Parallel Operating System The parallel processing system is designed to speed up the execution process of programs by dividing them into multiple segments and processing them simultaneously. These systems are called multiprocessor systems or tightly coupled systems due to high degree of resource sharing.. It is used for dealing with multiple processors simultaneously by using computer resources. Such system contains two or more CPUs sharing the computer resources.

Functions of Parallel Operating System Has a multiprocessing environment. Security among processes. The parallel OS can handle the load of tasks. Sharing of resources between other processes. Avoiding interference of other processes or threads with each other. Efficient utilization of all the resources.

Asymmetric Multi-processor Each processor is assigned a particular task. A master processor controls the system. Each processor has its own local resources that are not accessible by other processors including memory, disk storage and other peripherals. The other processors either have pre-defined tasks or wait for instructions from the master processor. Therefore the scheme is known as Master-Slave relationship. Only the master processor schedules and allocates work to the slave processors.

Symmetric Multi-processor (SMP) They have multiple CPUs, all with access to the same memory. Each processor performs all tasks within the operating system. There is no master-slave relationship Each processor will share the same physical memory. Example of SMP system is Solaris (commercial version of UNIX) designed by Sun Microsystems.

Advantages of Parallel Operating System It saves time and allows the execution of applications simultaneously. Increased Throughput (The number of processes completed per unit of time) Increased Reliability ( The ability for a system to remain available over a period of time) Multiple resources can be used simultaneously. Has a larger memory for the allocation of resources and tasks. Faster as compared to another operating system. Scaling. Disadvantages of Parallel Operating System The architecture of the parallel operating system is complex. High cost since more resources are used for synchronization, data transfer, thread, and communication. In the case of clusters, better cooling techniques are required. Huge power consumption. High maintenance.

Network Operating System A Network Operating System is a collection of programs and associated protocols which allow a set of computers interconnected by a computer network, to be used together conveniently. In this kind of operating systems, the users can log in to remote machines. They can also copy files from one machine to another. Features of Network Operating System : Allows users to access the various resources on the network. Controls access, so that only users with proper authorization can access particular resources. Make usage of remote resources appear like local resources.

Advantages of Network Operating Systems Highly stable due to central server. Provide good security. Upgradation of new technology and hardware can be easily implemented in the network. Provide remote access to servers from different locations. Disadvantages of Network Operating Systems High cost to buying server. Regular updating and maintenance are required.

Multi-Processing, Multi-Tasking and Multi-User Systems Multi-processing: Multiple CPUs perform more than one job at a time. However in Multi-programming a single CPU performs more than one job at a time. Multi-tasking: Multi-tasking provides many services to the user as well as the program it runs. Some of the important services are resource management, memory and I/O device Management. For example, while your program is running, pop up a calculator, use it and go back to your program. Multi-user: Multi-user system allows simultaneous access to a computer through multiple terminals. i.e., several users (for example in a railway reservation system) can access the same program from different terminals However in time sharing system, a single CPU serves many users at interactive terminals working on different jobs. Thus the evolution of the modern operating systems started from batch systems and continued to evolve with time sharing, multi-programming, personal computer, embedded systems (real time) and network of computers

Modern OS Time Sharing Batch Personal Computer Network OS Embedded OS Small Computer Protection Scheduling, Files Memory Management Memory Management Scheduling, Protection System Software Human – Computer Interface Client Server Model Real Time Scheduling Network Storage Resource Management

Ke rnel is basically the core and the heart of an OS (Operating system). It is the active part of Operating System running at all time. It is a programs which can interact with the hardware. Ex: Device driver, dll files, system files etc. Shell is basically an interface present between the kernel and the user. It is called as the command interpreter. It is a set of programs used to interact with the application programs. It is responsible for execution of instructions given to OS.

Components of a computer system. CPU, Disk Controller, USB Controller, Graphics Adaptor and Memory. User view and System view User View: From the user’s point view, the OS is designed for one user to monopolize its resources, to maximize the work that the user is performing and for ease of use. System View: From the computer's point of view, an operating system is a control program that manages the execution of user programs to prevent errors and improper use of the computer.

INSTRUCTION EXECUTION A program to be executed by a processor consists a set of instructions stored in memory. Instruction processing consists of two steps: Fetch : The processor reads (fetches) instructions from memory one at a time. Execute : Program execution consists of repeating the process of instruction fetch and instruction execution. Instruction execution may involve several operations and depends on the nature of the instruction. The processing required for a completion of single instruction is called an instruction cycle. 1. The processor fetches an instruction from memory. 2. The fetched instruction is loaded into the instruction register(R). 3. The instruction contains the bits that specify the action that the processor is to take. 4. The processor interprets the instruction and performs the required action.

In general, these actions fall into four categories: Processor-memory: Data may be transferred from processor to memory or from memory to processor. Processor-I/O: Data may be transferred to or from a peripheral device transferring between the processor and an I/O module. Data processing: The processor may perform some arithmetic or logic operation on data. Control: An instruction may specify that the sequence of execution be altered. For example the processor may fetch an instruction from location 149 which specifies that the next instruction will be from location 182. START Halt Fetch Next Instruction Execution Instruction Fetch Stage Execute Stage Instruction execution cycle

OPERATING SYSTEM COMPONENTS Process Management Main Memory Management File Management Input-Output System Management Secondary Storage Management Networking Protection System Command Interpreter System

Process Management A process is a program in execution. A process needs certain resources, including CPU time, memory, files, and I/O devices, to accomplish its task. The operating system is responsible for the following activities in connection with process management. Process creation and deletion. P rocess suspension and resumption. Provision of mechanisms for: ( i ) Process S ynchronization (ii) Process C ommunication

Main Memory Management Memory is a large array of words or bytes, each with its own address. It is a repository of quickly accessible data shared by the CPU and I/O devices. Main memory is a volatile storage device. It loses its contents in the case of system failure. The operating system is responsible for the following activities in memory management: ♦ Keep track of which parts of memory are currently being used and by whom. ♦ Decide which processes to load when memory space becomes available. ♦ Allocate and de-allocate memory space as needed.

File Management A file is a collection of related information defined by its creator. Commonly, files represent programs(both source and object forms) and data. Data files can be alphabetic, numeric, or alphanumeric. The operating system is responsible for the following activities in connections with file management: ✦ File creation and deletion. ✦ Directory creation and deletion. ✦ File backup on stable (non volatile) storage media.

Input-Output System Management The I/O system consists of: ✦ A buffer-caching system ✦ A general device-driver interface ✦ Drivers for specific hardware devices

Secondary Storage Management Since main memory (primary storage) is volatile and too small to accommodate all data and programs permanently, the computer system must provide secondary storage to back up main memory. Today modern computers use hard drives/SSD as the primary storage of both programs and data. The operating system is responsible for the following activities in connection with disk management: ✦ Free space management ✦ Storage allocation ✦ Disk scheduling

Networking (Distributed Systems): Network management is the process of administering and managing computer networks. It includes performance management, provisioning of networks, fault analysis, and maintaining the quality of service. A distributed system is a collection processors that do not share memory or a clock. Each processor has its own local memory. ♦ The processors in the system are connected through a communication network. ♦ Communication takes place using a protocol. ♦ A distributed system provides user access to various system resources. Access to a shared resource allows: Computation speed-up Increased data availability Enhanced reliability

Protection System Protection refers to a mechanism for controlling access by programs, processes, or users to both system and user resources. The protection mechanism must: ✦ Distinguish between authorized and unauthorized usage. ✦ Specify the controls to be imposed.

Command Interpreter System One of the most important components of an operating system is its command interpreter. The command interpreter is the primary interface between the user and the rest of the system. A command interpreter allows the user to interact with a program using commands in the form of text lines. It was frequently used until the 1970’s. However, in modern times many command interpreters are replaced by graphical user interfaces and menu-driven interfaces.

OPERATING SYSTEMS SERVICES Most operating systems provide certain services to the users which vary from one operating system to the other. However, these services make programming task easy and convenient to the user. The operating system services are of two types: User View: One set of operating-system services provide functions helpful to users. System View: Another set of operating system functions exist, which are used to ensure the efficient operation of the system itself (and not for helping user).

User View 1. User Interface Most operating systems have User Interface (UI). This interface can take several forms. One is a Command-Line Interface (CLD), which uses text commands and a method for entering them. Another is a Batch Interface. in which commands and directives are entered into files, to control those commands Most common interface used is a Graphical User Interface (GUI). Here the interface is Window system with a pointing device to direct I/O, choose from menus, and make selections, and a keyboard to enter text.

2. Program Execution System should load a program into memory and run it. Program should terminate normally or abnormally (with error indication). 3. I/O Operations For a running program requiring I/O operations, the system must be able to provide and efficiently control O/I devices. 4. File-System Manipulation The operating system should provide facilities to read, write, create delete files by name. Some programs include permission management to allow or deny access to files or directories.

5. Communications Communications are of two types. Between processes executing on the same computer. Between processes executing on different computers connected by a network. Communication can be implemented by the operating system by Shared memory Technique of message passing where packets of information are exchanged between processes. 6. Error Detection The operating system must be able to detect any kind of error and take proper action to correct the errors. Some of the common types of errors are: 1. Power failure 2. Memory error 3. Parity error in I/O devices 4. Out of paper in printer 5. Connection failure on a network 6. Arithmetic overflow in a user program

System View 1. Resources Allocation: Resource allocation is a very important service provided by the operating system when multiple users are working or multiple jobs are running at the same time Resources such as CPU, main memory, file storage and IO devices must be managed by the operating system. 2. Accounting: Accounting of : Which users? How many users? What kind of computer resources? helps to charge the user for using the resources. Further these usage statistics are available for improving the computing services.

3. Protection and Security: Protection plays an important role when several processes are executing concurrently. It must ensure that one process does not interfere with another and all access to system resources are controlled. Security of system is also important. Security is implemented by each user logging in using a password. Only then user is allowed to access resources such as I/O devices, modems etc. Security ensures that unauthorized access is restricted.

SYSTEM CALLS System Calls act as an interface between the processes and operating system. System calls are made when a process in user mode requires access to a resource. The Application Program Interface (API) connects the operating system’s functions to user programs. The kernel system can only be accessed using system calls. A system call can be written in assembly language or a high-level language. System calls are predefined functions that the operating system may directly invoke if a high-level language is used.

KERNAL PROCESS USER PROCESS Process Execution Generate System Return from system call Execute System Call 1 2 3 4

How are system calls made? When a computer software needs to access the operating system's kernel, it makes a system call. The system call uses an API to expose the operating system’s services to user programs.

Why do you need system calls in Operating System? There are various situations where you must require system calls in the operating system. Following of the situations are as follows: 1. It is must require when a file system wants to create or delete a file. 2. Network connections require the system calls to sending and receiving data packets. 3. If you want to read or write a file, you need to system calls. 4. If you want to access hardware devices, including a printer, scanner, you need a system call. 5. System calls are used to create and manage new processes.

Types of System Calls There are commonly five types of system calls as follows: 1. Process Control 2. File Management 3. Device Management 4. Information Maintenance 5. Communication

Process Control Process control is the system call that is used to direct the processes. Some process control examples include creating, load, abort, end, execute, process, terminate the process, etc. File Management File management is a system call that is used to handle the files. Some file management examples include creating files, delete files, open, close, read, write, etc. Device Management Device management is a system call that is used to deal with devices. Some examples of device management include read, write, get device attributes, release device, etc.

Information Maintenance Information maintenance is a system call that is used to maintain information. There are some examples of information maintenance, including getting system data, set time or date, get time or date, set system data, etc. Communication Communication is a system call that is used for communication. There are some examples of communication, including create, delete communication connections, send, receive messages, etc.

PROCESS CONCEPTS A process is basically a program in execution. The execution of a process must progress in a sequential fashion. A process is defined as an entity which represents the basic unit of work to be implemented in the system. To put it in simple terms, we write our computer programs in a text file and when we execute this program, it becomes a process which performs all the tasks mentioned in the program. When a program is loaded into the memory and it becomes a process, it can be divided into four sections ─ stack, heap, text and data. The following image shows a simplified layout of a process inside main memory

Stack Heap Data Text Process in memory The process stack stores temporary data such as method parameters, return addresses and local variables and also a data section containing global variables. It can also store a heap, which is dynamically allotted memory during execution of a process.

Program Process Program is static object Process is a dynamic object Program is a sequence of instructions Process is a sequences of instructions in execution program resides in secondary storage device A process resides in main memory Program is a passive entity Process is an active entity The time span of program is unlimited The time span of a process is limited

Process States New Ready Running Exit Waiting Admit Dispatch Interrupt Release I/O or Event wait I/O or Event completion

New : This is the initial state when a process is first created. 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. 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. Waiting : Process moves into the waiting state / blocked state if it needs to wait for a resource, such as waiting for user input, or waiting for a file to become available. Exit : Once the process finishes its execution, or it is terminated by the operating system, it is moved to the terminated state / exit state, where it waits to be removed from main memory.

Process Control Block (PCB) A Process Control Block is a data structure maintained by the Operating System for every process. The PCB is identified by an integer process ID (PID). A PCB keeps all the information needed to keep track of a process. There is a separate PCB for each process. Traffic controller module keeps the track on the status of the processes. PCB’s of the processes which are in the same state (ready, executing, waiting etc.) are linked together giving a specific name to the list such as ready list. If many processes waits for the same device, then PCB’s of all these processes are linked together in chain waiting for that device. If device becomes free then traffic controller checks the PCB chain to see if any process is waiting for the device. If processes are available in that device chain, then again it is placed back to ready state to request that device again. The architecture of a PCB is completely dependent on Operating System and may contain different information in different operating systems.

Here is a simplified diagram of a PCB

Process ID: Unique identification for each of the process in the operating system. Process State: The current state of the process i.e., whether it is ready, running, waiting, or whatever. Pointer : A pointer to parent process. Priority : Priority of the process. Program Counter : Program Counter is a pointer to the address of the next instruction to be executed for this process. CPU Registers: Various CPU registers where process need to be stored for execution for running state.

IO status information: This includes a list of I/O devices allocated to the process. Accounting information: This includes the amount of CPU used for process execution, time limits, execution ID etc. Process privileges: This is required to allow/disallow access to system resources.

PROCESS SCHEDULING Scheduling is a fundamental function of an operating system. All computer resources must be scheduled before use. Process scheduling is the activity of the process manager that handles the removal of the running process from the CPU and the selection of another process on the basis of a particular strategy. Process scheduling is an essential part of a Multiprogramming operating system. Such operating systems allow more than one process to be loaded into the executable memory at a time and loaded process shared the CPU using time.

Scheduling Queues Scheduling queues refer to queues of processes or devices. All processes are stored in the job queue. The processes residing in Main Memory, which are ready and waiting to execute are kept on the ready queue. Processes waiting for a device to become available or to deliver data and placed in device queues. Each device has its own device queue. The job queue is a linked list where each PCB contains a pointer pointing to the next PCB in the ready queue.

Job queue: All processes are stored in the job queue. Ready queue: A newly arrived process is put in ready queue. Processes waits ready queue for allocation of CPU. Once the CPU is assigned to process, then that process starts execution. Device queues: The processes which are blocked due to I/O device that processes are placed in I/O queue.

SCHEDULERS A scheduler is an operating system program (module) which selects the next job to be executed from the queues. The main objective of a scheduler is to increase the CPU utilization and throughput. Throughput is the amount of work done in a given interval of time. There are 3 types of schedulers: Long term scheduler Short term scheduler Medium term scheduler

Batch Queue I/O waiting queues Ready Queue Suspended and swapped-out queue CPU I/O Batch Jobs Long term scheduler Short term scheduler Medium term scheduler Medium term scheduler end Swap out Swap in

Long Term Scheduler It is also called a job scheduler. A long-term scheduler determines which job must be submitted for immediate processing. It selects processes from the queue and loads them into memory for execution. Process loads into the memory for CPU scheduling. The primary objective of the job scheduler is to provide a balanced mix of jobs, such as I/O bound and CPU bound. I/O bound Process is one that spends more time doing I/O rather than doing computation. CPU bound Process is one that spends more time in CPU computation than in generating I/O requests.

Short Term Scheduler It is also called as CPU scheduler. Its main objective is to increase system performance. It is the change of ready state to running state of the process. CPU scheduler selects a process among the processes that are ready to execute and allocates CPU to one of them. Short-term schedulers, also known as dispatchers, make the decision of which process to execute next. Short-term schedulers are faster than long-term schedulers.

Medium Term Scheduler Medium-term scheduling is a part of swapping. It removes the processes from the memory. A running process may become suspended if it makes an I/O request. A suspended processes cannot make any progress towards completion. In this condition, to remove the process from memory and make space for other processes, the suspended process is moved to the secondary storage. This process is called swapping, and the process is said to be swapped out or rolled out. Swapping may be necessary to improve the process mix.

Context Switching A context switching is the mechanism to store and restore the state or context of a CPU in Process Control block so that a process execution can be resumed from the same point at a later time. Using this technique, a context switcher enables multiple processes to share a single CPU. Context switching is an essential part of a multitasking operating system features. When the scheduler switches the CPU from executing one process to execute another, the state from the current running process is stored into the process control block. The time taken by the CPU to perform context switching is known as context switch time.

Context Switching Diagram

Operations on a process The Operating System must provide a mechanism for process creation and termination. There are many operations that can be performed on processes. Some of them are: Process Creation Process Preemption Process Blocking and Process Termination

Process Creation A process may create several new processes, via create process system call during execution. The creating process is called the parent process, and the new processes are called the children of that process. Each of these processes may in turn create other processes, formatting a tree of processes. In general, a process will need certain resources (CPU time, memory, files, I/O devices) to accomplish a task. When a process creates a subprocess, that subprocess may be able to obtain its resources directly from the operating system, or it may be constrained to a subset of this process. The parent may have to partition its resources among its children, of it may be able to share some resources (such as memory or files) among several of its children. Apart from obtaining the resources from parent process, a subprocess may receive initialization data as parameters from the parent process. For example, file name, device name etc.

A process may be created by another process using fork(). The creating process is called the parent process and the created process is the child process. A child process can have only one parent but a parent process may have many children. Both parent and child processes have the same memory image, open files, and environment strings. However, they have distinct address spaces. The figure 2.9 demonstrates process creation using fork() is as follows: pid = fork() wait() exit() exec() Parent Child Process (pid=0) Parent Process (pid>0) Parent Resumes Execution Process creation using fork()

Process Preemption An interrupt mechanism is used in preemption that suspends the process executing currently and the next process to execute is determined by the short-term scheduler. Preemption makes sure that all processes get some CPU time for execution. Ready Running Dispatch Preempt

Process Blocking The process is blocked if it is waiting for some event to occur. This event may be I/O as the I/O events are executed in the main memory and don’t require the processor. After the event is complete, the process again goes to the ready state. Ready Running Blocked Dispatch Preempt I/O wait I/O complete

Process Termination A process terminates when it finishes executing its final statement and asks the operating system to delete it using the exit() system call. At that point, the process may return a status value (an integer) to it parent process via the wait() system call. All the resources of the process are de-allocated by the operating system. A parent process can terminate the execution of one of its children for various reasons as: Child has exceeded its usage of some of the resources it has been allocated. The task assigned to the child is no longer required. The parent is existing, and the operating system does not allow child to continue if its parent terminates.

Cooperating Processes Processes executing concurrently in the operating system may be either independent processes or cooperating processes. A process is independent if it cannot affect or be affected by the other processes executing in the system. Any process that does not share its data with any other process is independent. A process is cooperating if it can affect or be affected by other processes executing in the system. Any process that shares data with other processes is a cooperating process. Advantages of Co-operating Process Computation Speedup Modularity Convenience

Methods of Cooperation Cooperation by Sharing : The Cooperation processes can cooperate with each other using shared data such as memory, variables, files etc. Process P1 Shared Data Process P2 Kernel

Cooperation by Communication: The Cooperating processes can cooperate with each other using messages. This may lead to deadlock if each process is waiting for a message from the other to perform a operation. Starvation is also possible if a process never receives a message. Process P1 Process P2 kernel M M M

THREAD In operating systems, a thread is the smallest sequence of programmed instructions that can be managed independently by a scheduler. Thread is also called a lightweight process. Threads provide a way to improve application performance through parallelism. Each thread belongs to exactly one process and no thread can exist outside a process. Threads are contained within processes. A process can have multiple threads, each executing concurrently and independently, sharing the same resources such as memory space and file. Each thread has its own Thread Control Block (TCB). Threads can share common data so they do not need to use inter-process communication like a processes, threads also have a states like ready, executing, blocked etc. Priority can be assigned to the threads just like process and highest priority thread is scheduled first.

S.NO Process Thread 1. Process means any program is in execution. Thread means a segment of a process. 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. 4. It also takes more time for context switching. It takes less time for context switching. 5. The process is less efficient in terms of communication. Thread is more efficient in terms of communication. 6.  Multiprogramming holds the concepts of multi-process. We don’t need multi programs in action for multiple threads because a single process consists of multiple threads.

S.NO Process Thread 8. The process is called the heavyweight process. A Thread is lightweight as each thread in a process shares code, data, and resources. 10. If one process is blocked then it will not affect the execution of other processes  If a user-level thread is blocked, then all other user-level threads are blocked.  11. The process has its own Process Control Block, Stack, and Address Space. Thread has Parents’ PCB, its own Thread Control Block, and Stack and common Address space. 12. Changes to the parent process do not affect child processes. Since all threads of the same process share address space and other resources so any changes to the main thread may affect the behavior of the other threads of the process. 13. A system call is involved in it. No system call is involved, it is created using APIs. 14. The process does not share data with each other. Threads share data with each other.

Threads offer several advantages in operating systems: Concurrency Resource Sharing Responsiveness Simplicity Concurrency : Threads allow multiple tasks to be executed simultaneously, enhancing system responsiveness and performance. Resource Sharing : Threads within the same process can share resources such as memory space, files, and other system resources, making communication and data sharing between threads more efficient than between separate processes. Responsiveness : By utilizing threads, an application can remain responsive to user input or system events while performing other tasks in the background. Simplicity : Threads are often easier to work with than processes due to their lightweight nature and shared resources within a process.

Threads can be implemented in various ways, including: User-level Threads Kernel-level Threads User-level Threads : Managed entirely by the application without kernel involvement. This method is fast and efficient but may suffer from limitations in terms of parallelism and system resource utilization. Kernel-level Threads : Managed directly by the operating system kernel. Kernel-level threads offer better parallelism and can take advantage of multi-core processors more effectively. However, they may have higher overhead due to frequent kernel calls.

UNIT - II UNIT-II CPU SCHEDULING CPU Scheduling - Scheduling Criteria, Scheduling Algorithms, Multiple -Processor Scheduling. System call interface for process management-fork, exit, wait, waitpid, exec Deadlocks - System Model, Deadlocks Characterization, Methods for Handling Deadlocks, Deadlock Prevention, Deadlock Avoidance, Deadlock Detection, and Recovery from Deadlock

CPU Scheduling Several processes are kept in memory at one time. When One process has to wait, the operating system takes CPU away from process and gives the CPU to another process. This pattern continues. Every time one process has to wait, another process can take over use of the CPU. Scheduling of this kind is a fundamental operating-system function. Almost all computer resources are scheduled before use. CPU-I/O Burst Cycle : Process execution consists of a cycle of CPU execution and O/I wait. Processes alternate between these two states. Process execution begins with a CPU burst. That is followed by an O/I burst, Which is followed another CPU burst, then another O/I burst, and so on. Eventually, the final CPU burst ends with a system request to terminate execution.

CPU Scheduling Criteria There are two modes in CPU Scheduling Algorithms. They are: Pre-emptive Scheduling Non Pre-emptive Scheduling

Pre-emptive Scheduling During CPU scheduling the following situations may arise: A process switches from running state to ready state due to an interrupt (such as time slice(or) time quantum, etc.). A process switches from waiting state to ready state (after completion of I/O operation). When scheduling takes place under the above circumstances, it is known as pre-emptive scheduling. In pre-emptive scheduling the CPU can be taken away from the process anytime. Further it requires special hardware (for example a timer) and is expensive. Consider an example of two processes P1 and P2 sharing the same data. Suppose when P1 is updating the data, it is pre-empted and P2 is run. Now P2 may try to read the data which is inconsistent. Therefore new mechanisms are required to coordinate, accessing of shared data

Non Pre-emptive Scheduling In a non pre-emptive scheduling, a selected job runs to completion. This implies that once the CPU has been allotted a process, the process keeps the CPU until it terminates or switches to the waiting state. The two situations that can arise during non pre-emptive CPU scheduling are: A process switches from running state to waiting state due to an O/I request. When a process terminates. CPU scheduling under such situation is known as Non pre-emptive scheduling or co-operative scheduling. Non pre-emptive scheduling method was used by Microsoft Windows 3x. It is the only method that can be used on certain hardware platforms However all subsequent versions of Windows operating systems have used pre-emptive scheduling.

NON-PREEMPTIVE SCHEDULING PREEMPTIVE SCHEDULING Once the CPU is given to a process, it cannot be taken away from that process. The CPU can be taken away from process at any time due to time slice or interrupt. Shorter Jobs must wait for completion of longer Jobs Shorter jobs need not wait. Cost is low. Cost is high. Overheads are low. Overheads are high due to storage of non-running programs UI main memory. Suitable for batch processing Suitable for real time, and interactive timesharing systems It occurs when the process either switches from running state to waiting state or when it terminates. It occurs when the process either switches from running state or from waiting state to ready state. There is no need of context switching. There is a need of context switching.

NON-PREEMPTIVE SCHEDULING PREEMPTIVE SCHEDULING Job is completed according to the allocated time. Completion time of the process inexecution cannot be computed accurately. Examples of non-preemptive scheduling are First Come First Serve and Shortest Job First. Examples of preemptive scheduling are Round Robin and Shortest Remaining Time First.

CPU Scheduling Criteria CPU utilization : If the CPU is busy all the time, the utilization factor of all components of the system will also be high. Ideally CPU utilization may range from 0 to 100%. However, in real systems it may range from 40% to 90%. Throughput : Throughput is the amount of work done in a unit of time. One way of measuring throughput is by the number of processes completed in a unit time. But this may not be useful for comparison, since it depends on the characteristics and resource requirement of the process being executed For example: the throughput may be 1 process per hour for long processes and 10 processes per second for short processes. Thus for comparison it must execute processes with similar complexity and requirements .

Turn around Time : Turn around time is defined as the time interval from the time of submission of a process, to the time of completion. Or Turn around time = Waiting time to get into memory + Waiting in the ready queue + CPU time + doing I/O operations. Fairness : When there is no guidance from the user or other sources, processes should be treated the same, and no process should suffer starvation. Predictability : A given job must run for the same amount of time and at the same cost, regardless of the system load. Large variation in response time or turn around time is not feasible.

Waiting Time : The waiting time is the time a process waits to get the CPU or it is the sum of periods spent waiting in ready queue. Generally a CPU scheduling algorithm having the least average waiting time, is said to be the best algorithm. Response Time : Response is the time from the submission of a request until the first response is produced. In an interactive system it is the time interval between the last interaction of a program and the appearance of the last result on the monitor. Priorities : When processes are assigned priorities, the scheduling policy must favour higher-priority processes.

SCHEDULING ALGORITHMS BASIC TERMINOLOGIES Process ID: The Process ID is the first Thing is to be written while solving the problem. The Process ID acts like the name of the process. It is usually represented with numbers or P letter with numbers Example: 1. 0, 1, 2, 3, . . . . . . . . . . 2. P0, P1, P2, P3 . . . . . . . . Usually, we start the naming of the process from zero. We can also start the numbering from number 1 also. It is our interest. Arrival Time: The time which is required for the Process to enter the ready queue or the time when the Process is ready to be executed by the CPU. This Arrival Time can be represented as AT in short form. The Arrival Times is always positive or also zero.

Burst Time: The Time Slot which the Process requires to complete the Process is known as the Burst Time. The Burst Time can be represented as BT in short form. The Burst Times of a process is always greater than zero. Completion Time: The Total Time required by the CPU to complete the process is known as Completion Time. The Completion Time can be represented as CT in short form. The Completion will always be greater than zero. Turn Around Time: The time taken by the CPU since the Process has been ready to execute or since the process is in Ready Queue is known as Turn Around Time. The Turn Around Time can be calculated with the help of Completion Time and Arrival Time. The Turn Around Time can be represented as TAT in short form. The Turn Around Time is the difference of Completion Time and Arrival Time. Formula TAT = CT - AT Here, CT is Completion Time and AT is Arrival Time.

Waiting Time: The time the Process has been waiting to complete its process since the assignment of process for completion is known as Waiting Time. The Waiting Time can be represented as WT in short form. The Waiting Time can be calculated with the help of Turn Around Time and Burst Time. The Waiting Time is the difference between Turn Around Time and Burst Time Formula WT = TAT - BT Ready Queue: The Queue where all the processes are stored until the execution of the previous process. This ready queue is very important because there would be confusion in CPU when two same kinds of processes are being executed at the same time. Gantt Chart: It is the place where all the already executed processes are stored. This is very useful for calculating Waiting Time, Completion Time, Turn Around Time.

Types of CPU Scheduling Algorithms First Come First Serve Shortest Job First Priority Scheduling Round Robin Scheduling

First Come First Serve It is one of the simplest CPU scheduling algorithms. In this scheme, the process that requests the CPU first, gets the CPU first. Its implementation is managed by. FIFO (First-in First-out) queue, i.e., when a process enters the ready queue, its PCB is linked to the tail of the queue (i.e., inserted at the rear). When the CPU becomes free, it is allotted to the process at the front of the queue and the running process is then removed from the queue. Once a process gets the CPU, it runs up to completion. CPU - - - -> Central Processing Unit FCFS - - - -> First Come First Serve AT - - - - - > Arrival Time BT - - - - -> Burst Time WT - - - -> Waiting Time TAT - - - -> Turn Around Time CT - - - - -> Completion Time FIFO - - - > First In First Out

Characteristics of FCFS (First Come First Serve): First Come First Serve can follow or can be executed in Non-Preemptive Approach. The Process which enters the Ready Queue is executed First. So, we say that FCFS follows First in First Out Approach. First Come First Serve is only executed when the Arrival Time (AT) is greater than or equal to the Time which is at present. Advantages Very easy to perform by the CPU Follows FIFO Queue Approach Disadvantages First Come First Serve is not very efficient. First Come First Serve suffers because of Convoy Effect.

Convoy Effect In First Come First Serve (FCFS ) FCFS may suffer from the convoy effect if the burst time of the first job is the highest among all. As in the real life, if a convoy is passing through the road then the other persons may get blocked until it passes completely. This can be simulated in the Operating System also. 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.

Example TALK AND CHALK

Shortest Job First CPU Scheduling Algorithm This is another type of CPU Scheduling Algorithms. Here, in this CPU Scheduling Algorithm we are going to learn how CPU is going to allot resources to the certain process. The Shortest Job is heavily dependent on the Burst Times. Every CPU Scheduling Algorithm is basically dependent on the Arrival Times. Here, in this Shortest Job First CPU Scheduling Algorithm, the CPU allots its resources to the process which is in ready queue and the process which has least Burst Time. If we face a condition where two processes are present in the Ready Queue and their Burst Times are same, we can choose any process for execution. In actual Operating Systems, if we face the same problem then sequential allocation of resources takes place. Shortest Job First can be called as SJF in short form.

Characteristics: SJF (Shortest Job First) has the least average waiting time. This is because all the heavy processes are executed at the last. So, because of this reason all the very small, small processes are executed first and prevent starvation of small processes. It is used as a measure of time to do each activity. If shorter processes continue to be produced, hunger might result. The idea of aging can be used to overcome this issue. Shortest Job can be executed in Preemptive and also non preemptive way also. Advantages SJF is used because it has the least average waiting time than the other CPU Scheduling Algorithms SJF can be termed or can be called as long term CPU scheduling algorithm. Disadvantages Starvation is one of the negative traits Shortest Job First CPU Scheduling Algorithm exhibits. Often, it becomes difficult to forecast how long the next CPU request will take.

Example TALK AND CHALK

Priority CPU Scheduling This is another type of CPU Scheduling Algorithms. Here, in this CPU Scheduling Algorithm we are going to learn how CPU is going to allot resources to the certain process. The Priority CPU Scheduling is different from the remaining CPU Scheduling Algorithms. Here, each and every process has a certain Priority number. There are two types of Priority Values. Highest Number is considered as Highest Priority Value Lowest Number is considered as Highest Priority Value Priority for Prevention The priority of a process determines how the CPU Scheduling Algorithm operates, which is a preemptive strategy. Since the editor has assigned equal importance to each function in this method, the most crucial steps must come first. The most significant CPU planning algorithm relies on the FCFS (First Come First Serve) approach when there is a conflict, that is, when there are many processors with equal value.

Characteristics Priority CPU scheduling organizes tasks according to importance. When a task with a lower priority is being performed while a task with a higher priority arrives, the task with the lower priority is replaced by the task with the higher priority, and the latter is stopped until the execution is finished. A process's priority level rises as the allocated number decreases. Advantages The typical or average waiting time for Priority CPU Scheduling is shorter than First Come First Serve (FCFS). It is easier to handle Priority CPU scheduling It is less complex Disadvantages The Starvation Problem is one of the Preemptive Priority CPU Scheduling Algorithm's most prevalent flaws. Because of this issue, process must wait a longer period of time to be scheduled into the CPU. The hunger problem or the starvation problem is the name given to this issue.

Example TALK AND CHALK

Round Robin CPU Scheduling Round Robin is a CPU scheduling mechanism those cycles around assigning each task a specific time slot. It is the First come, First served CPU Scheduling technique with preemptive mode. The Round Robin CPU algorithm frequently emphasizes the Time-Sharing method. Round Robin CPU Scheduling Algorithm characteristics include: Because all processes receive a balanced CPU allocation, it is straightforward, simple to use, and starvation-free. One of the most used techniques for CPU core scheduling. Because the processes are only allowed access to the CPU for a brief period of time, it is seen as preemptive. The benefits of round robin CPU Scheduling: Every process receives an equal amount of CPU time, therefore round robin appears to be equitable. To the end of the ready queue is added the newly formed process.

Example TALK AND CHALK

Multiple processor scheduling Multiple processor scheduling or multiprocessor scheduling focuses on designing the system's scheduling function, which consists of more than one processor. Multiple CPUs share the load (load sharing) in multiprocessor scheduling so that various processes run simultaneously. In general multiprocessor scheduling is complex as compared to single processor scheduling. In the multiprocessor scheduling, there are many processors, and they are identical, and we can run any process at any time. The multiple CPUs in the system are in close communication, which shares a common bus, memory, and other peripheral devices. So we can say that the system is tightly coupled. These systems are used when we want to process a bulk amount of data, and these systems are mainly used in satellite, weather forecasting, etc.

Multiprocessor systems may be heterogeneous (different kinds of CPUs) or homogenous (the same CPU). Systems in which the processors are identical in terms of their functionality are known as homogenous systems. Systems in which the processors are different are known as heterogeneous systems. Here only the homogenous systems are considered. Approaches to Multiple Processor Scheduling: There are two approaches to multiple processor scheduling in the operating system: Asymmetric Multiprocessing Symmetric Multiprocessing (SMP)

1 . Asymmetric Multiprocessing: It is used when all the scheduling decisions and I/O processing are handled by a single processor called the Master Server . The other processors execute only the user code . This is simple and reduces the need for data sharing, and this entire scenario is called Asymmetric Multiprocessing. 2. Symmetric Multiprocessing: It is used where each processor is self-scheduling . All processes are in a common ready queue, or each processor has its own private queue for ready processes. The scheduling proceeds further by having the scheduler for each processor examine the ready queue and select a process to execute. All modern operating s ystems including windows XP, Linux, Solaris and Mac OSX support SMP.

System Calls A system call is a mechanism that provides the interface between a process and the operating system. It is a programmatic method in which a computer program requests a service from the kernel of the OS. The system calls for process management are useful to create a process, suspends the execution of process, terminating the process etc. Different OS uses different system calls for this purpose. The Linux operating system uses the following system calls for process management: fork(), wait(), waitpid (), exec() and exit().

fork(): Processes generate clones of themselves using the fork() system call. It is one of the most common ways to create processes in operating systems. When a parent process spawns a child process, execution of the parent process is interrupted until the child process completes. Once the child process has completed its execution, control is returned to the parent process. exit(): The exit() is a system call that is used by a process to terminate its execution. This call indicates that the thread execution is complete, which is especially useful in multi-threaded environments.

wait() In some systems, a process may have to wait for another process to complete its execution before proceeding. When a parent process makes a child process, the parent process execution is suspended until the child process is finished. The wait() system call is used to suspend the parent process. Once the child process has completed its execution, control is returned to the parent process. waitpid () The waitpid () system call would wait for specified children to terminate and return its termination status. exec() When an executable file replaces an earlier executable file in an already executing process, this system function is invoked. As a new process is not built, the old process identification stays, but the new process replaces data, stack, data, head, etc.

DEADLOCKS Every process needs some resources to complete its execution. However, the resource is granted in a sequential order. The process requests for some resource. OS grant the resource if it is available otherwise let the process waits. The process uses it and release on the completion. A Deadlock is a situation where each of the computer process waits for a resource which is being assigned to some another process. In this situation, none of the process gets executed since the resource it needs, is held by some other process which is also waiting for some other resource to be released.

A deadlock occurs when a set of processes is stalled because each process is holding a resource and waiting for another process to acquire another resource. In the diagram below, for example, Process 1 is holding Resource 1 while Process 2 acquires Resource 2, and Process 2 is waiting for Resource 1.

Deadlock Characteristics / Deadlock Necessary Conditions: In a deadlock, process never finish executing and system resources are tied up. A deadlock situation can arise if the following four conditions hold simultaneously in a system. Mutual Exclusion: At a time only one process can use the resources. If another process requests that resource, requesting process must wait until the resource has been released. Hold and Wait: A process must be holding at least one resource and waiting to additional resource that is currently held by other processes. No Preemption: Resources allocated to a process can’t be forcibly taken out from it unless it releases that resource after completing the task. Circular Wait: All the processes must be waiting for the resources in a cyclic manner so that the last process is waiting for the resource which is being held by the first process.

Difference between Starvation and Deadlock S.No . Deadlock Starvation 1 Deadlock is a situation where no process proceeds. Starvation is a situation where the low priority process got blocked and the high priority processes proceed. 2 Deadlock is an infinite waiting. Starvation is a long waiting but not infinite. 3 Every Deadlock is always a starvation. Every starvation need not be deadlock. 4 The requested resource is blocked by the other process. The requested resource is continuously be used by the higher priority processes. 5 Deadlock happens when Mutual exclusion, hold and wait, No preemption and circular wait occurs simultaneously. It occurs due to the uncontrolled priority and resource management.

Deadlock System Model: A system consists of a finite number of resources to be distributed among a number of competing processes. The resources are partitioned into several types each of which consists of a number of identical instances. A process may utilized a resources in the following sequence Request: In this state one can request a resource. Use: In this state the process operates on the resource. Release: In this state the process releases the resources.

Resource Allocation Graph ( RAG ) The resource allocation graph is the pictorial representation of the state of a system. As its name suggests, the resource allocation graph is the complete information about all the processes which are holding some resources or waiting for some resources. It also contains the information about all the instances of all the resources whether they are available or being used by the processes. In Resource allocation graph, the process is represented by a Circle while the Resource is represented by a rectangle. Let's see the types of vertices and edges in detail.

Vertices are mainly of two types, Resource and process. Each of them will be represented by a different shape. Circle represents process while rectangle represents resource. A resource can have more than one instance. Each instance will be represented by a dot inside the rectangle.

Edges in RAG are also of two types, one represents assignment and other represents the wait of a process for a resource. The above image shows each of them. A resource is shown as assigned to a process if the tail of the arrow is attached to an instance to the resource and the head is attached to a process.

A process is shown as waiting for a resource if the tail of an arrow is attached to the process while the head is pointing towards the resource.

Single instances RAG If there is a cycle in the Resource Allocation Graph and each resource in the cycle provides only one instance, then the processes will be in deadlock. For example, if process P1 holds resource R1, process P2 holds resource R2 and process P1 is waiting for R2 and process P2 is waiting for R1, then process P1 and process P2 will be in deadlock.

Here’s another example, that shows Processes P1 and P2 acquiring resources R1 and R2 while process P3 is waiting to acquire both resources. In this example, there is no deadlock because there is no circular dependency. So cycle in single-instance resource type is the sufficient condition for deadlock.

P1 P2 Allocate Request R1 R2 R1 R2 1 1 1 1 R1 R2 P1 P2 P3 P1 P2 Allocate Request R1 R2 R1 R2 1 1 P3 1 1 Availability (0, 0) Availability (0, 0) 1, 0 DEADLOCK NO DEADLOCK Single Instance RAG - Example

Multi-instances RAG There is no deadlock in this RAG. Even though there is a cycle, still there is no deadlock. Therefore in multi-instance resource cycle is not sufficient condition for deadlock.

RAG – EXERCISE - 1 Multiple Instance RAG - Example

RAG – EXERCISE - 2 Multiple Instance RAG - Example Single Instance and Circular wait, it is always a deadlock. Multiple Instance and Circle wait, it is not a deadlock always.

Methods For Handling Deadlocks The problem of deadlock can deal with the following 3 ways: 1. Deadlock Prevention 2. Deadlock Avoidance 3. Deadlock Detection and Recovery 4. Head-in-the-sand approach

Deadlock Prevention Deadlock prevention is a set of methods for ensuring that at least one of these necessary conditions cannot hold. Mutual Exclusion Hold and wait No Pre-emption Circular Wait TALK AND CHALK

Deadlock Detection and Recovery The system considers that the deadlock will definitely occur. In order to get rid of deadlocks, The OS periodically checks the system for any deadlock. In case, it finds any of the deadlock then the OS will recover the system using some recovery techniques. The main task of the OS is detecting the deadlocks. The OS can detect the deadlocks with the help of Resource allocation graph.

In single instanced resource types, if a cycle is being formed in the system then there will definitely be a deadlock. On the other hand, in multiple instanced resource type graph, detecting a cycle is not just enough. We have to apply the safety algorithm on the system by converting the resource allocation graph into the allocation matrix and request matrix. TALK AND CHALK

Banker’s Algorithm This algorithm can be used in banking system to ensure that the bank never allocates all its available cash such that it can no longer satisfy the needs of all its customer. This algorithm is applicable to a system with multiple instances of each resource type. When a new process enter in to the system it must declare the maximum number of instances of each resource type that it may need. This number may not exceed the total number of resources in the system. TALK AND CHALK

In order to recover the system from deadlocks, either OS considers resources or processes. For Resource Preempt the resource We can snatch one of the resources from the owner of the resource (process) and give it to the other process with the expectation that it will complete the execution and will release this resource sooner. Well, choosing a resource which will be snatched is going to be a bit difficult. Rollback to a safe state System passes through various states to get into the deadlock state. The operating system can rollback the system to the previous safe state. For this purpose, OS needs to implement check pointing at every state. The moment, we get into deadlock, we will rollback all the allocations to get into the previous safe state.

For Process Kill a process Killing a process can solve our problem but the bigger concern is to decide which process to kill. Generally, Operating system kills a process which has done least amount of work until now. Kill all process This is not a suggestible approach but can be implemented if the problem becomes very serious. Killing all process will lead to inefficiency in the system because all the processes will execute again from starting.

UNIT - III UNIT-III PROCESS MANAGEMENT AND SYNCHRONIZATION Process Management and Synchronization - The Critical Section Problem, Synchronization Hardware, Semaphores, and Classical Problems of Synchronization, Critical Regions, Monitors. Inter process Communication Mechanisms: IPC between processes on a single computer system, IPC between processes on different systems, using pipes, FIFOs, message queues, shared memory.

Process Synchronization Process Synchronization is the coordination of execution of multiple processes in a multi-process system to ensure that they access shared resources in a controlled and predictable manner. It aims to resolve the problem of race conditions and other synchronization issues in a concurrent system. The main objective of process synchronization is to ensure that to prevent the possibility of inconsistent data due to concurrent access. To achieve this, various synchronization techniques such as semaphores, monitors, and critical sections are used. In a multi-process system, synchronization is necessary to ensure data consistency and integrity, and to avoid the risk of deadlocks and other synchronization problems. 

On the basis of synchronization, processes are categorized as one of the following two types: Independent Process: The execution of one process does not affect the execution of other processes. Cooperative Process: A process that can affect or be affected by other processes executing in the system.

Race Condition When more than one process is executing the same code or accessing the same memory or any shared variable in that condition there is a possibility that the output or the value of the shared variable is wrong so for that all the processes doing the race to say that my output is correct this condition known as a race condition.  Several processes access and process the manipulations over the same data concurrently, and then the outcome depends on the particular order in which the access takes place. A race condition is a situation that may occur inside a critical section. This happens when the result of multiple thread execution in the critical section differs according to the order in which the threads execute. Race conditions in critical sections can be avoided if the critical section is treated as an atomic instruction. Also, proper thread synchronization using locks or atomic variables can prevent race conditions.

Example: Note : We are assuming the final value of a common variable(shared) after execution of Process P1 and Process P2 is 10 (as Process P1 increment variable (shared=10) by 1 and Process P2 decrement variable (shared=11) by 1 and finally it becomes shared=10). But we are getting undesired value due to a lack of proper synchronization.

The Critical Section Problem The regions of a program that try to access shared resources and may cause race conditions are called critical section. Consider a system consisting of n processes (P0, P1, ……… Pn -1) each process has a segment of code which is known as critical section in which the process may be changing common variable, updating a table, writing a file and so on. The important feature of the system is that when the process is executing in its critical section no other process is to be allowed to execute in its critical section. The execution of critical sections by the processes is a mutually exclusive. The critical section problem is to design a protocol that the process can use to cooperate each process must request permission to enter its critical section. The section of code implementing this request is the entry section. The critical section is followed on exit section. The remaining code is the remainder section.

In the above diagram, the entry section handles the entry into the critical section. It acquires the resources needed for execution by the process. The exit section handles the exit from the critical section. It releases the resources and also informs the other processes that the critical section is free.

Requirements of Synchronization mechanisms Although there are some properties that should be followed if any code in the critical section: Primary 1. Mutual Exclusion: The mechanism must ensure: The processes access the critical section in a mutual exclusive manner. Only one process is present inside the critical section at any time. No other process can enter the critical section until the process already present inside it completes. 2. Progress: The mechanism must ensure: An entry of a process inside the critical section is not dependent on the entry of another process inside the critical section. A process can freely enter inside the critical section if there is no other process present inside it. A process enters the critical section only if it wants to enter. A process is not forced to enter inside the critical section if it does not want to enter.

Secondary Bounded Wait: The mechanism should ensure: The wait of a process to enter the critical section is bounded. A process gets to enter the critical section before its wait gets over. Architectural Neutrality: The mechanism should ensure: It can run on any architecture without any problem. There is no dependency on the architecture.

Hardware Synchronization Algorithms Process Synchronization problems occur when two processes running concurrently share the same data or same variable. The value of that variable may not be updated correctly before its being used by a second process. Such a condition is known as Race Around Condition. we will talk about the most efficient hardware solution to process synchronization problems and its implementation.  There are three algorithms in the hardware approach of solving Process Synchronization problem:  Test and Set  Swap  Unlock and Lock 

Semaphores For the solution to the critical section problem one synchronization tool is used which is known as semaphores. Semaphore is an integer variable which is used in mutual exclusion manner by various concurrent cooperative process in order to achieve synchronization. Types of Semaphores: There are two main types of semaphores i.e. counting semaphores and binary semaphores Counting Semaphores Binary Semaphores The two Semaphore Operations are: Wait Semaphore Operation Signal Semaphore Operation

Wait Semaphore Operation The Wait Operation is used for deciding the condition for the process to enter the critical state or wait for execution of process. Here, the wait operation has many different names. Sleep Operation Down Operation Decrease Operation P Function (most important alias name for wait operation) The Wait Operation works on the basis of Semaphore Value. Here, if the Semaphore value is greater than zero or positive then the Process can enter the Critical Section Area. If the Semaphore value is equal to zero then the Process has to wait for the Process to exit the Critical Section Area. If the Process exits the Critical Section we have to reduce the value of Semaphore.

Basic Algorithm of P Function or Wait Operation P (Semaphore value) { Allow the process to enter if the value of Semaphore is greater than zero or positive. Do not allow the process if the value of Semaphore is less than zero or zero. Decrement the Semaphore value if the Process leaves the Critical State. }

Signal Semaphore Operation The Signal Semaphore Operation is used to update the value of Semaphore. The Semaphore value is updated when the new processes are ready to enter the Critical Section. The Signal Operation is also known as: Wake up Operation Up Operation Increase Operation V Function (most important alias name for signal operation)

We know that the semaphore value is decreased by one in the wait operation when the process left the critical state. So, to counter balance the decreased number 1 we use signal operation which increments the semaphore value. This induces the critical section to receive more and more processes into it. The most important part is that this Signal Operation or V Function is executed only when the process comes out of the critical section. The value of semaphore cannot be incremented before the exit of process from the critical section Basic Algorithm of V Function or Signal Operation V (Semaphore value) { If the process goes out of the critical section then add 1 to the semaphore value Else keep calm until process exits }

Types of Semaphores There are two types of Semaphores. 1. Binary Semaphore Here, there are only two values of Semaphore in Binary Semaphore Concept. The two values are 1 and 0. If the Value of Binary Semaphore is 1, then the process has the capability to enter the critical section area. If the value of Binary Semaphore is 0 then the process does not have the capability to enter the critical section area.

2. Counting Semaphore Here, there are two sets of values of Semaphore in Counting Semaphore Concept. The two types of values are values greater than and equal to one and other type is value equal to zero. If the Value of Binary Semaphore is greater than or equal to 1, then the process has the capability to enter the critical section area. If the value of Binary Semaphore is 0 then the process does not have the capability to enter the critical section area.

Advantages of a Semaphore Semaphores allow only one process into the critical section. They follow the mutual exclusion principle strictly and are much more efficient than some other methods of synchronization. There is no resource wastage because of busy waiting in semaphores as processor time is not wasted unnecessarily to check if a condition is fulfilled to allow a process to access the critical section. Semaphores are implemented in the machine independent code of the microkernel. So they are machine independent.

Disadvantages of a Semaphore Semaphores are complicated so the wait and signal operations must be implemented in the correct order to prevent deadlocks. Semaphores are impractical for last scale use as their use leads to loss of modularity. This happens because the wait and signal operations prevent the creation of a structured layout for the system. Semaphores may lead to a priority inversion where low priority processes may access the critical section first and high priority processes later.

Classical Problems of Synchronization Producer-Consumer Problem / Bounded-Buffer Problem Readers-Writers Problem Dining-Philosophers Problem

Producer-Consumer Problem The Producer-Consumer problem is a classical multi-process synchronization problem, that is we are trying to achieve synchronization between more than one process. There is one Producer in the producer-consumer problem, Producer is producing some items, whereas there is one Consumer that is consuming the items produced by the Producer. The same memory buffer is shared by both producers and consumers which is of fixed-size. The task of the Producer is to produce the item, put it into the memory buffer, and again start producing items. Whereas the task of the Consumer is to consume the item from the memory buffer.

Let's understand what is the problem? Below are a few points that considered as the problems occur in Producer-Consumer: The producer should produce data only when the buffer is not full. In case it is found that the buffer is full, the producer is not allowed to store any data into the memory buffer. Data can only be consumed by the consumer if and only if the memory buffer is not empty. In case it is found that the buffer is empty, the consumer is not allowed to use any data from the memory buffer. Accessing memory buffer should not be allowed to producer and consumer at the same time. The solution for the producer is to go to sleep if the buffer is full. The next time the consumer removes an item from the buffer, it wakes up the producer who starts to fill the buffer again. In the same way the consumer goes to sleep if it finds the buffer to be empty. The next time the producer puts the data into the buffer, it wakes up the sleeping consumer.

Solution to Producer Consumer problem using Semaphores m (mutex), a binary semaphore which is used to acquire and release the lock. empty, a counting semaphore whose initial value is the number of slots in the buffer, since, initially all slots are empty. full, a counting semaphore whose initial value is 0. Producer Code Consumer Code do{ wait(empty); // wait until empty > 0 and then decrement empty. wait(mutex); // acquire lock /* add data to buffer */ signal(mutex); // release lock signal(full); // increment full } while (TRUE) do{ wait(full); // wait until full > 0 and then decrement full. wait(mutex); // acquire lock /* remove data to buffer */ signal(mutex); // release lock signal(empty); // increment empty } while (TRUE)

The Readers-Writers Problem A database is to be shared among several concurrent processes. Some of these processes may want only to read the database, whereas others may want to update the database. We distinguish between these two types of processes by referring to the former as Readers and to the latter as writers. The conditions to be satisfied as below: Any number of readers may simultaneously read the data. Only one writer at a time may write into the data area. If a writer is writing, no reader may read it. Case Process 1 Process 2 Allowed / Not Ca se 1 Writing Writing Not Allowed Case 2 Reading Writing Not Allowed Case 3 Writing Reading Not Allowed Case 4 Reading Reading Allowed

Solution to Readers-Writers problem using Semaphores Will make use of two semaphores and an integer variable. mutex, a semaphore (initialized to 1) which is used to ensure mutual exclusion when readcount is updated i.e. when any reader enters or exit from the critical section. wrt , a semaphore (initialized to 1) common to both reader and writer processes. readcount , an integer variable (initialized to 0) that keeps track of how many processes are currently reading the object. Functions for semaphore :  wait() : decrements the semaphore value.  signal() : increments the semaphore value. 

Writer process:  Writer requests the entry to critical section. If allowed i.e. wait() gives a true value, it enters and performs the write. If not allowed, it keeps on waiting. It exits the critical section. do { // writer requests for critical section wait( wrt ); /* performs the write */ // leaves the critical section signal( wrt ); } while(true);

Reader process:  Reader requests the entry to critical section. If allowed:  It increments the count of number of readers inside the critical section. If this reader is the first reader entering, it locks the  wrt  semaphore to restrict the entry of writers if any reader is inside. It then, signals mutex as any other reader is allowed to enter while others are already reading. After performing reading, it exits the critical section. When exiting, it checks if no more reader is inside, it signals the semaphore “ wrt ” as now, writer can enter the critical section. If not allowed, it keeps on waiting.

do { wait(mutex); // Reader wants to enter the critical section readcnt ++; // The number of readers has now increased by 1 if ( readcnt ==1) wait( wrt ); // this ensure no writer can enter if there is even one reader signal(mutex ); // other readers can enter while this current reader is inside the critical section /* current reader performs reading here */ wait(mutex); // a reader wants to leave readcnt --; if ( readcnt == 0) // that is, no reader is left in the critical section signal( wrt ); // writers can enter signal(mutex); // reader leaves } while(true);

Dining-Philosophers Problem The dining philosophers problem is summarized as five philosophers sitting at a table doing one of two things - eating or thinking. While eating, they are not thinking, while thinking, they are not eating. The five philosophers sit at a circular table with a bowl of noodles in the center. A fork is placed in between each philosopher, and as such, each philosopher has one fork to his left and one fork to his right. As noodles is difficult to serve and eat with a single fork, it is assumed that a philosopher must eat with two forks. The philosophers can only use the fork immediate left or right as shown in the figure.

When the philosophers are thinking they never speak to each other. One cannot pick up a fork that is already in the hand of a neighbor. When a hungry philosopher has both his forks at the same time, he eats with out releasing his forks. When he has finished eating, he puts down both of his forks and starts thinking again. One simple solution is to represent each fork / chopstick with a semaphore. A philosopher tries to grab a fork / chopstick by executing a wait() operation on that semaphore. He releases fork / chopstick by executing a signal() operation on the appropriate semaphores. Thus, the shared data are: Semaphore chopstick[5]; Where all the elements of chopstick are initialized to 1.

The structure of Philosopher i do { Wait( take_chopstickC [ i ] ); Wait( take_chopstickC [(i+1) % 5] ) ; /* EATING THE NOODLE */ Signal( put_chopstickC [ i ] ); Signal( put_chopstickC [ (i+1) % 5] ) ; /* THINKING */ } }

Inter Process Communication In general, Inter Process Communication is a type of mechanism usually provided by the operating system. The main aim of this mechanism is to provide communications in between several processes. These are the following methods that are used to provide the synchronization: 1. Mutual Exclusion 2. Semaphore 3. Barrier 4. Spinlock Mutual Exclusion:- It is generally required that only one process thread can enter the critical section at a time. This also helps in synchronization and creates a stable state to avoid the race condition.

Semaphore:- Semaphore is a type of variable that usually controls the access to the shared resources by several processes. Semaphore is further divided into two types which are as follows: 1. Binary Semaphore 2. Counting Semaphore Barrier:- A barrier typically not allows an individual process to proceed unless all the processes does not reach it. It is used by many parallel languages, and collective routines impose barriers. Spinlock:- Spinlock is a type of lock as its name implies. The processes are trying to acquire the spinlock waits or stays in a loop while checking that the lock is available or not. It is known as busy waiting because even though the process active, the process does not perform any functional operation (or task).

Approaches to Interprocess Communication We will now discuss some different approaches to inter-process communication which are as follows: Pipes Shared Memory Message Queue Direct Communication Indirect communication Message Passing FIFO

Pipe:- The pipe is a type of data channel that is unidirectional in nature. It means that the data in this type of data channel can be moved in only a single direction at a time. Still, one can use two-channel of this type, so that he can able to send and receive data in two processes. Typically, it uses the standard methods for input and output. These pipes are used in all types of POSIX systems and in different versions of window operating systems as well. Shared Memory:- It can be referred to as a type of memory that can be used or accessed by multiple processes simultaneously. It is primarily used so that the processes can communicate with each other. Therefore the shared memory is used by almost all POSIX and Windows operating systems as well. Message Queue:- In general, several different messages are allowed to read and write the data to the message queue. In the message queue, the messages are stored or stay in the queue unless their recipients retrieve them. In short, we can also say that the message queue is very helpful in inter-process communication and used by all operating systems.

Direct Communication:- In this type of communication process, usually, a link is created or established between two communicating processes. However, in every pair of communicating processes, only one link can exist. Indirect Communication Indirect communication can only exist or be established when processes share a common mailbox, and each pair of these processes shares multiple communication links. These shared links can be unidirectional or bi-directional. FIFO:- It is a type of general communication between two unrelated processes. It can also be considered as full-duplex, which means that one process can communicate with another process and vice versa.

UNIT - IV UNIT-IV MEMORY MANAGEMENT AND VIRTUAL MEMORY Memory Management and Virtual Memory - Logical versus Physical Address Space, Swapping, Contiguous Allocation, Paging, Segmentation, Segmentation with Paging, Demand Paging, Page Replacement, Page Replacement Algorithms.

MEMORY MANAGEMENT Memory is the important part of the computer that is used to store the data. Its management is critical to the computer system because the amount of main memory available in a computer system is very limited. At any time, many processes are competing for it. Moreover, to increase performance, several processes are executed simultaneously. For this, we must keep several processes in the main memory, so it is even more important to manage them effectively.

Role of Memory management Following are the important roles of memory management in a computer system: Memory manager is used to keep track of the status of memory locations, whether it is free or allocated. Memory manager permits computers with a small amount of main memory to execute programs larger than the size or amount of available memory. It does this by moving information back and forth between primary memory and secondary memory by using the concept of swapping. The memory manager is responsible for protecting the memory allocated to each process from being corrupted by another process. If this is not ensured, then the system may exhibit unpredictable behavior. Memory managers should enable sharing of memory space between processes. Thus, two programs can reside at the same memory location although at different times.

Memory Management Techniques:

Logical Address A logical address is an address that is generated by the CPU during program execution. The logical address is a virtual address as it does not exist physically, and therefore, it is also known as a Virtual Address. This address is used by process as a reference to access the physical memory location by CPU. The logical address is viewed by the user. The user can't view the physical address directly. The logical address is used as a reference to access the physical address. The hardware device called Memory-Management Unit is used for mapping logical addresses to their corresponding physical address.

Physical Address A physical address is the actual address in the main memory where data is stored. Physical addresses are used by the Memory Management Unit (MMU) to translate logical addresses into physical addresses. The user must use the corresponding logical address to go to the physical address rather than directly accessing the physical address. For a computer program to function, physical memory space is required. Therefore, the logical address and physical address need to be mapped before the program is run. Logical addresses are mapped to physical addresses using a page table. The page table contains information about the mapping between logical and physical addresses.

What is Memory Management Unit (MMU) The run-time mapping between the virtual and physical addresses is done by a hardware device known as MMU. The operating system will handle the processes and move the processes between disk and memory in memory management. It keeps track of available and used memory. 

Terms Logical Address Physical Address Definition the CPU generates the logical address while the program is running The physical address is a location in memory. Location The logical address does not exist physically in the memory, and therefore it is sometimes known as a virtual address. The physical address is a location in the memory unit. Access The logical address is used as a reference to access the physical address. The physical address cannot be accessed directly. Address space The set of all the logical addresses generated about a program by the CPU is called Logical Address Space. Whereas all the physical addresses mapped to the logical address is called Physical Address Space.

Swapping Swapping is a memory management scheme in which any process can be temporarily swapped from main memory to secondary memory so that the main memory can be made available for other processes. It is used to improve main memory utilization. In secondary memory, the place where the swapped-out process is stored is called swap space. The purpose of the swapping in operating system is to access the data present in the hard disk and bring it to RAM so that the application programs can use it. The thing to remember is that swapping is used only when data is not present in RAM. Although the process of swapping affects the performance of the system, it helps to run larger and more than one process. This is the reason why swapping is also referred to as memory compaction.

The concept of swapping has divided into two more concepts: Swap-in and Swap-out. Swap-out is a method of removing a process from RAM and adding it to the hard disk. Swap-in is a method of removing a program from a hard disk and putting it back into the main memory or RAM. Example : Suppose the user process's size is 2048KB and is a standard hard disk where swapping has a data transfer rate of 1Mbps. Now we will calculate how long it will take to transfer from main memory to secondary memory. User process size is 2048Kb   Data transfer rate is 1Mbps = 1024 kbps   Time = process size / transfer rate        = 2048 / 1024        = 2 seconds        = 2000 milliseconds   Now taking swap-in and swap-out time, the process will take 4000 milliseconds.   

Advantages of Swapping It helps the CPU to manage multiple processes within a single main memory. It helps to create and use virtual memory. Swapping allows the CPU to perform multiple tasks simultaneously. Therefore, processes do not have to wait very long before they are executed. It improves the main memory utilization. Disadvantages of Swapping If the computer system loses power, the user may lose all information related to the program in case of substantial swapping activity. If the swapping algorithm is not good, the composite method can increase the number of Page Fault and decrease the overall processing performance.

Contiguous Allocation A software or process requires memory space in order to run. As a result, a process must be given a specific amount of memory that corresponds to its needs. Contiguous and non-contiguous memory allocation are the two basic types of memory allocation. Contiguous memory allocation enables the tasks to be finished in a single memory region. Where as non-contiguous memory allocation distributes the procedure throughout many memory locations in various memory sections. We use this contiguous memory allocation strategy to allocate contiguous blocks of memory to each process. Therefore, we allot a continuous segment from the entirely empty area to the process based on its size whenever a process requests to reach the main memory.

Techniques for Contiguous Memory Allocation Input Queues Continuous blocks of memory assigned to processes cause the main memory to always be full. A procedure, however, leaves behind an empty block termed as a hole after it is finished. A new procedure could potentially be implemented in this area. As a result, there are processes and holes in the main memory, and each one of these holes might be assigned to a new process that comes in. First-Fit This is a fairly straightforward technique where we start at the beginning and assign the first hole, which is large enough to meet the needs of the process.

Best-Fit The goal of this greedy method, which allocates the smallest hole that meets the needs of the process. Therefore, in order to select the greatest match for the procedure without wasting memory, we must first sort the holes according to their diameters. Worst-Fit The Best-Fit strategy is in opposition to this one. The largest hole is chosen to be assigned to the incoming process once the holes are sorted based on size.

Paging Paging is a memory management scheme. The main idea behind the paging is to divide each process in the form of pages. The main memory will also be divided in the form of frames. One page of the process is to be stored in one of the frames of the memory. The pages can be stored at the different locations of the memory but the priority is always to find the contiguous frames or holes. Pages of the process are brought into the main memory only when they are required otherwise they reside in the secondary storage. Different operating system defines different frame sizes. The sizes of each frame must be equal. Considering the fact that the pages are mapped to the frames in Paging, page size needs to be as same as frame size.

Example Let us consider the main memory size 16 kb and Frame size is 1 KB therefore the main memory will be divided into the collection of 16 frames of 1 KB each. There are 4 processes in the system that is P1, P2, P3 and P4 of 4 KB each. Each process is divided into pages of 1 KB each so that one page can be stored in one frame. Initially, all the frames are empty therefore pages of the processes will get stored in the contiguous way.

Frames, pages and the mapping between the two is shown in the image below.

Let us consider that, P2 and P4 are moved to waiting state after some time. Now, 8 frames become empty and therefore other pages can be loaded in that empty place. The process P5 of size 8 KB (8 pages) is waiting inside the ready queue. Given the fact that, we have 8 non contiguous frames available in the memory and paging provides the flexibility of storing the process at the different places. Therefore, we can load the pages of process P5 in the place of P2 and P4.

Memory Management Unit The purpose of Memory Management Unit (MMU) is to convert the logical address into the physical address. The logical address is the address generated by the CPU for every page while the physical address is the actual address of the frame where each page will be stored. When a page is to be accessed by the CPU by using the logical address, the operating system needs to obtain the physical address to access that page physically. The logical address has two parts. Page Number Offset Memory management unit of OS needs to convert the page number to the frame number.

Mapping from page table to main memory In operating systems, there is always a requirement of mapping from logical address to the physical address. However, this process involves various steps which are defined as follows. 1. Generation of logical address CPU generates logical address for each page of the process. This contains two parts: page number and offset. 2. Scaling To determine the actual page number of the process, CPU stores the page table base in a special register. Each time the address is generated, the value of the page table base is added to the page number to get the actual location of the page entry in the table. This process is called scaling.

3. Generation of physical Address The frame number of the desired page is determined by its entry in the page table. A physical address is generated which also contains two parts : frame number and offset. The Offset will be similar to the offset of the logical address therefore it will be copied from the logical address. 4. Getting Actual Frame Number The frame number and the offset from the physical address is mapped to the main memory in order to get the actual word address.

Segmentation  In Operating Systems, Segmentation is a memory management technique in which the memory is divided into the variable size parts. Each part is known as a segment which can be allocated to a process. The details about each segment are stored in a table called a segment table. Segment table contains mainly two information about segment: Base: It is the base address of the segment Limit: It is the length of the segment. Till now, we used Paging as our main memory management technique. Paging is more close to the Operating system. It divides all the processes in the form of pages regardless of the fact that a process can have some relative parts of functions which need to be loaded in the same page.

Operating system doesn't care about the User's view of the process. It may divide the same function into different pages and those pages may or may not be loaded at the same time into the memory. It decreases the efficiency of the system. It is better to have segmentation which divides the process into the segments. Each segment contains the same type of functions such as the main function can be included in one segment and the library functions can be included in the other segment.

S No. Paging Segmentation 1 Non-Contiguous memory allocation Non-contiguous memory allocation 2 Paging divides program into fixed size pages. Segmentation divides program into variable size segments. 3 OS is responsible Compiler is responsible. 4 Paging is faster than segmentation Segmentation is slower than paging 5 Paging is closer to Operating System Segmentation is closer to User 6 It suffers from internal fragmentation It suffers from external fragmentation 7 There is no external fragmentation There is no external fragmentation 8 Logical address is divided into page number and page offset Logical address is divided into segment number and segment offset 9 Page table is used to maintain the page information. Segment Table maintains the segment information 10 Page table entry has the frame number and some flag bits to represent details about pages. Segment table entry has the base address of the segment and some protection bits for the segments.

Demand Paging Demand paging can be described as a memory management technique that is used in operating systems to improve memory usage and system performance. Demand paging is a technique used in virtual memory systems where pages enter main memory only when requested or needed by the CPU. In demand paging, the operating system loads only the necessary pages of a program into memory at runtime, instead of loading the entire program into memory at the start. The operating system then loads the required pages from the disk into memory and updates the page tables accordingly.

What is Page Fault? The term “page miss” or “page fault” refers to a situation where a referenced page is not found in the main memory. When a program tries to access a page, that is not currently loaded in physical memory (RAM), an exception occurs known as a page fault. Before enabling the program to access a page that is required, the operating system must bring it into memory from secondary storage (such a hard drive) in order to handle a page fault.

Fragmentation  Fragmentation is an unwanted problem in the operating system in which the processes are loaded and unloaded from memory, and free memory space is fragmented. Processes can't be assigned to memory blocks due to their small size, and the memory blocks stay unused. Contiguous memory allocation allocates space to processes whenever the processes enter RAM. These RAM spaces are divided either by fixed partitioning or by dynamic partitioning. As the process is loaded and unloaded from memory, these areas are fragmented into small pieces of memory that cannot be allocated to coming processes. Types of Fragmentation There are mainly two types of fragmentation in the operating system. These are as follows: Internal Fragmentation External Fragmentation

Internal Fragmentation When a memory block is allocated to a process, and if the process is smaller than the amount of memory allocated, a free space is created in the given memory block. Due to this, the free space of the memory block is unused, which causes internal fragmentation For Example: Assume that memory allocation in RAM is done using fixed partitioning (i.e., memory blocks of fixed sizes). 2MB, 4MB, 4MB, and 8MB are the available sizes. The Operating System uses a part of this RAM.

Let's suppose a process P1 with a size of 3MB arrives and is given a memory block of 4MB. As a result, the 1MB of free space in this block is unused and cannot be used to allocate memory to another process. It is known as internal fragmentation.

How to avoid internal fragmentation? The problem of internal fragmentation may arise due to the fixed sizes of the memory blocks. It may be solved by assigning space to the process via dynamic partitioning. Dynamic partitioning allocates only the amount of space requested by the process. As a result, there is no internal fragmentation. External Fragmentation External fragmentation happens when a dynamic memory allocation method allocates some memory but leaves a small amount of memory unusable. The quantity of available memory is substantially reduced if there is too much external fragmentation. There is enough memory space to complete a request, but it is not contiguous. It's known as external fragmentation.

Let's take the example of external fragmentation. In the above diagram, you can see that there is sufficient space (50 KB) to run a process (05) (need 45KB), but the memory is not contiguous. You can use compaction, paging, and segmentation to use the free space to execute a process.

How to remove external fragmentation? This problem occurs when you allocate RAM to processes continuously. It is done in paging and segmentation, where memory is allocated to processes non-contiguously. As a result, if you remove this condition, external fragmentation may be decreased. Compaction is another method for removing external fragmentation. External fragmentation may be decreased when dynamic partitioning is used for memory allocation by combining all free memory into a single large block. The larger memory block is used to allocate space based on the requirements of the new processes. This method is also known as defragmentation.

Advantages of fragmentation Fast Data Writes Data write in a system that supports data fragmentation may be faster than reorganizing data storage to enable contiguous data writes. Fewer Failures If there is insufficient sequential space in a system that does not support fragmentation, the write will fail. Storage Optimization A fragmented system might potentially make better use of a storage device by utilizing every available storage block.

Disadvantages of fragmentation Need for regular defragmentation A more fragmented storage device's performance will degrade with time, necessitating the requirement for time-consuming defragmentation operations. Slower Read Times The time it takes to read a non-sequential file might increase as a storage device becomes more fragmented.

Virtual Memory A storage method known as virtual memory gives the user the impression that their main memory is quite large. By considering a portion of secondary memory as the main memory, this is accomplished.

Page Replacement Algorithms There are three types of Page Replacement Algorithms. First In First Out Page Replacement Algorithm Optimal Page Replacement Algorithm Least Recently Used (LRU) Page Replacement Algorithm

First In First Out Page Replacement Algorithm This is the first basic algorithm of Page Replacement Algorithms. This algorithm is basically dependent on the number of frames used. Then each frame takes up the certain page and tries to access it. When the frames are filled then the actual problem starts. The fixed number of frames is filled up with the help of first frames present. This concept is fulfilled with the help of Demand Paging. After filling up of the frames, the next page in the waiting queue tries to enter the frame. If the frame is present then, no problem is occurred. Because of the page which is to be searched is already present in the allocated frames. If the page to be searched is found among the frames then, this process is known as Page Hit.

If the page to be searched is not found among the frames then, this process is known as Page Fault. When Page Fault occurs this problem arises, then the First In First Out Page Replacement Algorithm comes into picture. The First In First Out (FIFO) Page Replacement Algorithm removes the Page in the frame which is allotted long back. This means the useless page which is in the frame for a longer time is removed and the new page which is in the ready queue and is ready to occupy the frame is allowed by the First In First Out Page Replacement.

Let us understand this First In First Out Page Replacement Algorithm working with the help of an example. Example: Consider the reference string 6, 1, 1, 2, 0, 3, 4, 6, 0, 2, 1, 2, 1, 2, 0, 3, 2, 1, 2, 0 for a memory with three frames and calculate number of page faults by using FIFO (First In First Out) Page replacement algorithms. Points to Remember Page Not Found - - - > Page Fault Page Found - - - > Page Hit Reference String: Number of Page Hits = 8 Number of Page Faults = 12

OPTIMAL Page Replacement Algorithm This is the second basic algorithm of Page Replacement Algorithms. This algorithm is basically dependent on the number of frames used. Then each frame takes up the certain page and tries to access it. When the frames are filled then the actual problem starts. The fixed number of frames is filled up with the help of first frames present. This concept is fulfilled with the help of Demand Paging After filling up of the frames, the next page in the waiting queue tries to enter the frame. If the frame is present then, no problem is occurred. Because of the page which is to be searched is already present in the allocated frames. If the page to be searched is found among the frames then, this process is known as Page Hit. If the page to be searched is not found among the frames then, this process is known as Page Fault.

When Page Fault occurs this problem arises, then the OPTIMAL Page Replacement Algorithm comes into picture. The OPTIMAL Page Replacement Algorithms works on a certain principle. The principle is: Replace the Page which is not used in the Longest Dimension of time in future This principle means that after all the frames are filled then, see the future pages which are to occupy the frames. Go on checking for the pages which are already available in the frames. Choose the page which is at last.

Example: Suppose the Reference String is: 0, 3, 4, 6, 0, 2, 1, 2, 1, 2, 0, 3, 2, 1, 2, 0 6, 1, 2 are in the frames occupying the frames. Now we need to enter 0 into the frame by removing one page from the page So, let us check which page number occurs last From the sub sequence 0, 3, 4, 6, 0, 2, 1 we can say that 1 is the last occurring page number. So we can say that 0 can be placed in the frame body by removing 1 from the frame. Let us understand this OPTIMAL Page Replacement Algorithm working with the help of an example.

Example: Consider the reference string 6, 1, 1, 2, 0, 3, 4, 6, 0, 2, 1, 2, 1, 2, 0, 3, 2, 1, 4, 0 for a memory with three frames and calculate number of page faults by using OPTIMAL Page replacement algorithms. Points to Remember Page Not Found - - - > Page Fault Page Found - - - > Page Hit Reference String: Number of Page Hits = 8 Number of Page Faults = 12 The Ratio of Page Hit to the Page Fault = 8 : 12 - - - > 2 : 3 - - - > 0.66 The Page Hit Percentage = 8 *100 / 20 = 40%

Least Recently Used (LRU) Replacement Algorithm The LRU stands for the Least Recently Used. It keeps track of page usage in the memory over a short period of time. It works on the concept that pages that have been highly used in the past are likely to be significantly used again in the future. It removes the page that has not been utilized in the memory for the longest time. LRU is the most widely used algorithm because it provides fewer page faults than the other methods. Example: Let's take the following reference string to understand the LRU Page Replacement algorithm. 5 0 1 2 0 3 2 0 3 4 1 0 5 0 4 3 2 1 2 0 1 Find the number of page faults when the LRU page replacement policy is used. Also, consider the page frame size to be three.

Solution: Reference String: 5 0 1 2 0 3 2 0 3 4 1 0 5 0 4 3 2 1 2 0 1 Total number of reference strings = 21 Total number of page faults or page misses = 14 We know that, Total number of page hits = Total number of reference strings - Total number of page faults Total number of page hits = 21 - 14 = 7 Page fault probability = Total number of page faults / Total number of reference strings Page fault probability = 14/21 = 0.67 Page fault percentage = Total number of page faults / Total number of reference strings * 100 Page fault percentage = 14/21*100 = 67%

UNIT - V UNIT-V FILE SYSTEM INTERFACE AND OPERATIONS File System Interface and Operations - Access methods, Directory Structure, Protection, File System Structure, Allocation methods, Free-space Management. Usage of open, create, read, write, close, lseek , stat, ioctl system calls.

FILE-SYSTEM INTERFACE What is a File? Computers store the information on physical storage devices like disks. The operating system provides a structured mechanism (more technically, ‘logical view’) to store this information. ‘File’ is nothing but a logical storage unit of information. A file represents the smallest unit of allocation on logical secondary storage. We cannot store information on our computer unless we store it in files as it is the most basic level of storage. The part of the operating system dealing with files is called  the file system . The file system manages and organizes the files.

File Attributes Each file has characteristics like file name, file type, date (on which file was created), etc. These characteristics are referred to as ‘File Attributes’. The operating system associates these attributes with files. In different operating systems files may have different attributes. Some people call attributes metadata also. Following are some common file attributes: Name:  File name is the name given to the file. A name is usually a string of characters. Identifier:  Identifier is a unique number for a file. It identifies files within the file system. It is not readable to us, unlike file names. Type:  Type is another attribute of a file which specifies the type of file such as archive file (.zip), source code file (.c, .java), .docx file, .txt file, etc. Location:  Specifies the location of the file on the device (The directory path). This attribute is a pointer to a device.

Size:  Specifies the current size of the file (in Kb , Mb, Gb, etc.) and possibly the maximum allowed size of the file. Protection:  Specifies information about Access control (Permissions about Who can read, edit, write, and execute the file.) It provides security to sensitive and private information. Time, date, and user identification:  This information tells us about the date and time on which the file was created, last modified, created and modified by which user, etc.

File Operations The various operations which can be implemented on a file such as read, write, open and close etc. are called file operations. These operations are performed by the user by using the commands provided by the operating system. Some common operations are as follows: Create operation: This operation is used to create a file in the file system. It is the most widely used operation performed on the file system. To create a new file of a particular type the associated application program calls the file system. This file system allocates space to the file. As the file system knows the format of directory structure, so entry of this new file is made into the appropriate directory. Open operation: This operation is the common operation performed on the file. Once the file is created, it must be opened before performing the file processing operations. When the user wants to open a file, it provides a file name to open the particular file in the file system. It tells the operating system to invoke the open system call and passes the file name to the file system.

Write operation: This operation is used to write the information into a file. A system call write is issued that specifies the name of the file and the length of the data has to be written to the file. Whenever the file length is increased by specified value and the file pointer is repositioned after the last byte written. Read operation: This operation reads the contents from a file. A Read pointer is maintained by the OS, pointing to the position up to which the data has been read. Re-position or Seek operation: The seek system call re-positions the file pointers from the current position to a specific place in the file i.e. forward or backward depending upon the user's requirement. This operation is generally performed with those file management systems that support direct access files.

Delete operation: Deleting the file will not only delete all the data stored inside the file it is also used so that disk space occupied by it is freed. In order to delete the specified file the directory is searched. When the directory entry is located, all the associated file space and the directory entry is released. Truncate operation: Truncating is simply deleting the file except deleting attributes. The file is not completely deleted although the information stored inside the file gets replaced. Close operation: When the processing of the file is complete, it should be closed so that all the changes made permanent and all the resources occupied should be released. On closing it deallocates all the internal descriptors that were created when the file was opened.

File Operation Description System Calls / APIs Creating Files Create a new file for data storage. open() (Linux-like systems) CreateFile () (Windows) Creating Directories Create a new directory for organizing files. mkdir()  (Linux systems) CreateDirectory () (Windows) Opening Files Open a file that you already have open to read or write from. open() (Linux systems) CreateFile () (Windows) Reading Files Retrieve data from an open file. read() (Linux systems) ReadFile () (Windows) Writing Files Store data in an open file. write() (Linux systems) WriteFile () (Windows) Renaming Files and Directories If you want to rename a file or directory,. rename() (Linux systems) MoveFile () (Windows) Deleting Files and Directories Remove files or directories. unlink() (Linux systems) remove() (Linux systems) DeleteFile () (Windows) RemoveDirectory () (Windows)

File type Usual extension Function Executable exe, com, bin Read to run machine language program Object obj, o Compiled, machine language not linked Source Code C, java, pas, asm, a Source code in various languages Batch bat, sh Commands to the command interpreter Text txt, doc Textual data, documents Word Processor wp, tex, rrf, doc Various word processor formats Archive arc, zip, tar Related files grouped into one compressed file Multimedia mpeg, mov, rm For containing audio/video information Markup xml, html, tex It is the textual data and documents Library lib, a ,so, dll It contains libraries of routines for programmers Print or View gif, pdf, jpg It is a format for printing or viewing an ASCII or binary file. File type’s

File System The file can be referred to as the collection of data which is in the form of bits/ bytes which are stored in secondary storage like hard drives. File access methods in OS refer to the techniques used to access read and write data from and to a file. When an application program asks for a file, the first request is directed to the logical file system. The logical file system contains the Meta data of the file and directory structure. If the application program doesn't have the required permissions of the file then this layer will throw an error. Logical file systems also verify the path to the file. Generally, files are divided into various logical blocks. Files are to be stored in the hard disk and to be retrieved from the hard disk. Hard disk is divided into various tracks and sectors. Therefore, in order to store and retrieve the files, the logical blocks need to be mapped to physical blocks. This mapping is done by File organization module. It is also responsible for free space management. Once File organization module decided which physical block the application program needs, it passes this information to basic file system.

File Access Methods Let's look at various ways to access files stored in secondary memory. Sequential Access Direct Access Indexed Access All the above file accessed methods in OS have their own benefits and drawbacks. We can use any of the above-mentioned file access methods in OS depending upon the intended use of data the storage medium, and the type of data being stored.

Sequential Access Sequential access is one of the simplest and oldest file access methods in OS. In this, the data is accessed in sequential order, one record at a time. This is useful for log files or media files as they are large files and the file is processed by accessing the data sequentially. We do not have to keep the track of the location of the data as the data is accessed sequentially. In this method, the data is read from the beginning of the file until the desired result is found. It is used in devices like tapes as they have continuous steam of data and the data is written and read sequentially. In the sequential file access method in OS, the data is stored on disk or tape in a continuous block, and each of the records is accessed in the proper order. The position of the tape is changed at each round. We can also use this in batch mode. But sometimes it can be slow for large files where the data is accessed randomly because if the record needs to be modified or deleted, the entire file needs to be written.

Advantages of Sequential Access It is very simple to implement as the data is read from the beginning until the desired result is found. It is very efficient for reading a large file. Used to read the continuous stream of data. The overhead is low. Disadvantages of Sequential Access It is comparatively slow for random access of the data as it only requires reading the data sequentially so it will not work fast in the case of randomly distributed data. It will face difficulty in managing the fragmented data as in this case it will result in wasted storage space. It is inefficient for writing large files where the data needs to be modified or added as we have to rewrite the entire record if the data needs to be modified or deleted. It is limited to single-user access is multiple users try to access the same file sequential access will create a conflict.

Direct Access It is also known as the random access method this is one of the file access methods in OS and is used to read and write the data in a non-sequential order. We can access the data directly by specifying the location of the data on the storage medium. It is commonly used in solid-state drives, hard disks, and the flash drives, where the data is stored in blocks or sectors and each of the blocks or sectors is accessed using a unique address. Direct access allows reading the data randomly through any point without reading all the preceding data. And this feature makes it very useful and efficient in large files as we can access any specific data within the large file, and it is often used in real-time systems where data needs to be accessed quickly.

Advantages of Direct Access We can use direct access for processing large files where the data is not accessed sequentially. It is faster than sequential access as here only the desired result is written or read. It is efficient in indexing structures like B-tress as they allow efficient access to data in large files. It also provides multi-user access. Disadvantages of Direct Access It has a higher overhead compared to sequential access as we need to determine the location of data before accessing it. Reading the data sequentially can be time-consuming. Sometimes it leads to fragmentation which will automatically lead to wastage of storage and hence the reduction in performance.

Indexed Access As the name suggests the indexed access method is one of the file access methods in OS that uses the index to locate the data within a file. In this method, we will create an index that will contain the address of all the data blocks within the file. When a user request to access any part of the data the request is first passed to the index with the help of the index will find the address of that data block and will locate it, then the block is read forms the storage medium. It provides an efficient way to access all the data blocks or records within a large file without having to search the entire file for the data. This method is commonly used in database management systems where efficient and quick access to records is required.

Advantages of Indexed Access It provides fast access to all the data blocks within the file as we can directly locate the data block easily. It is very efficient for large files as it saves a lot of searching time. It has better memory management as it allocation space to the index and data blocks separately. And this will prevent memory overflow. It will improve data integrity by maintaining a separate index for each file. Disadvantages of Indexed Access There can be index overhead with creating and maintaining the index. As we have to update the index every time a record is modified or updated. The storage requirements increase as the additional storage space to store the index The concurrent access is limited as multiple users cannot modify the same index simultaneously. The complexity will increase due to the addition of an index in the file management system.
Tags