# data[].attributes.body
Operating system is like intermediary person between user and the program.

Its interact with hardware and application.

# data[].attributes.headline
Computer System can be divided into different components?

# data[].attributes.subject
Computer System can be divided into different components?

# data[].attributes.body
Yes, computer system can be divided by 4 components.

User

Application

Operating System

Hardware

# data[].attributes.body
User : people, machines, other computers.

Application : word processors, compilers, web browsers.

Operating System :

control and coordinates use of hardware

e.g. : Windows, Macintosh, Android, IOS, Linux.

Hardware:

provides basic computing resources

resources : cpu, memory, I/O devices, storage.

# data[].attributes.body
Operating system provides the means for proper use of these resources (cpu, memory, i/o devices, storage) in the operation of the computer system.

Provides an environment within which other programs can do useful work.

# data[].attributes.body
User view

System view

# data[].attributes.body
ease of use

good performance

efficient

# data[].attributes.body
Share resources and may exchange information

fairness in utilization the resources

# data[].attributes.body
Mobile computers (e.g., smartphones and tablets)
To provide easy accessibility

optimized for usability & battery life

# data[].attributes.body
Embedded computers(e.g., computers in home devices, washing machine)

Run without user intervention

# data[].attributes.body
1. Operating System is a resource allocator

Manages all resources(cpu, memory, i/o, storage)

Decides between conflict request for efficient & fair resource use

2. Operating System is a control program

Security system

Control execution of programs to prevent errors

# data[].attributes.attachment_caption
The diagram show the modern computer system.

# data[].attributes.body
Each of the I/O devices has a controller.

Each of the controller has its own buffer.

The function of buffer is to store data that required by its own devices.

The device controller and buffers are connected to memory subsequently connected to our CPU.

CPU will take data/instruction from memory to process it.

# data[].attributes.headline
How does CPU know any task to be process or to be execute?

# data[].attributes.subject
How does CPU know any task to be process or to be execute?

# data[].attributes.body
The CPU will be request that some task/services to be process or to be execute.

The answer is Interrupt.
Any event that request the CPU to act is known by interrupt.

# data[].attributes.body
Thread is the smallest sequence of programmed instructions that can be managed independently by a scheduler for execution

A process may have 1 or many threads to complete their execution
- process with 1 thread - single threaded process
- process with many thread - multi threaded process

The sharing function of thread makes it lighter, easier to be executed and easier to create the thread.

# data[].attributes.body
The straight line in the diagram shows that the CPU is executing the process.
When interrupt occur in the CPU, the CPU will be stop for a while.
Its stop to handling a new request.
Then, the CPU will continue the process of executing the request that occur based on the priority of the request.

# data[].attributes.body
The occurrence of an event is usually signaled by an interrupt.

There are 2 types of interrupt :

Hardware

Software

Hardware Interrupt

I/O devices that trigger the interrupt by sending a signal to CPU through the System Bus.

Software Interrupt

Software program that trigger the interrupt by another software known by system call.

# data[].attributes.body
Direct memory access is designed so that less interrupt occurs.
The device controller transfers an entire block of data to or from its own buffer to memory with no intervention by the CPU.

# data[].attributes.body
Most CPU design nowadays includes multiple computing cores on a single chip. Such multiprocessor systems are called multicore.

# data[].attributes.body
There is 2 types of services:
1. For User
2. For System Programs

Operating System Services provide their function for both types.

# data[].attributes.headline
OS Services that are helpful for the system programs.

# data[].attributes.subject
OS Services that are helpful for the system programs.

# data[].attributes.body
1. 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
owners information stored in a multiuser or networked computer system may want to control use of that information and 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.

# data[].attributes.body
1. User interface

Command Line Interface(CLI), Graphical User Interface(GUI), Touch Screen, Batch

2. Program execution

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

3. I/O operations

A running program may require I/O, which may involve a file or an I/O devices.

4. File-system manipulation

Programs need to read and write files or directories, create, delete, search, list file information, and permission management for file.

5. Communication

Processes may exchange information, on the same computer or between another computer over a network

6. Error detection (aware of possible errors in the OS)

error may occur in the CPU and memory hardware, I/O devices, or in user program.

OS should take the appropriate action of the error.

