# data[].attributes.body
Operating System

What is OS?

A computer program that acts as an intermediary between a user  and the computer hardware.

What OS do?

Controls the hardware and coordinates its use among the  application programs for the users.

Kernel

One program running at all times on the computer

Computer System

Structure

User ↹ Apps ↹ OS ↹ Hardware

User View

•Multi user comp
•Handheld comp
•Embedded comp

System View

OS ➜ resource allocator + control program

# data[].attributes.body
Comp Startup

•Bootstrap program is loaded at power-up or reboot.
•stored in ROM or EPROM(firmware).

Interrupts

Hardware (signal to CPU).
•Software (system call).
•OS (interrupt driven).

Storage hierarchy

•Caching ➜ copying information from slow storage into faster storage system

•Device Driver ➜ Manage I/O

Computer-System Architecture

Single general-purpose processor

designed to execute exactly one program.

Multiprocessors aka  parallel systems, tightly-coupled
systems

types :-
•Symmetric Multiprocessing ➜ performs all tasks
•Asymmetric Multiprocessing ➜ specific task


Advantages :-
•Increased throughput
•Economy of scale
•Increased reliability

What is Multicore?

multiple computing cores on a single chip(faster/less power)

Clustered Systems

•share via storage-area network (SAN)
•high-availability service

•Asymmetric clustering ➜ 1 machine in hot-standby mode
•Symmetric clustering ➜ multiple nodes running applications, monitoring each other

Some:-
•for high-performance computing (HPC),write use parallelization
•have distributed lock manager (DLM)

Multiprogrammed System

Batch system ➜ One job selected and run via job scheduling

Timesharing systems ➜ creating interactive computing
•respond time < 1 sec
•must execute >= 1 program(process)

•use CPU scheduling for multi process

•use swapping if processes do not fit in memory

•Virtual memory enables a computer to be able to compensate shortages of physical memory by transferring pages of data from random access memory to disk storage

Modes of operation

•User mode
•Kernel mode
•Mode bit (0 or 1)

Timer

To prevent process to be in infinite loop

Program counter

Specifying location of next instruction to execute

file

Abstracts physical properties to logical storage unit

Protection and Security

Protection ➜ controlling access of processes (or users)  to resources
Security ➜ defense of the system against internal and external attacks

Filter by
User ID/Group ID/Privilege escalation

# data[].attributes.body
provide an environment for execution of programs and services to programs and users

Helpful to user

user interface ➜ Command-Line (CLI), Graphics User Interface (GUI),  touch-screen,  Batch

Program execution ➜ The system must be able to load a program into memory and to run that program, end execution, either normally or abnormally (indicating error)

I/O operations ➜  A running program may require I/O, which may involve a file or an I/O device

File-system manipulation ➜  The file system is of particular interest. Programs need to read and write files and directories, create and delete them, search them, list file Information, permission management

Communications ➜ Processes may exchange information, on the same computer or between computers over a network
Communications may be via shared memory or through message passing (packets moved by the OS)

Error detection ➜
•May occur in the CPU and memory hardware, in I/O devices, in user program

•For each type of error, OS should take the appropriate action to ensure correct and consistent computing

•Debugging facilities can greatly enhance the user’s and programmer’s abilities to efficiently use the system1.

Ensuring the efficient operation
Resource allocation ➜ When  multiple users or multiple jobs running concurrently, resources must be allocated to each of them
-Many types of resources -   CPU cycles, main memory, file storage, I/O devices.

2.Logging ➜ To keep track of which users use how much and what kinds of computer resources

3.Protection and security ➜ The owners of information stored in a multiuser or networked computer system may want to control use of that information, concurrent processes should not interfere with each other

Protection ➜involves ensuring that all access to system resources is controlled

Security ➜ of the system from outsiders requires user authentication, extends to defending external I/O devices from invalid access attempts

User Interface

CLI/Command Line
shells ➜ multiple flavours

GUI/Graphical
user friendly
•icons represent files
•mouse button over objects

Touchscreen
•gestures
•virtual keyboard
•voice command

System calls

•via high-level application programming interface(API)
•interface-maintains a table indexed according to these numbers

# data[].attributes.body
I learnt that computer system have  many components within it to do the tasks. all of it work together to do their job. This chapter pretty easy to understand but it have a lot of things to cover on.

# data[].attributes.body
Process Concept
process ➜ a program in execution

parts:-

