OS Lecture
OS Lecture
Recommended Books
2
Operating System
William Stalling
Operating System Concepts
Abraham Silberschatz
Contents
3
Microsoft Windows,
macOS,
Linux,
Unix.
Each type of operating system has its strengths and
is designed for specific use cases, ranging from
personal computers to servers and embedded
systems.
What is an Operating System
9
OS is a resource allocator
Manages all resources
Decides between conflicting requests for efficient and
fair resource use
OS is a control program
Controls execution of programs to prevent errors and
improper use of the computer
What is an Operating System
10
Computer-system operation:
One or more CPUs, device controllers connect through
common bus providing access to shared memory.
Concurrent execution of CPUs and devices competing for
memory cycles.
Computer-System Operation
17
Time-Sharing or Multi-User/Multi-Tasking
Operating Systems:
Enables multiple users to interact with the system
concurrently.
Time is shared among users, and each user gets a small
time slice for their task.
Provides the illusion of simultaneous execution.
Examples: UNIX, Linux, modern versions of
Microsoft Windows.
78
OPERATING SYSTEM
RESOURCE MANAGEMENT
Lecture#13-14
82
Resource Sharing
CPU
Memory
Printer
Files
etc
84
Printer
Keyboard
Ram
Disk
Files on Disk
Networks
Multiprocessors
92
Scheduling
Algorithm that executes to determines which app
should execute next
Need to be fair
Needs to able to prioritize some Apps over the other
Memory Management
97
Executing Programs(Process)
98
Executing Programs(Process)
99
Sharing RAM
100
Single Contiguous Model
101
No Sharing
One process occupies RAM at a time
When one process completes, another process is allocated
RAM
Drawback
Process memory size is restricted b RAM
Single Contiguous Model
102
No Sharing
One process occupies RAM at a time
When one process completes, another process is allocated
RAM
Drawback
Process memory size is restricted b RAM
Single Contiguous Model
103
No Sharing
One process occupies RAM at a time
When one process completes, another process is allocated
RAM
Drawback
Process memory size is restricted b RAM
Single Contiguous Model
104
No Sharing
One process occupies RAM at a time
When one process completes, another process is allocated
RAM
Drawback
Process memory size is restricted b RAM
Partition Model
105
Partition Model
(allocating and deallocating processes)
106
Partition Model
(allocating and deallocating processes)
107
Partition Model (fragmentation)
108
Partition Model
(finding the right fit)
109
Free space
Free space
Partition Model
(finding the right fit)
110
Free space
Free space
Partition Model
(finding the right fit)
111
Free space
Free space
Partition Model
(finding the right fit)
112
Free space
Free space
Partition Model (Deallocation)
113
Free space
Free space
Partition Model (Deallocation)
114
Free space
OS should detect where are three contiguous free blocks
to merge to make a one complete free block
New process Free space
Free space
Limitations
115
116
OPERATING SYSTEM
VIRTUAL MEMORY
Lecture#13-14
Virtual Memory
117
Virtual Memory
118
Ready Queue:
Processes that are residing in main memory and are
ready to execute are placed in the ready queue.
The CPU scheduler selects processes from this queue
for execution based on the scheduling algorithm.
Process Scheduling Queue
158
Device Queue:
Processes waiting for a particular I/O device or
resource are placed in device queues.
These queues are associated with specific I/O devices
such as printers, disk drives, etc.
Once the I/O operation is complete, the process is
moved back to the ready queue.
Process Scheduling Queue
159
Waiting Queue:
Processes that are waiting for a particular event to
occur (other than I/O) are placed in the waiting queue.
For example, a process waiting for a signal or a
message.
Process Scheduling
160
Suspended Queue:
Processes that are temporarily taken out of main
memory and placed in secondary storage due to low
priority or resource limitations are in the suspended
queue.
They may be brought back to the ready queue later
when the system resources become available.
Queueing-diagram representation of process
scheduling
161
Schedulers
Short-term scheduler is invoked very frequently
(milliseconds) (must be fast)
Long-term scheduler is invoked very infrequently (seconds,
minutes) (may be slow)
The long-term scheduler controls the degree of
multiprogramming
Processes can be described as either:
I/O-bound process – spends more time doing I/O than
computations, many short CPU bursts
CPU-bound process – spends more time doing computations;
few very long CPU bursts
Addition of Medium Term Scheduling
Context Switch
When CPU switches to another process, the system
must save the state of the old process and load the
saved state for the new process via a context switch
Context of a process represented in the PCB
Context-switch time is overhead; the system does no
useful work while switching
Time dependent on hardware support
Operations on Processes
165
Process Creation
Process Termination
Process Creation
Parent process create children processes, which,
in turn create other processes, forming a tree of
processes
Generally, process identified and managed via a
process identifier (pid)
Resource sharing
Parent and children share all resources
Children share subset of parent’s resources
Parent and child share no resources
Process Creation (Cont.)
Address space
Child duplicate of parent
Child has a program loaded into it
UNIX examples
fork system call creates new process
exec system call used after a fork to replace the
process’ memory space with a new program
168
responsible for
responsible for managing clients that managing clients
directly log onto the system. that connect to the
system by using ssh
Process Creation
When a process creates a new process, two
possibilities for execution exist:
Parent and children execute concurrently
Parent waits until children terminate
Process Creation
There are also two address-space possibilities for
the new process:
The child process is a duplicate of the parent process (it
has the same program and data as the parent).
The child process has a new program loaded into it
Creating a process by Cloning
173
Cloning
Child process is exact replica of the parent
Fork system call
Creating a process by Cloning
174
Cloning
Child process is exact replica of the parent
Fork system call
Creating a process by Cloning
175
Creating a process by Cloning
176
Fork system call from OS perspective
177
Fork system call from OS perspective
178
Fork system call from OS perspective
179
Fork system call from OS perspective
180
Child Process in Ready Queue
181
Copying Page Table
182
Copying Page Table
183
Duplicating Page Tables
184
Copying Page Table
185
Example
186
Example
187
Process Termination
188
The parent is exiting, and the operating system does not allow
a child to continue if its parent terminates
196
197
198
Interprocess Communication
199
Information Sharing:
Enable multiple applications to access and share the
same piece of information concurrently.
Computation Speedup:
Achieve faster execution of a particular task by
breaking it into subtasks and executing them in
parallel.
Multiple processing cores are necessary to achieve
parallel execution.
Example: If a computationally intensive task can be
divided into smaller subtasks, each subtask can be
executed concurrently on separate processing cores,
resulting in a speedup of the overall computation.
Interprocess Communication
206
Modularity:
Construct the system in a modular fashion by dividing
system functions into separate processes or threads.
This promotes a more organized and maintainable
system architecture.
Example: In a complex software system, different
modules or components may perform distinct functions.
By implementing these functions as separate processes
or threads, the system becomes more modular, making it
easier to understand, develop, and maintain.
Interprocess Communication
207
Memory Allocation:
A section of memory is allocated by an operating system or a
controlling process. This section is designated for sharing among
multiple processes.
Attaching Processes:
Processes that want to communicate with each other attach themselves
to the shared memory region. This attachment is typically done using
system calls or library functions provided by the operating system.
Data Exchange:
Processes can now read from and write to the shared memory region.
Any changes made by one process in the shared memory space are
immediately visible to other processes attached to the same region.
213
Synchronization:
Since multiple processes can access the shared
memory simultaneously, synchronization mechanisms
used to prevent conflicts and ensure data consistency.
Detachment and Cleanup:
When processes are done with the shared memory,
they can detach from it.
Advantages of Shared Memory Model:
214
For example:
An Internet chat program could be designed so that
chat participants communicate with one another by
exchanging messages.
220
Many-to-One threading
models
Maps Many user level threads
to one kernel level thread
Disadvantage of Many-to-
One Model
The entire process will block if
a thread makes a blocking
system call
Because only one thread can
access the kernel at a time,
multiple threads are unable to
run in parallel on
multiprocessors.
Threading Models
253
One-to-One Model
The one-to-one model maps
each user thread to a kernel
thread.
Many-to-Many Model
Multiplexes many user level
threads to a smaller or equal
number of kernel level threads
THREADING ISSUES
Lecture#29-31
Threading Issues
256
Issue
If one thread in a process calls
fork(), does the new process
duplicate all threads, or is the new
process single- threaded?
Threading issues
261
Solution:
Some UNIX systems have chosen
to have two versions of fork(),
one that duplicates all threads
another that duplicates only the
thread that invoked the fork()
system call.
262
Asynchronous cancellation:
1. The thread is terminated immediately upon cancellation
request, regardless of its current state or what it might be
doing.
Deferred Cancellation:
In Situations where:
Asynchronous cancellation:
Resources have been allocated to a canceled thread
CPU SCHEDULING
Lecture#
CPU Scheduling
277
Note: Assuming after every 15th CPU cycle P1,P2,P3 & P4 are arriving
Dealing with Starvation
313
DEADLOCKS
Lecture#42-43
Deadlock
319
It is a directed graphs
Used to model resource allocations in the system to
determine the whether the deadlock occurs in the
system or not.
Resource Allocation Graph
325
It is a directed graphs
Used to model resource allocations in the system to
determine the whether the deadlock occurs in the
system or not.
Resource Allocation Graph
326
It is a directed graphs
Used to model resource allocations in the system to
determine the whether the deadlock occurs in the
system or not.
Resource Allocation Graph
327
It is a directed graphs
Used to model resource allocations in the system to
determine the whether the deadlock occurs in the
system or not.
Necessary conditions for Deadlock
328
SYNCHRONIZATION
Lecture#43-45
Motivating Scenario
340
Motivating Scenario
341
Motivating Scenario
342
Motivating Scenario
343
Motivating Scenario
344
Race Condition
345
Race Condition in Multicore
346
Critical Section Solution
347
Solution to Critical Section problem
348