Debug the facilities can enhance the user's and programmer's abilities to efficiently use the system.

# data[].attributes.body
Programming interface to the services provided by the OS

Is a system operation that have instruction to tell the CPU what had to be done to execute the task.

It is a set of instruction.

Use High Level Language (API)

# data[].attributes.body
Explanation:
When user click open of the file directories.
System call will switch from user mode to kernel mode, where kernel mode is the part of the operating system want to execute the task.
System call will take the instruction of the task and implement the instruction to do the task that user want.
The user can view the file directories.

# data[].attributes.body
Process control

File Management

Device Management

# data[].attributes.body
Provide a convenient environment for program development and execution.
Can be divided into:

File manipulation

Status information sometimes stored in a file

Programming language support

Program loading and execution

Communications

Background services

Application programs

Most user's view of the operation system is defined by system programs, not the actual system calls.

# data[].attributes.body
Process - a program in execution; process must progress in sequential fashion.

Multiple parts

program code also known as text section.

current activity including program counter

Stack containing temporary data

Data section containing global variables

Heap containing memory dynamically allocated during time

Program is passive entity; process is active

Program becomes process when execute file loaded into memory

Execution of program started via GUI mouse clicks, cmd entry of its name, etc

One program can be several processes

Consider multiple users executing the same program

# data[].attributes.body
When a program in the secondary storage is being execute, that program will be transfer into memory and becoming a process.

# data[].attributes.body
As a process executes, it changes state.
A process at any one time can be at any state:

New - the process  is being created.

Running - Instructions are being executed

Waiting - The process is waiting for event to occur

Ready - The process is waiting to be assigned to a processor

Terminated - The process has finished execution

# data[].attributes.body
For each process, it has process control block (PCB).

It keeps the information of that process.

OS will generate the process control block (PCB).

Process State - running, waiting, etc.

Program Counter - location of instruction to next execute.

CPU Registers - contents of all process-centric registers

CPU scheduling information - priorities, scheduling queue pointers.

Memory-management information - memory allocated to the process.

Accounting information - CPU used, clock time elapsed since start, time limits.

I/O status information - I/O devices allocated to process, list of open files

# data[].attributes.body
A process can have many threads.
so far, process has a single thread of execution.
This topic will be explain more on the next chapter.

# data[].attributes.body
maximize CPU use, quickly switch processes onto CPU core

Process scheduler selects among available processes for next execution on CPU core

Maintains scheduling queues of processes

1. Ready queue - set of all processes residing in main memory, ready and waiting to execute.

2. Waiting queue - set of processes waiting for an event to occur. example: I/O

# data[].attributes.body
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

# data[].attributes.body
System must provide mechanisms for:

process creation

process termination

# data[].attributes.body
Parent process create children processes, which, in turn create other processes, forming a tree of processes.

Process identified and managed via a process identifier(pid)

Resource sharing options
- Parent and children share all resources
- Children share subset of parent's resources
- Parent and child share no resources

Execution options
- Parent and children execute simultaneously
- Parent waits until children terminate

Address space
- Child duplicate of parent
- Child has a program loaded into it

# data[].attributes.body
Process executes last statement and then asks the os to delete it using exit ( ) system call.
- Returns status data from child to parent (via wait ( ))
- Process' resources are deallocated by operating system

Parent may terminate the execution of children processes using the abort ( ) system call.

Some reasons for doing so:
- Child has exceeded allocated resources
- Task assigned to child is no longer required
The parent is exiting and the os does not allow a child to continue if its parent terminates.

# data[].attributes.body
Proses may be independent or cooperating.

# data[].attributes.body
Shared memory

Message passing

# data[].attributes.body
Paradigm for cooperating processes, producer process produces information that is consumed by a consumer process

unbounded-buffer places no practical limit on the size of the buffer

bounded-buffer assumes that there is a fixed buffer size

# data[].attributes.headline
Interprocess Communication - Shared Memory

# data[].attributes.subject
Interprocess Communication - Shared Memory

# data[].attributes.body
An area of memory shared among the processes that wish to communicate.

The communication is under the control of the users processes not the os.

Major issues is to provide the mechanism that will allow user processes to synchronize their actions when they access shared memory.