text section
program counter
stack
data section
heap

program  ➜ passive , process - active
program ➜ process if exe file loaded into memory

# data[].attributes.body
process control block(PCB)
-data structure that contains information of the process related to it

process state - running

program counter ➜ loc next exe

cpu scheduling info ➜ priorities

memory management information ➜ memory allocated to the process

accounting info ➜ cpu used

i/o status info ➜ i/o device allocated to process

Process Scheduling

process scheduler ➜ selects available process for next execution on CPU core

scheduling queues

ready queue ➜ ready & waiting to execute

wait queues ➜ waiting for an event

Context switch

save the state (old process) ➜ saved state(new process) via context switch
context of a process ➜ PCB

Process Creation

tree of process(parent->children) via process identifier (pid)

Process Termination

-after last statement, OS use exit() system call
-return via wait()
-terminate via abort()

Interprocess Communication

-independent(cannot affect/affected)/cooperating(can affect/affected)

-need interprocess communication(IPC)-> (shared memory/passing message)

producer consumer problem (buffer)

unbounded- size no limit

bounded -fixed size

message passing

-two ops->send & receive

-need comm link

Synchronization

blocking = synchronous
blocking send ➜ sender blocked until  received
blocking receive ➜ receiver blocked until available
non blocking = asynchronous
non- blocking send
non blocking receive

both = rendezvous

# data[].attributes.body
process state

# data[].attributes.body
Types of system calls

•process control(end, abort, load, execute..)

•File management(create/delete/close file..)

•Device management(request/release device..)

•Information maintenance(get/set -> time/data..)

•Communication(create/delete comm connection)

•Protection(control, permission, allow/deny)

System Services

•File management(manipulate files/directories)•Status information(use registry)

•File modification(use text editor)

•Programming language support
(Absolute/relocatable/overlay loaders)

•Communication(virtual connections)

•Background services(services, subsystems, daemon)

•Application programs

# data[].attributes.body
process = heavy , thread = light

Benefits

responsiveness ➜ may continue execute even if blocked

resource sharing ➜ thread share rss

economy ➜ cheaper than process creation . thread switching < context switching

scalability ➜ take adv of multicore arc

-multiple thread can execute concurrently in a process

- lightweight bcs have own stack/can access shared data/share same address space

challenge of multicore/multi processor

-dividing activities

-balancce

-data splitting

-data dependency

-testing and debugging

parallelism ➜ more than one task simultaneously

concurrency  ➜ more than one task making progress

multithreading model

many-to-one
many user thread ➜ 1 kernel thread

1 block ➜ all block
may not run in parallel

one-to-one
1 user thread(UT)➜ 1 kernel thread(KT)

create UT ➜ create KT
more concurrency,may restricted due to overhead

many-to-many
many UT ➜ many KT
allow OS create sufficient number of KT

Two level model
like many-to-many but bound to kernel thread

# data[].attributes.body
CPU Scheduler

-put CPU core into processes in ready queue
-nonpreemptive(<1-4) else preemptive

-dispatch latency ➜ time for dispatcher stop a process and start another running
scheduling criteria

Scheduling criteria

CPU utilization(max) ➜ keep it busy

throughput(max) ➜ num of process execute/time

turnaroundtime(min) ➜ total time to execute a process
waiting time(min) ➜ total time wait in ready queue

response time(min) ➜ total time from request until first response

FIRST COME FIRST SERVE
-in arrival order

SHORTEST JOB FIRST
-shortest time

SHORTEST REMAINING TIME FIRST
-FCFS but have preemptio

ROUND ROBIN
-based on quantum time

-after elapsed ,preempted

PRIORITY(preemptive/nonpreemptive)
-smaller integer = higher priority
-use aging(time increase,increase priority) to overcome starvation(never execute)

architectures

multicore cpu ➜ faster,less power

multithreaded multicore system

make progress on another thread while memory retrieve happens

hypertrading(assign each core multiple hardware threads)

multiple processor scheduling (load balancing)

load balancing ➜ keep workload evenly distributed

push migration ➜ checks load ,pushes task from overloaded CPU to other CPUs

pull migration ➜ pulls waiting task from busy processor

real time CPU scheduling

soft real time systems ➜ critical real time task(highest priority)

hard real time systems ➜ task must be serviced by itds deadline

latencies

interrupt-time (arrival of interupt -> start of routine that services interupt)
dispatch-time for schedule to take current process off CPU and switch to another

priority based scheduling

-must support preemptive for real time scheduling

-new characteristics ➜ periodic one require CPU at constant interval

-rate=1/p

# data[].attributes.body
Race condition

-A race condition is an undesirable situation that occurs when a device
or system attempts to perform two or more operations at the same time.

-The operations must
be done in the proper sequence to be done correctly.

Critical section problem

-critical section is a segment of code which can be accessed by a signal process at a specific point of time.

-The section consists of
shared data resources that required to be accessed by other processes.

- to ensure the orderly execution of cooperating processes

3 requirements

1. Mutual Exclusion - If process Pi is executing in its critical section, then no other processes can be executing in their critical sections

2. Progress - If no process is executing in its critical section and there exist some processes that wish to enter their critical section, then the selection of the processes that will enter the critical section next cannot be postponed indefinitely

3.Bounded Waiting -  A bound must exist on the number of times that other processes are allowed to enter their critical sections after a process has made a request to enter its critical section and before that request is granted

2 approach

1.Preemptive – allows preemption of process when running in kernel mode

2.Non-preemptive – runs until exits kernel mode, blocks, or voluntarily yields CPU

Peterson's solution

a concurrent programming algorithm for mutual exclusion that allows
two or more processes to share a single-use resource without conflict,
using only shared memory for communication

# data[].attributes.body
Mutex lock

A mutex provides mutual exclusion, either producer or consumer can have the key (mutex) and proceed with their work. As long as the buffer is filled by the producer, the consumer needs to wait, and vice versa.

Semaphore

A semaphore is a generalized mutex. In lieu of a single buffer, we can split the 4 KB buffer into four 1 KB buffers (identical resources). A semaphore can be associated with these four buffers. The consumer and producer can work on different buffers at the same time.

# data[].attributes.body
characterlization

mutual exclusion-1 process at 1 time to use resources
hold and wait-a process holding at least one resource is waiting to acquire additional resources held by other processes
no preemption-a resource can be released only voluntarily by the process holding it, after that process has completed its task

resource allocation graph

v into 2 types
p=processes
r=resources

request edge = p->r
assignment edge = r->p

facts
no cycle = no deadlock

contain cycle
1.one instance per resource=deadlock
2.several instances per resource type = possible of deadlock

handling deadlock

prevention
Mutual Exclusion – not required for sharable resources (e.g., read-only files); must hold for non-sharable resources

Hold and Wait – must guarantee that whenever a process requests a resource, it does not hold any other resources

No Preemption –
-If a process that is holding some resources requests another resource that cannot be immediately allocated to it, then all resources currently being held are released
-Preempted resources are added to the list of resources for which the process is waiting
-Process will be restarted only when it can regain its old resources, as well as the new ones that it is requesting

Circular Wait – impose a total ordering of all resource types, and require that each process requests resources in an increasing order of enumeration

deadlock avoidance

-each process need to declare the maximum number of resources of each type that it may need
-examines the resource-allocation state to ensure that there can never be a circular-wait condition
-Resource-allocation state is defined by the number of available and allocated resources, and the maximum demands of the processes

safe state

-the system can allocate all the resources requested by all the processes without entering into deadlock.
-If the system cannot fulfill the request of all processes then the state of the system is called unsafe.

avoidence algorithms

-Single instance of a resource type(Use a resource-allocation graph)
-Multiple instances of a resource type(Use the banker’s algorithm)

# data[].attributes.body
banker algorithm
-simulating the allocation for predetermined maximum possible amounts of all resources, then makes an “s-state” check to test for possible activities, before deciding whether allocation should be allowed to continue.

safety algorithm

1.Initialize: Work = Available
2.Determine Need; Need = Max – Allocation
3.Check the process Pi  and compare the Needs of Pi and Work,
Need of Pi =< Work
4.If True
Pi enter safe state sequence
Go to Step 5.
Else
Pi = Pi + 1
Go to step 3
End
5.Work = Work + AllocationiFinish[i] = true
Pi = Pi + 1
Go to step 3
6.If all processes get the resource, then the system is in a safe state

# data[].attributes.body
memory protection
-implemented using the base registers and limit registers.
-base register (holding the smallest legal physical address of a program
-limit register (specifies the size of the program) define the boundary of a program in memory in memory)

address binding
-the mapping of computer instructions and data to physical memory locations.
-input queue(kept binary executeble file)