# data[].attributes.headline
Interprocess Communication - Message Passing

# data[].attributes.subject
Interprocess Communication - Message Passing

# data[].attributes.body
Mechanism for processes to communicate and to synchronize their actions

The operations:
- send(message)
- receive(message)

Message size is either fixed or variable

If process A and process B wish to communicate with each other, they will need to:
- establish a communication link between them
- exchange messages via send or receive

Major issues for message passing is:
- how the links are being established.
- the capacity of a link.
- the direction of link which is unidirectional or bi-directional.
- can a link be associated with more than two processes?

# data[].attributes.headline
Implementation of communication link (Message Passing)

# data[].attributes.subject
Implementation of communication link (Message Passing)

# data[].attributes.body
Physical
- Shared memory
- Hardware bus
- Network

Logical
- Direct or indirect
- Synchronous or asynchronous
- Automatic or explicit buffering

# data[].attributes.body
The processes must name each other explicitly
- send (A, message) - send a message to process A
- receive (B, message) - message are being received by process B

Properties of communication link
- Links are established automatically
- A link is associated with exactly one pair of communicating processes
- Between each pair there exists exactly on link
- Link may be unidirectional, but is usually bi-directinal

# data[].attributes.body
Message are being directed to mailbox.

The mailbox will receive all the messages.

Each mailbox has a unique id to indicates to the processes that will send to it.

Processes can communicate if they share the same mailbox.

Properties of communication link

Link establish if processes share a common mailbox

A link may be associated with many processes

Each pair of processes may share several communication links

Link may be unidirectional or bi-directional

# data[].attributes.body
code, data and files belong to the process

where the bottom part of the diagram is belong to the thread

this thread with take the information about code, data and files from the process

# data[].attributes.body
for each thread the with have their own register, stack and PC (program counter).
they will share the information about the code, data and files which belong to the process.

# data[].attributes.body
Both multiprocessors and multicore will support multitasking or multiprogramming in different ways.
Multiprocessor - its having more than 2 CPUs added to the system
Multicore - its having single CPU with multiple execution units

Efficiency
Multicore is more efficient than multiprocessor

# data[].attributes.body
pressure on the programmers, challenges include:

Dividing activities

Balance

Data splitting

Data dependency

Testing and debugging

Parallelism implies a system can perform more than one task simultaneously
Concurrency support more than one task making a progress
- Single processor / core, scheduler providing concurrency

Both Parallelism and Concurrency allow multitasking but
Concurrency for single core
Parallelism for multi core

# data[].attributes.body
There are user threads and kernel threads

Many-to-one
One-to-One
Many-to-Many

The relationships models is refer to user threads and kernel threads.

Many-to-One: Many user threads to One kernel threads

One-to-One: One user threads to One kernel threads

Many-to-Many: Many user threads to Many kernel threads

# data[].attributes.body
The main disadvantage is one thread blocking causes all to block

Multiple threads may not run in parallel on multicore system because only one may be in kernel at a time

# data[].attributes.body
Each user-level thread maps to kernel thread

By creating a user-level thread, it also creates a kernel thread

More concurrency than many-to-one

Number of threads per process sometimes restricted due to overhead(cost for creation the threads)

# data[].attributes.body
Allows many user-level threads to maps to many kernel threads

Allows the OS to create a sufficient number of kernel threads

# data[].attributes.body
Combination of many-to-many model and one-to-one model

# data[].attributes.body
A race condition exists when access to shared data is not controlled, possibly resulting in corrupt data values.

To avoid the corrupt data values requires a mechanism called process synchronization

Process synchronization is the OS task to coordinate among cooperating processes.

Synchronization is done through locking and critical section problem.

# data[].attributes.body
Involves using tools that control access to shared data to avoid race conditions. These tools must be used carefully, as their incorrect use can result in poor system performance, including deadlock.

# data[].attributes.body
Peterson's solution

Mutex

Semaphore

# data[].attributes.body
Computer system has 4 components which is user, application, operating system and harware.
The roles of each components is the same concept as any company that has position roles in the company such as manager, ceo, admin, etc. each position has its own responsibilities toward the company.

Based on my understanding of How CPU can be excuted:
Interrupt is occur when some task/process is needed.
Interrupt is just like our assignment that needs to be done, our deadline to submit is the priority of the request that needs to be execute. When a new assignment is assigned, we will stop the process of finishing the other assignment to process the task of that new assignment. Hehe

From my perspective of multicore:
This multicore is the same concept as human multitasking. Some of us may have an ability to multitask to complete several work.

# data[].attributes.body
Operations

create a new mailbox (port)

send and receive messages through mailbox

destroy a mailbox

Primitives are defined as:

send(A, message) - send a message to mailbox A

receive(A, message) - receive a message from mailbox A

Mailbox sharing

P1, P2, and P3 share the same mailbox

P1 sends, while P2 and P3 receive

Who gets the message?

The solutions for many processes share the same mailbox:
Allow a link to be associated with at most two processes
Allow only one process at a time to execute a receive operation
Allow the system to select arbitrarily the receiver. Sender is notified who the receiver.

# data[].attributes.body
Message passing may blocking or non-blocking

Blocking is considered synchronous:

Blocking send - the sender is blocked until the message is receive

Blocking receive - the receiver is blocked until a message is available

Non-blocking is considered synchronous:

Non-blocking send - the sender sends the message

Non-blocking receive - the receiver receives either a valid or null messages.

Different combinations possible

If both send and receive are blocking, we have a rendezvous.

# data[].attributes.body
It uses queues of messages attached to the link.

There is 3 ways to implement:
1. Zero capacity - no messages are queued on a link.
Sender must wait for receiver.
2. Bounded capacity - finite length of n messages
Sender must wait to send the message if the link is full.
3. Unbounded capacity - infinite length
Sender never waits to send the message

# data[].attributes.body
Thread is the smallest version of process that help process to run and execute it. There is two types of thread which is single thread and multithread.

From my perspective multithread is more efficient than single thread because in multithread each thread have its own register and stack number to execute the process and they also can share the resource that they need.

Multithread is the capabilities to run the process based on the relation which is many to one, one to one or many to many.

The concept that I can relate with multithread is:
It is like working in a group of project and single person project. The ways working in a group so much faster than single person because we can divide the work for each person and complete it faster.

# data[].attributes.body
Selects from among the processes in ready queue, and allocates the CPU core to one of them.

CPU Scheduling may take places when a process:

Switches from running to waiting state

Switches from running to ready state

Switches from waiting to ready

Terminates

Non-preemptive scheduling:

running to waiting state

terminates

Preemptive scheduling:

All other scheduling

- Consider access to shared data
- Consider preemption while in kernel mode
- Consider interrupts occur during OS activities

# data[].attributes.body
Gives control of the CPU the process selected by the short-term scheduler, this involves:

Switching context

Switching to user mode

Jumping to the proper location in the user program to restart the program

Dispatch latency - time to take for the dispatcher to stop one process and start another running state

# data[].attributes.body
CPU utilization – keep the CPU as busy as possible

Throughput – number of processes that complete their execution per time unit

Turnaround time – amount of time to execute a particular process

Waiting time – amount of time a process has been waiting in the ready queue

Response time – amount of time it takes from when a request was submitted until the first response is produced, not output  (for time-sharing environment)

Optimization Criteria

Maximize CPU utilization

Maximize throughput

Minimize the turnaround time

Minimize the waiting time

Minimize the response time

# data[].attributes.body
Each process has a segment code called the critical section problem

Critical section problem is a section for processes to share resources (files, data or database)

A critical section consists of
- Entry section: requesting entry into critical section
- Critical section: a section where only one process can execute at any one time
- Exit section: release the shared resources
- Remainder section: the rest of codes for execution

Each process must request permission to enter its critical section. The section of code implement this request is the entry section

# data[].attributes.body
Applicable to only 2 processes at a time, so it is a 2 process solution only

Two processes share two variables:
- Int turn;
- Boolean flag[2]

The variable turn indicates whose turn to enter the critical section.

The flag array to indicate if a process is ready to enter critical section. flag[i] = true implies that process Pi is ready!

# data[].attributes.headline
Peterson's solution and Critical Section Requirement

# data[].attributes.subject
Peterson's solution and Critical Section Requirement