type of addresses
-program(symbolic address)
-secondary address(logical address)
-main memory(physical address)

three types of an address binding in the OS
-Compile time  (the time at which the source code is converted into an executable code)
-Load time (when the OS is reading an executable from long term storage)
-Execution time (instructions in the computer program

# data[].attributes.body
multiple partition allocation

-Variable-partition(the memory space has variable size to  accommodate process’ needs.)

-Hole(block of available memory; holes of various size are scattered throughout memory)

# data[].attributes.body
Allocate the first hole that is big enough

# data[].attributes.body
-Allocate the smallest hole that is big enough
-Must search entire list, unless the list is ordered by size
-Produces the smallest leftover hole

# data[].attributes.body
-Allocate the largest hole;
-Must also search entire list, unless the list is ordered by size
-Produces the largest leftover hole

# data[].attributes.body
External Fragmentation – total memory space exists to satisfy a request, but it is not contiguous and therefore cannot be used.

Internal Fragmentation – allocated memory may be slightly larger than requested memory.

# data[].attributes.body
segmentation

Mapping is done via  Segment table:
base – contains the starting physical address where the segments reside in

memory
limit – specifies the length of the segment

Segment table is kept in memory-
Segment-table base register (STBR) points to the segment table’s location in memory
Segment-table length register (STLR) indicates number of segments used by a program

paging

-Divide physical memory into fixed-sized blocks called frames

-Size of  a frame  is power of 2  between 512 bytes and 16 Mbytes

-Divide logical memory into blocks of same size as frames called pages

-Backing store, where the program is permanently residing, is also split  into storage units (called blocks), which are the same size as the frame and page

Set up a page table to translate logical to physical addresses

Page table is kept in memory.
Page-table base register (PTBR) points to the page table

Page-table length register (PTLR) indicates size of the page table

# data[].attributes.body
1.Number of pages required   = Process size/ page size
2.Total internal fragmentation     = page size -  space used     =

# data[].attributes.body
Shared code
-One copy of read-only code shared among processes (i.e., text editors, compilers, window systems) Similar to multiple threads sharing the same process space Also useful for inter-process communication if sharing of read-write pages is allowed

Private code and data
-Each process keeps a separate copy of the code and data The pages for the private code and data can appear anywhere in the logical address space

# data[].attributes.body
Backing store
– fast disk large enough to accommodate copies of all memory images for all processes
-must provide direct access to these memory images

-System maintains a ready queue of ready-to-run processes which are either in memory or have memory images on disk.

# data[].attributes.body
So after class, I read the slide again to relearn  it. firstly , OS services can be helpful to us as user or for the operation . It  is easier to understand when I imagine it in real life situation like we use OS interface /file to do task while for efficiency of course we need security and protection from virus. OS provide us with windows defender.

# data[].attributes.body
In this chapter, I learnt that every execution will change the state of the process. I able to understand how the process going from one state to another state .other than that, inter process communication is really important in term of efficiency.

# data[].attributes.body
I do not have any problem with this chapter. It is really short and simple . It is about thread ,multi thread and the model , multiprocessor, multiprocessor

# data[].attributes.body
This is the hardest chapter from 1 to 5. During class, I cannot understand it clearly so I re-watch the recorded class and watch on YouTube to understand about scheduling  algorithm. I also make some exercises regarding it.

# data[].attributes.body
what I can conclude , each approach have advantages and disadvantages. so we need to pick the most suitable approach
based on the problem.

# data[].attributes.body
for this chapter, I able to understand how to do the calculation for the safety algorithm. after class, I watch video from the slide to make sure I get everything right. It use basic calculation but we need to do it step by step to avoid careless mistake

# data[].attributes.body
For this chapter , I relearn some topic during my diploma .I still remember the concept of first, best and worst fit .because of that ,i do not have any problem to understand it.

# data[].attributes.body
Demand paging

-bring into memory when needed
-less i/o and memory,fast,more userslazy swapper-only swap when needed

valid-invalid bit

-v(in memory),i(not in memory)

page fault

occur:-
1-invalid->abort
 -not in memory,step 2
2-find free frame
3-Swap page into frame via scheduled disk operation
4-Reset tables to indicate page now in memory
Set validation bit = v
5-Restart the instruction that caused the page fault

Pure demand paging – start process with no pages in memory

trap->state->determine->check->issue->allocate->receive->state->determine->correct->wait->restore

page replacement

-need page into memory
-no free frame