# data[].attributes.body
Mutual exclusion is preserved
Pi enters Critical section only if:
either flag[i] = false or turn = i
Progress requirement is satisfied
Bounded-waiting requirement is met

# data[].attributes.body
Mutex lock is the simplest tools to solve critical section

Protect a critical section by first acquire() a lock then release() the lock
- Boolean indicates whether if lock is available or not

A process that attempts to acquire an unavailable lock is blocked until the lock is released.

Mutex lock solution requires busy waiting, therefore this lock called a spinlock

# data[].attributes.body
Its provide more sophisticated ways than mutex lock for process to synchronize their activities.

S - integer variable

Can only be accessed via two indivisible (atomic) operations:
- wait()
- signal()

# data[].attributes.body
Its has two types of semaphore values

Counting semaphore - S takes unrestricted domain numbers

Binary semaphore - S takes only 0 and 1

Can solve various synchronization problems.

# data[].attributes.body
Refers to a set of properties that a system must satisfy to ensure that processes make progress during their execution life cycle.

A process waiting indefinitely under the certain circumstances is an example of a "liveness failure"

# data[].attributes.body
Deadlock

A situation where two or more processes are waiting indefinitely for an event that can be caused only by one of the waiting processes.

Priority Inversion

Scheduling problem when lower-priority process holds a lock needed by higher-priority process

Solved via priority-inheritance protocol

# data[].attributes.body
Is a state when every process in a set waiting for an event that can be caused only by another process in that set.

Can be described by the Resource Allocation Graph.

# data[].attributes.body
Mutual exclusion - only 1 process at a time can use a resource

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.

Circular wait - there exists a set of process of waiting processes such that P0 is waiting for a resource that is held by P1, P1 is waiting for a resource that is held by P2, and so on.

# data[].attributes.body
request edge
- directed edge Pi -> Rj
assignment edge
- directed edge Rj ->Pi

# data[].attributes.body
Basic Facts

If graph contains no cycles
= No deadlock

If graph contains a cycles
- If only one instance per resource type, then deadlock
- If several instances per resource type, possibility of deadlock

# data[].attributes.body
OS will try to ignore the problem and pretend that deadlocks never occur in the system.

Deadlock prevention

Deadlock avoidance

Use a protocol to prevent deadlocks and ensuring that the system will never enter a deadlocked state.
If the deadlock still occur in the operating system, then it will allow the system to enter a deadlocked state, detect it, and recover.

# data[].attributes.body
It provides a set methods to ensure that at least one of the necessary conditions cannot hold.
Its try its best to make sure at least one of the 4 characteristics of the deadlock is not happen in the operating system.
These methods prevent deadlocks by constraining how requests for resources can be made.

# data[].attributes.body
Mutual Exclusion

not required for shareable resources. example: 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 is holding resources and then it require another resource and requests another resources that cannot be allocated to it, it needs to release all the resources that being held at the moments.

Process will be restarted again when only it can regain its old resources with the new request resources that it needs.

Circular wait

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

# data[].attributes.body
Requires some future knowledge of requests for resources from each of the participating processes.

It much declare the maximum number of resources of each type that it may needs.

Examines the resource-allocation state to ensure that there can never be a circular-wait condition

# data[].attributes.body
Single instance resource

Use a resource-allocation graph

Multiple instance resource

Use banker's algorithm

# data[].attributes.body
Process P1 request resource R2
Process P1 is holding resource R1
Process P2 request resource R1
Process P2 is holding resource R2

There is a cycle so, the deadlock is occur.

# data[].attributes.body
This algorithm is used to avoid deadlock with multiple instances of a resource type

Each process must have a priori claim maximum use

When a process requests a resource it may have to wait

When a process gets all its resources it must return them in a finite amount of time

# data[].attributes.attachment_caption
It is also called by memory management strategies 

# data[].attributes.body
Every process must be in a memory in order to executed.

The process will be swap in and swap out in the memory.

Typical instruction
- fetch instruction from memory
- decode the instructions
- operand fetch
- possible storage of result in memory

Register access or Cache can be done in one CPU clock (or less)
- It sits between CPU and Memory to deal with the "stall" issue

Protection of memory is required to ensure correct operation:

User process cannot access operating system memory

1 user process cannot access the memory of another user process