CPT104 - Operating Systems Concepts-小豪的笔记

CPT104 - Operating Systems Concepts

文章目录

1. Operating Systems Concepts

1.1 Short introduction in Operating Systems concept

  • An interface between users and hardware
  • an environment "architecture” Allows convenient usage; hides the tedious stuff
  • Allows efficient usage; parallel activity, avoids wasted cycles
  • Provides information protection
  • Gives each user a slice of the resources
  • Acts as a control program.

1

  • Time Sharing - multiprogramming environment that’s also interactive.
  • Multiprocessing Tightly coupled systems that communicate via shared memory. Used for speed improvement by putting together a number of off-the-shelf processors.
  • Distributed Systems - Loosely coupled systems that communicate via message passing. Advantages include resource sharing, speed up, reliability, communication.
  • Real Time Systems - Rapid response time is main characteristic. Used in control of applications where rapid response to a stimulus is essential.

carry out the most commonly required operations:

  • Process Management
  • Memory management
  • File System Management
  • I/O System Management
  • Protection and Security

1.2 Process Concept

  • Process = a program in execution; process execution must progress in sequential fashion
  • An operating system executes a variety of programs:
    • Time-shared system – user programs or tasks
  • A process is considered an ‘active’ entity A program is considered to be a ‘passive’ entity (stored on disk (executable file)).
  • Program becomes process when executable file loaded into memory

1.2.1 Process in Memory

2

1.2.2 Process State

  • As a process executes, it changes state.
  • The state of a process is defined in part by the current activity of that process.
    • new: The process is being created
    • running: Instructions are being executed
    • waiting: The process is waiting for some event to occur
    • ready: The process is waiting to be assigned to a processor
    • terminated: The process has finished execution

3

1.2.3 Process Control Block (PCB)(进程控制块)

  • Process Control block is a data structure used for storing the information about a process.
  • Each & every process is identified by its own PCB.
  • It is also called as context of the process.
  • PCB of each process resides in the main memory.
  • PCB of all the processes are present in a linked list.
  • PCB is important in multiprogramming environment as it captures the information pertaining to the number of processes running simultaneously.

4

PID = Process identifier Unique number

1.3 Process Scheduling (进程调度)

  • Process scheduler selects from among the processes in memory that are ready to execute, and allocates the CPU to one of them
  • Scheduling queues(维护进程的调度队列) of processes:
    • Job queue – set of all processes in the system
    • Ready queue – set of all processes residing in main memory, ready and waiting to execute
    • Device queues – set of processes waiting for an I/O device

1.3.1 Schedulers

  • Long-Term Scheduler is also called Job Scheduler and is responsible for controlling the Degree of Multiprogramming i.e. the total number of processes that are present in the ready state.
  • Short-Term Scheduler is also known as CPU scheduler and is responsible for selecting one process from the ready state for scheduling it on the running state.
  • Medium-term scheduler is responsible for swapping of a process from the Main Memory to Secondary Memory and vice-versa (mid-term effect on the performance of the system).

5

1.3.2 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

1.4 Operations on Processes (进程操作)

1.4.1 process creation

fork()

Parent process create children processes, which, in turn create other processes, forming a tree of processes

  • 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 concurrently
    • Parent waits until children terminate

1.4.2 process termination

exit()

  • Process executes last statement and then asks the operating system to delete it using the exit() system call.
    • Returns status data from child to parent
    • Process’ resources are deallocated by operating system
  • Parent may wait terminate the execution of children processes.
    • Child has exceeded allocated resources

6

1.5 Inter-process Communication (进程间通信)

INDEPENDENT PROCESSES - neither affect other processes or be affected by other processes.

COOPERATING PROCESSES - can affect or be affected by other processes.

  • Information Sharing - processes which need access to the same file for example.
  • Computation speedup - a problem can be solved faster if the problem can be broken down into sub-tasks to be solved simultaneously
  • Modularity - break a system down into cooperating modules. (e.g. databases with a client-server architecture.)
  • Convenience - even a single user may be multi-tasking, such as editing, compiling, printing, and running the same code in different windows.

1.5.1 Message-Passing Systems

  • communication takes place by way of messages exchanged among the cooperating processes.

  • A message-passing facility provides at least two operations:

    • send(message)
    • receive(message)
  • The message size is either fixed or variable

  • If processes P and Q want to communicate: a communication link must exist between them.

  • Are several methods for logically implementing a link and the send()/receive() operations:

    • Direct or indirect communication
    • Synchronous or asynchronous communication

1.5.2 Shared-Memory Systems

  • a region of memory is shared by cooperating processes.
  • processes can exchange information by reading and writing all the data to the shared region.

Two types of buffers can be used:

  • unbounded-buffer places no practical limit on the size of the buffer
  • bounded-buffer assumes that there is a fixed buffer size

7

1.5.3 Direct or Indirect communication

exchanged messages by communicating processes reside in a temporary queue.

BUFFERING

Zero capacity. The queue has a maximum length of zero; thus, the link cannot have any messages waiting in it.

Bounded capacity. The queue has finite length n; thus, at most n messages can reside in it.

Unbounded capacity. The queue’s length is potentially infinite.

1.5.3.1 Direct Communication
  • Processes must name each other explicitly:
    • send (P, message) – send a message to process P
    • receive(Q, message) – receive a message from process Q
  • Direct Communication is implemented when the processes use specific process identifier for the communication, but it is hard to identify the sender ahead of time.
1.5.3.2 Indirect Communication
  • create a new mailbox (port) send and receive messages through mailbox
    • send(A, message) – send a message to mailbox A
    • receive(A, message) – receive a message from mailbox A
  • destroy a mailbox

1.5.4 Synchronous and Asynchronous Message Passing

  • Message passing may be either blocking or non-blocking ▪
  • Blocking is considered synchronous
    • Blocking send – the sender is blocked until the message is received
    • Blocking receive – the receiver is blocked until a message is available
  • Non-blocking is considered asynchronous
    • Non-blocking send – the sender sends the message and continue
    • Non-blocking receive – the receiver receives:
      • A valid message, or
      • Null message

2.Threads

CPU (central processing unit) is a piece of hardware in a computer that executes binary code.

OS (operating system) is software that schedules when programs can use the CPU.

Process is a program that is being executed.

Thread is part of a process.

2.1 What is a thread?

  • Thread of execution is the smallest sequence of programmed instructions that can be managed independently by a scheduler and executed by the CPU independently of the parent process.
  • A thread is a lightweight process that can be managed independently by a scheduler.
  • Executes a series of instructions in order (only one thing happens at a time).
  • A thread is a flow of control within a process.
  • When there are multiple threads running for a process, the process provides common memory.
  • Multiple tasks with the application can be implemented by separate threads.

O.S. view: a thread is an independent stream of instructions that can be scheduled to run by the OS.

Software developer view: a thread can be considered as a “procedure” that runs independently from the main program.

  • Sequential program: a single stream of instructions in a program.
  • Multi-threaded program: a program with multiple streams (are needed to use multiple cores/CPUs )

2.1.1 Benefits of Threads

  • Responsiveness
    • Allows a program continue running if part of it is blocked or its is performing a lengthy operation
  • Resource Sharing and Economy
    • Threads share an address space, files, code, and data
    • Avoid resource consumption
    • Perform much a faster context switch
  • Utilization of multiprocessor MP Architectures
    • Place code, files and data in the main memory.
    • Distribute threads to each of CPUs, and
    • Let them execute in parallel
  • Deadlock avoidance

2.1.2 Thread

Threads are scheduled on a processor, and each thread can execute a set of instructions independent of other processes and threads.

Thread Control Block TCB stores the information about a thread

1

It shares with other threads belonging to the same process its code section, data section and other operating-system resources, such as open files and signals.

2.1.2 Thread states

Thread Control Block (TCB) contains:

Execution State: CPU registers, program counter, pointer to stack

Scheduling info: State, priority, CPU time

Various Pointers (for implementing scheduling queues)

  • Pointer to enclosing process (PCB)?

Etc.

OS keeps track of TCBs in protected memory (in Array, or Linked List)

2

2.1.3 Single and Multithreaded Processes

  • A traditional process has a single thread.
  • If a process has multiple threads, it can perform more than one task at time.

3

The stack pointer (SP) points to the top of the stack, which stores data

The program counter (PC), indicating the next instruction

4

2.2 Multicore Programming

2.2.1 Concurrency(并发) vs. Parallelism(并行)

Concurrent execution on single-core system

Concurrency means multiple tasks which start, run, and OS rapidly switches back and forth between them -> tasks are interleaved

The concurrency creates the illusion of parallel execution.

5

Parallelism on a multi-core system

A system is parallel if it can perform more than one task simultaneously.

6

2.3 Multithreading Models

CPU is spending time in two very distinct modes:

2.3.1 User mode and Kernel mode

Kernel Mode

executing code has complete and unrestricted access to the underlying hardware.

  • It can execute any CPU instruction and reference any memory address.
  • Kernel mode is generally reserved for the lowest-level, most trusted functions of the operating system.
  • Crashes in kernel mode are catastrophic; they will halt the entire PC.

User Mode

executing code has no ability to directly access hardware or reference memory.

  • Code running in user mode must delegate to system APIs (Application Programming Interface) to access hardware or memory.
  • Crashes in user mode are always recoverable.
  • Most of the code running on your computer will execute in user mode.

The processor switches between the two modes depending on what type of code is running on the processor.

Applications run in user mode, and core operating system components run in kernel mode.

While many drivers run in kernel mode, some drivers may run in user mode.

2.3.2 Types of threads

There are two broad categories of thread implementation:

  • User Level Threads

  • Kernel Level Threads

2.3.2.1 User threads (ULT)

is the unit of execution that is implemented by users and the kernel is not aware of the existence of these threads. Management done by user-level threads library

  • Three primary thread libraries:
    • POSIX Pthreads
    • Windows threads
    • Java threads
    • threading in programming like in C#, Python
2.3.2.2 Kernel threads (KLT)

are handled by the operating system directly and the thread management is done by the kernel.

  • Examples – virtually all general-purpose operating systems, including:
    • Windows
    • Linux
    • Mac OS X
    • iOS
    • Android
2.3.2.3 Difference between ULT & KLT
User Level ThreadsKernel Level Thread
User level threads are faster to create and manage.Kernel level threads are slower to create and manage.
Implementation is by a thread library at the user level.Operating system supports creation of Kernel threads.
User level thread is generic and can run on any operating system.Kernel level thread is specific to the operating system.
Multi-threaded application cannot take advantage of multiprocessing.Kernel routines themselves can be multithreaded.

2.3.3 Multithreading Models

  • Mapping user level threads to kernel level threads
  • In a combined system, multiple threads within the same application can run in parallel on multiple processors.
  • Multithreading models are three types
    • Many – to – One
    • One – to – One
    • Many – to - Many
2.3.3.1 Many-to-One Model

Many user-level threads are mapped to a single kernel thread

The process can only run one user-level thread at a time because there is only one kernel-level thread associated with the process.

Thread management done at user space, by a thread library.

2.3.3.2 One-to-One Model

Each user thread mapped to one kernel thread

Kernel may implement threading and can manage threads, schedule threads.

Kernel is aware of threads.

Provides more concurrency; when a thread blocks, another can run.

2.3.3.3 Many-to-Many Model

Allows many user level threads to be mapped to many kernel threads

Allows the operating system to create a sufficient number of kernel threads

Number of kernel threads may be specific to an either a particular application or a particular machine.

The user can create any number of threads and corresponding kernel level threads can run in parallel on multiprocessor.

2.4 Thread Libraries

Threads can be created, used, and terminated via a set of functions that are part of a Thread API (a thread library)

Thread library provides programmer with API (Application Programming Interface) for creating and managing threads

  • Threads may be implemented in user space or kernel space

Library entirely in user space with no kernel support.

  • all code and data structures for the library exist in user space.
  • invoking a function in the library results in a local function call in user space and not a system call.

Kernel-level library supported directly by the operating system.

  • code and data structures for the library exist in kernel space.
  • invoking a function in the API for the library typically results in a system call to the kernel.

Three primary thread libraries: POSIX threads, Java threads, Win32 threads

2.5 Implicit threading

2.5.1 Managing threads

There are 2 categories: Explicit and Implicit threading. Explicit threading - the programmer creates and manages threads.

Implicit threading - the compilers and run-time libraries create and manage threads.

Three alternative approaches for designing multithreaded programs:

  • Thread pool - create a number of threads at process startup and place them into a pool, where they sit and wait for work.

  • OpenMP is a set of compiler directives available for C, C++, and Fortran programs that instruct the compiler to automatically generate parallel code where appropriate.

  • Grand Central Dispatch (GCD) - is an extension to C and C++ available on Apple’s MacOS X and iOS operating systems to support parallelism.

2.6 Threading issues / Designing multithreaded programs

2.6.1 fork() and exec() System Calls

  • fork() would create a new duplicate process.

Here the issue is whether the new duplicate process created by fork() will duplicate all the threads of the parent process or the duplicate process would be single-threaded.

  • exec() system call when invoked replaces the program along with all its threads with the program that is specified in the parameter to exec()

2.6.2 Signal Handling

What happens if thread never does any I/O, never waits, and never yields control?

Utilize External Events

  • Interrupts: signals from hardware or software that stop the running code and jump to kernel
  • Timer: like an alarm clock that goes off every (some) many milliseconds

How the signal would be delivered to the thread would be decided?

  • asynchronous signal is generated from outside the process that receives it
  • synchronous signal is delivered to the same process that caused the signal to occur

Options:

  • Deliver the signal to the thread to which the signal applies
  • Deliver the signal to every thread in the process
  • Deliver the signal to certain threads in the process
  • Assign a specific thread to receive all signals for the process

2.6.3 Thread Cancellation

Two general approaches:

  • Asynchronous cancellation(异步撤销) terminates the target thread immediately.
    • it is troublesome if a thread to be canceled is in the middle of updating shared data
  • Deferred cancellation(延迟撤销) allows the target thread to periodically check if it should be cancelled

3. Process Synchronization

3.1 What is Process Synchronization (PS)

  • PS is the task of coordinating the execution of processes in a way that no two processes can have access to the same shared data and resources, at one time.
  • n processes all competing to use some shared resource.

3.2 The Critical-Section Problem(临界区问题)

Critical Sections are sequences of code that cannot be interleaved among multiple threads/processes.

Each (concurrent) thread/process has a code segment, called Critical Section (CS), in which the shared data is accessed.

3.2.1 Critical section to prevent a race condition(竞争条件)

  • Multiprogramming allows logical parallelism (multiple programs to exist in memory at the same time) uses devices efficiently but we lose correctness when there is a race condition.
  • Avoid/ forbid / deny execution in parallel inside critical section, even we lose some efficiency, but we gain correctness.

3.2.2 Solutions to CS problem

Concurrent processes come into conflict when they use the same resource (competitively or shared)

There are 3 requirements that must stand for a correct solution:

  • Mutual exclusion
  • Progress
  • Bounded waiting

3.2.3 The Critical-Section Problem

Mutual Exclusion: When a process/thread is executing in its critical section, no other process/threads can be executing in their critical sections.

Progress: If no process/thread is executing in its critical section, and if there are some processes/threads that wish to enter their critical sections, then one of these processes/threads will get into the critical section. No process running outside its critical region may block any process.

Bounded Waiting: No process/thread should have to wait forever to enter into the critical section.

  • the waiting time of a process/thread outside a critical section should be limited (otherwise the process/thread could suffer from starvation).

3.2.3 Types of solutions to CS problem

  • Software solutions
    • algorithms whose correctness relies only on the assumption that only one process/thread at a time can access a memory location/resource
  • Hardware solutions
    • rely on special machine instructions for “locking”
  • Operating System and Programming Language solutions (e.g., Java)
    • provide specific functions and data structures for programmers to use for synchronization.

3.3 Software solutions

3.3.1 Peterson’s Solution

  • is used for mutual exclusion that allows two or more processes to share a single-use resource without conflict, using only shared memory for communication.
  • The central problem is to design the entry and exit sections

Peterson’s algorithm

do {
flag[i] = TRUE;
turn = j;
while ( flag[j] && turn == j);

//CRITICAL SECTION
    
flag[i] = FALSE;
    
//REMAINDER SECTION
    
} 
while (TRUE);

The three CS requirements are met:

Mutual Exclusion is assured as only one process can access the critical section at any time.

each Pi enters its critical section only if either:

flag[j] = false or turn = i

Progress is also assured, as a process outside the critical section does not block other processes from entering the critical section.

Bounded Waiting is preserved as every process gets a fair chance.

3.4 Hardware Solutions

Solution to CS Problem using LOCKS

3.4.1 Single-processor environment

could disable interrupts Effectively stops scheduling other processes.

TEST AND SET SOLUTION

1

  • satisfy the mutual exclusion requirement, but unfortunately do not guarantee bounded waiting.

3.4.2 Multi-processor environment

provides special atomic hardware instructions. Atomic means non-interruptable (i.e., the instruction executes as one unit)

COMPARE AND SWAP SOLUTION

  • a global variable lock is initialized to 0.
  • the only Pi that can enter CS is the one which finds lock = 0
  • this Pi excludes all other Pj by setting lock to 1.

2

3.4.3 Advantages and Disadvantages

Advantages

  • Applicable to any number of processes on either a single processor or multiple processors sharing main memory
  • Simple and easy to verify
  • It can be used to support multiple critical sections; each critical section can be defined by its own variable

Disadvantages

  • Busy-waiting is when a process is waiting for access to a critical section it continues to consume processor time.
  • Starvation is possible when a process executes its critical section, and more than one process is waiting for a long time.
  • Deadlock is the permanent blocking of a set of processes waiting an event (the freeing up of CS) that can only be triggered by another blocked process in the set.

3.5 Operating Systems and Programming Language Solutions

3.5.1 Mutex Lock / Mutual exclusion

  • A mutex is a programming flag used to grab and release an object.
  • When data processing is started that cannot be performed simultaneously elsewhere in the system, the mutex is set to lock which blocks other attempts to use it.
  • The mutex is set to unlock when the data are no longer needed, or the routine is finished.

‒To enforce mutex at the kernel level and prevent the corruption of shared data structures - disable interrupts for the smallest number of instructions is the best way.

‒To enforce mutex in the software areas – use the busy-wait mechanism‒To enforce mutex in the software areas – use the busy-wait mechanism

using mutex is to acquire a lock prior to entering a critical section, and to release it when exiting.

3

Mutex object is locked or unlocked by the process requesting or releasing the resource

4

This type of mutex lock is called a spinlock because the process “spins” while waiting for the lock to become available.

3.5.2 Semaphore

  • Semaphore was proposed by Dijkstra in 1965
  • is a technique to manage concurrent processes by using a simple non-negative integer value and shared between threads / processes.
  • Only three atomic operations may be performed on a semaphore: initialize, decrement, and increment.
    • the decrement operation may result in the blocking of a process
    • the increment operation may result in the unblocking of a process

This variable is used to solve the critical section problem and to achieve process synchronization in the multiprocessing environment.

A semaphore S may be initialized to a non-negative integer value.

- is accessed only through two standard atomic operations: wait() and signal().

  • wait() operation decrements the semaphore value

If the S<0, then the process executing the wait() is blocked. Otherwise, the process continues execution.

wait(S){
    while (S<=0)
        ;//busy wait
    S--;
}
  • signal() operation increments the semaphore value
signal(S){
    S++;
}

There are two main types of semaphores:

  • COUNTING SEMAPHORE – allow an arbitrary resource count. Its value can range over an unrestricted domain. It is used to control access to a resource that has multiple instances.

  • BINARY SEMAPHORE – similar to mutex lock. It can have only two values: 0 and 1.

    Its value is initialized to 1. It is used to implement the solution of critical section problem with multiple processes.

3.5.2.1 COUNTING Semaphores

The semaphore S is initialized to the number of available resources.

Each process that uses a resource, it performs a WAIT() operation on the semaphore (thereby decrementing the number of available resources).

When a process releases a resource, it performs a SIGNAL() operation (incrementing the number of available resources).

When the count for the semaphore goes to 0, all resources are being used. After that, processes that wish to use a resource will be block until the count becomes greater than 0.

3.5.2.2 BINARY Semaphores

A binary semaphore may only take on the values 0 and 1.

  1. A binary semaphore may be initialized to 1.
  2. The WAIT() operation (decrementing) checks the semaphore value.
    • If the value is 0, then the process executing the wait() is blocked
    • If the value is 1, then the value is changed to 0 and the process continues execution.
  3. The SIGNAL() operation (incrementing) checks to see if any processes are blocked on this semaphore (semaphore value equals 0).
    • If so, then a process blocked by a signal() operation is unblocked.
    • If no processes are blocked, then the value of the semaphore is set to 1.
3.5.2.3 Mutex vs. Binary semaphore

A key difference between the a mutex and a binary semaphore is that the process that locks the mutex (sets the value to zero) must be the one to unlock it (sets the value to 1).

In contrast, it is possible for one process to lock a binary semaphore and for another to unlock it.

3.5.2.4 Some issues of semaphore
  • Starvation - when the processes that require a resource are delayed for a long time. Process with high priorities continuously uses the resources preventing low priority process to acquire the resources.
  • Deadlock is a condition where no process proceeds for execution, and each waits for resources that have been acquired by the other processes.

3.6 Classical Problems of Synchronization

3.6.1 The Bounded-Buffer / Producer-Consumer Problem

5

The mutex binary semaphore provides mutual exclusion for accesses to the buffer pool and is initialized to the value 1.

The empty and full semaphores count the number of empty and full buffers.

  • the semaphore empty is initialized to the value n;
  • the semaphore full is initialized to the value 0.

3.6.2 The Readers–Writers Problem

6

A data set is shared among a number of concurrent processes.

  • Only one single writer can access the shared data at the same time, any other writers or readers must be blocked.
  • Allow multiple readers to read at the same time, any writers must be blocked.

Solution: Acquiring a reader–writer lock requires specifying the mode of the lock: either read or write access.

3.6.3 The Dining-Philosophers Problem

How to allocate several resources among several processes.

7

Several solutions are possible:

  • Allow only 4 philosophers to be hungry at a time.
  • Allow pickup only if both chopsticks are available. ( Done in critical section )
  • Odd # philosopher always picks up left chopstick 1 st ,
  • Even # philosopher always picks up right chopstick 1 st .

文章目录

4. CPU Scheduling I

4.1 Basic Concepts

Execution phases of a process

1

4.1.1 CPU–I/O Burst Cycle(执行周期)

  • Process execution consists of a cycle of CPU execution and I/O wait.
  • Process execution begins with a CPU burst, followed by an I/O burst, then another CPU burst … etc
  • The duration of these CPU burst have been measured.
  • An I/O-bound program would typically have many short CPU bursts, A CPU-bound program might have a few very long CPU bursts.
  • this can help to select an appropriate CPU-scheduling algorithm.

4.1.2 Types of Processes

I/O bound

  • Has small bursts of CPU activity and then waits for I/O (eg. Word processor)
  • Affects user interaction (we want these processes to have highest priority)

2

CPU bound

  • Hardly any I/O, mostly CPU activity (eg. gcc, scientific modeling, 3D rendering, etc.)
  • Useful to have long CPU bursts
  • Could do with lower priorities

3

4.1.3 CPU scheduler

The CPU scheduler is the mechanism to select which process has to be executed next and allocates the CPU to that process. Schedulers are responsible for transferring a process from one state to the other.

Basically, we have three types of schedulers i.e.

  • Long-Term Scheduler
  • Short-Term Scheduler
  • Medium-Term Scheduler

4.1.4 Scheduling

Scheduling Levels

4

5

  • The ready list, also known as a ready queue, in the operating system keeps a list of all processes that are ready to run and not blocked on input/output or another blocking system request.
  • The entries in this list are pointers to a Process Control Block, which stores all information and state about a process.

4.1.5 Scheduler & Dispatcher

6

  • Schedulers are special system software that handles process scheduling in various ways.

  • Dispatcher, module of the operating system, removes process from the ready queue and sends it to the CPU to complete.

4.1.6 CPU Scheduling Policies

PREEMPTIVE SCHEDULING = the system may stop the execution of the running process and after that, the context switch may provide the processor to another process.

The interrupted process is put back into the ready queue and will be scheduled sometime in future, according to the scheduling policy.

NON-PREEMPTIVE SCHEDULING= when a process is assigned to the processor, it is allowed to execute to its completion, that is, a system cannot take away the processor from the process until it exits.

Any other process which enters the queue has to wait until the current process finishes its CPU cycle.

Non-preemptive scheduling

  • the CPU is allocated to the process until it terminates.
  • the running process keeps the CPU until it voluntarily gives up the CPU
    • process exits
    • switches to blocked state
    • 1 and 4 only (no 3)

7

Preemptive scheduling

  • the CPU is allocated to the processes for a specific time period
  • the running process can be interrupted and must release the CPU (can be forced to give up CPU)

4.1.7 Dispatcher

Dispatcher module gives control of the CPU to 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 that program

  • Dispatch latency – time it takes for the dispatcher to stop one process and start another running.

Dispatcher is invoked during every process switch; hence it should be as fast as possible

4.2 Scheduling Criteria

Max CPU utilization – keep the CPU as busy as possible

Max Throughput – complete as many processes as possible per unit time

Fairness - give each process a fair share of CPU

Min Waiting time – process should not wait long in the ready queue

Min Response time – CPU should respond immediately

4.3 Scheduling Algorithms

4.3.1 Terms the algorithms deal with…

Arrival Time (AT): Time at which the process arrives in the ready queue.

Completion Time: Time at which process completes its execution.

Burst Time: Time required by a process for CPU execution.

Turnaround Time (TT): the total amount of time spent by the process from coming in the ready state for the first time to its completion.

Turnaround time = Exit time - Arrival time.

Waiting Time (WT): The total time spent by the process/thread in the ready state waiting for CPU.

Waiting Time = Turn Around Time – Burst Time

Response time: Time at which the process gets the CPU for the first time.

4.3.2 First- Come, First-Served (FCFS) Scheduling

  • Processes are executed on first come, first served basis.
  • Poor in performance as average wait time is high.

4.3.3 Shortest Job First (SJF) NO preemption

  • schedule process with the shortest burst time
  • the shortest burst time is scheduled first.

Advantages

– Minimizes average wait time and average response time

Disadvantages

– Not practical : difficult to predict burst time -

​ Learning to predict future

– May starve long jobs

The real difficulty with the Shortest-Job-First SJF algorithm is knowing the length of the next CPU request.

- there is no way to know the exact length of process’s next CPU burst.

Computing an approximation of the length of the next CPU burst

SJF cannot be implemented at the level of the short-term CPU scheduling

4.3.3.1 Determining Length of Next CPU Burst

Estimate it using lengths of past bursts: next = average of all past bursts.

Exponential averaging: next = average of (past estimate + past actual)

8

  • If a=0, then recent history has no effect

  • If a=1, then only the most recent CPU bursts matter

4.3.4 Shortest-Remaining-Time-First SRTF(SJF with preemption)

  • If a new process arrives with a shorter burst time than remaining of current process, then schedule new process
  • Further reduces average waiting time and average response time
  • Context Switch - the context of the process is saved in the Process Control Block PCB when the process is removed from the execution and the next process is scheduled.
  • This PCB is accessed on the next execution of this process.

4.3.5 Priority Scheduling

  • Each process is assigned a priority (an integer number).
  • The CPU is allocated to the process with the highest priority (smallest integer➡highest priority)
  • Priorities may be:
    • Internal priorities based on criteria within OS. Ex: memory needs.
    • External priorities based on criteria outside OS. Ex: assigned by administrators.

!!! PROBLEM➡Starvation – low priority processes may never execute

SOLUTION➡Aging – as time progresses increase the priority of the process Example: do priority = priority + 1 every 15 minutes

4.3.6 Round Robin (RR) Scheduling

  • Each process gets a small unit of CPU time (time quantum or time-slice), usually 10-100 milliseconds.
  • After this time has elapsed, the process is preempted and added to the end of the ready queue Ready queue is treated as a circular queue
  • If there are n processes in the ready queue and the time quantum is q, then each process gets 1/n of the CPU time in chunks of at most q time units at once. No process waits more than (n-1)q time units.

Performance

  • q large - RR scheduling = FCFS scheduling
  • q small - q must be large with respect to context switch, otherwise overhead is too high

4.3.7 Multilevel Queue Scheduling

  • Ready queue is partitioned into separate queues; e.g., two queues containing o
    • foreground (interactive) processes . May have externally defined priority over
    • background processes o background (batch) processes

Process permanently associated to a given queue; no move to a different queue

There are two types of scheduling in multi-level queue scheduling:

  • Scheduling among the queues.
  • Scheduling between the processes of the selected queue.

Must schedule among the queues too (not just processes):

  • Fixed priority scheduling; (i.e., serve all from foreground then from background). Possibility of starvation.
  • Time slice – each queue gets a certain amount of CPU time which it can schedule amongst its processes;

80% to foreground in RR, and 20% to background in FCFS

4.3.8 Multilevel Queue Scheduling

The various categories of processes can be:

  • Interactive processes
  • Non-interactive processes
  • CPU-bound processes
  • I/O-bound processes
  • Foreground processes
  • Background processes

Multilevel feedback queues - automatically place processes into priority levels based on their CPU burst behavior.

I/O-intensive processes will end up on higher priority queues and CPU-intensive processes will end up on low priority queues.

A process can move between the various queues

A multilevel feedback queue uses two basic rules:

  1. A new process gets placed in the highest priority queue.
  2. If a process does not finish its quantum, then it will stay at the same priority level otherwise it moves to the next lower priority level

A process can move between the various queues;

​ aging can be implemented this way.

  • Multilevel-feedback-queue scheduler defined by the following parameters:
    • number of queues
    • scheduling algorithms for each queue
    • method used to determine when to upgrade a process
    • method used to determine when to demote a process
    • method used to determine which queue a process will enter when that process needs service

Three queues:

Q0 – RR with time quantum 8 milliseconds

  • Highest priority. Preempts Q1 and Q2 proc’s

Q1 – RR time quantum 16 milliseconds

  • Medium priority. Preempts processes in Q2

Q2 – FCFS

  • Lowest priority

9

Scheduling:

  1. A new job enters queue Q0 which is served first-come first served

    • When it gains CPU, job receives 8 milliseconds

    • If it does not finish in 8 milliseconds, job is moved to queue Q1

  2. At Q1 job is again served RR and receives 16 additional milliseconds

    • If it still does not complete, it is preempted and moved to queue Q2

5. CPU Scheduling II

5.1 Thread Scheduling

5.1.1 Contention scope

User threads are mapped to kernel threads**.**

The thread models:

  • Many to One model
  • One to One model
  • Many to Many model.

The contention scope refers to the competition the User level threads to access the kernel resources.

There are two possible contention scopes:

  • Process Contention Scope PCS, a.k.a local contention scope.
  • System Contention Scope SCS, a.k.a global contention scope.

5.1.2 Thread Scheduling

The basic levels to schedule threads:

Process Contention Scope (unbound threads) - competition for the CPU takes place among threads belonging to the same process. The thread library schedules the PCS thread to access the resources via available LWPs (priority as specified by the application developer during thread creation).

  • It used many-to-many and many-to-one models. o

System Contention Scope (bound threads) - competition for the CPU takes place among all threads in the system. This scheme is used by the kernel to decide which kernel-level thread to schedule onto a CPU.

  • It used only a one-to-one model.

5.2 Multiple-Processor Scheduling

5.2.1 Approaches to Multiple-Processor Scheduling

  • CPU scheduling more complex when multiple CPUs are available
  • We are focused on multiprocessor systems in which the processors are identical Homogeneous processors
  • Asymmetric multiprocessing – only one processor accesses the system data structures, alleviating the need for data sharing
  • Symmetric multiprocessing (SMP) – each processor is selfscheduling, all processes in common ready queue, or each has its own private queue of ready processes

5.2.2 Asymmetric multiprocessing

Master – Slave Configuration

One processor as master and other processors in the system as slaves. The master processor runs the OS and processes while slave processors run the processes only.

The process scheduling is performed by the master processor.

The parallel processing is possible as a task can be broken down into subtasks and assigned to various processors.

1

5.2.3 Symmetric Configuration SMP

Any processor can access any device and can handle any interrupts generated on it.

Mutual exclusion must be enforced such that only one processor is allowed to execute the OS at one time.

To prevent the concurrency of the processes many parts of the OS are independent of each other such as scheduler, file system call, etc.

2

5.2.4 Approaches to Symmetric Configuration

3

5.2.5 Processor Affinity

Processor affinity is the ability to direct a specific task, or process, to use a specified core.

  • The idea behind: if the process is directed to always use the same core it is possible that the process will run more efficiently because of the cache re-use.
    • Note: If a process migrates from one CPU to another, the old instruction and address caches become invalid, and it will take time for caches on the new CPU to become ‘populated’.
  • Soft affinity – OSs try to keep a process running on the same processor but not guaranteeing it will do so.
  • Hard affinity - allows a process to specify a subset of processors on which it may run.

5.2.6 Load Balancing

Load Balancing → a method of distributing work between the processors fairly in order to get optimal response time, resource utilization, and throughput.

  • Push migration = A system process periodically (e.g., every 200 ms) checks ready queues and moves (or push) processes to different queues (if need be).
  • Pull migration = If scheduler finds there is no process in ready queue so it raids another processor’s run queue and transfers a process onto its own queue so it will have something to run (pulls a waiting task from a busy processor).

4

5.2.7 Multicore Processors

  • A core executes one thread at a time. Hyper Threading allows multiple threads to run on each core of CPU
  • Single-core processor spends time waiting for the data to become available (slowing or stopping of a process ) = Memory stall.

5

Solution!!! Multicore processor: to put multiple processor cores onto a single chip to run multiple kernel threads concurrently.

6

5.2.8 Multithreading

How do we execute multiple threads on same core?

Techniques for multithreading:

  • Coarse-grained multithreading - switching between threads only when one thread blocks (long latency event such as a memory stall occurs).
  • Fine-grained multithreading - instructions “scheduling” among threads obeys a Round Robin policy.

5.3 Real-Time CPU Scheduling

5.3.1 Characteristics of a RTOS

A real-time operating system RTOS are deadline driven.

Examples: the patient monitoring in a hospital intensive-care unit, the autopilot in an aircraft, radar systems, robot control in an automated factory, etc.

Hard RTOS – is one that must meet its deadline; otherwise, it will cause unacceptable damage or a fatal error to the system.

Soft RTOS – an associated deadline that is desirable but not necessary; it still makes sense to schedule and complete the task even if it has passed its deadline.

The timing constraints are in the form of period and deadline.

The period is the amount of time between iterations of a regularly repeated task.

The deadline is a constraint of the maximum time limit within which the operation must be complete.

  • Aperiodic tasks (random time) has irregular arrival times and either soft or hard deadlines.

  • Periodic tasks (repeated tasks), the requirement may be stated as “once per period T” or “exactly T units apart.”

5.3.2 Issues in Real-time Scheduling

The major challenges for an RTOS is to schedule the real-time tasks.

Two types of latencies may delay the processing (performance):

  1. Interrupt latency – aka interrupt response time is the time elapsed between the last instruction executed on the current interrupted task and start of the interrupt handler.

    An interrupt is a signal emitted by a device attached to a computer or from a program within the computer. It requires the operating system to stop and figure out what to do next.

  2. Dispatch latency – time it takes for the dispatcher to stop one process and start another running. To keep dispatch latency low is to provide preemptive kernels.

5.3.3 Real-Time CPU Scheduling

The RTOS schedules all tasks according to the deadline information and ensures that all deadlines are met.

Static scheduling. A schedule is prepared before execution of the application begins.

Priority-based scheduling. The priority assigned to the tasks depends on how quickly a task has to respond to the event.

Dynamic scheduling. There is complete knowledge of tasks set, but new arrivals are not known. Therefore, the schedule changes over the time.

5.3.4 Characteristics of processes

Processes are considered periodic (repeated tasks).

A periodic process has:

  • processing time t,
  • deadline d by which it must be serviced by the CPU, and
  • period p.

0 ≤ t ≤ d ≤ p

Rate of a periodic task is 1/p

A process may have to announce its deadline requirements to the scheduler.

7

5.3.4.1 Rate-Monotonic Scheduling

It is a static priority-based preemptive scheduling algorithm.

The task with the shortest period will always preempt the executing task.

The shortest period = the highest priority;

8

5.3.4.2 Earliest-Deadline-First Scheduling

The scheduling criterion is based on the deadline of the processes. The processes / tasks need not be periodic.

Dynamically assigns priorities according to deadline.

  • the earlier the deadline = the higher the priority;
5.3.4.3 Proportional Share Scheduling

Scheduling that pre-allocates certain amount of CPU time to each of the processes.

Fair-share scheduler

  • Guarantee that each process obtain a certain percentage of CPU time
  • Not optimized for turnaround or response time

T shares are allocated among all processes in the system

An application receives N shares where N < T

This ensures each application will receive N / T of the total processor time

5.4 Algorithm Evaluation

5.4.1 Deterministic evaluation

Takes a particular predetermined workload and defines the performance of each algorithm for that workload.

5.4.2 Queueing Models

  • If we define a queue for the CPU and a queue for each I/O device, we can test the various scheduling algorithms using queueing theory.
  • Little ’ s formula – processes leaving queue must equal processes arriving, thus、

n = λ x W

n = average queue length

W = average waiting time in queue

λ = average arrival rate into queue

5.4.3 Simulations

We can use trace tapes

This is data collected from real processes on real machines and is fed into the simulation.

6. Resource Management Deadlocks

6.1 Deadlocks

6.1.1 Deadlock Characterization

Each process utilizes a resource as follows:

  • request
  • use
  • release

Deadlock can be defined as the permanent blocking of a set of processes that compete for system resources.

Deadlock can arise if four conditions hold simultaneously.

  • MUTUAL EXCLUSION: only one 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: a closed chain of processes exists, such that each process holds at least one resource needed by the next process in the chain .

6.1.2 Resource-Allocation Graph

A set of vertices V and a set of edges E

  • V is partitioned into two types:

    • P = {P1 , P2 , …, Pn }, the set consisting of all the processes in the system
    • R = {R1 , R2 , …, Rm}, the set consisting of all resource types in the
  • request edge – directed edge Pi → Rj

  • assignment edge – directed edge Rj → Pi

1
2

6.1.3 Basic Facts

  • If graph contains no cycles -> no deadlock
  • If graph contains a cycle ->
    • if only one instance per resource type, then deadlock
    • if several instances per resource type, possibility of deadlock

6.2 HANDLING DEADLOCKS

6.2.1 Methods for Handling Deadlocks

  • Ensure that the system will never enter a deadlock state.
  • To deal with the deadlock, the following three approaches can be used:
    • Deadlock prevention
    • Deadlock avoidance
    • Deadlock detection and recovery
  • Ignore the problem and pretend that deadlocks never occur in the system (used by most operating systems, including UNIX

6.2.2 Deadlock Prevention

adopting a policy that eliminates one of the conditions (conditions 1 through 4)

Mutual Exclusion – In general, the first of the four conditions cannot be disallowed. If access to a resource requires mutual exclusion, then mutual exclusion must be supported by the OS.

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

  • Require process to request and be allocated all its resources before it begins execution or allow process to request resources only when the process has none allocated to it.
  • Low resource utilization; starvation possible

No Preemption – can be prevented in several ways.

  • if a process holding certain resources is denied a further request, that process must release its original resources and, if necessary, request them again together with the additional resource.
  • if a process requests a resource that is currently held by another process, the OS may preempt the second process and require it to release its resources.

Circular Wait – can be prevented by defining a linear ordering of resource types.

  • if a process has been allocated resources of type R, then it may subsequently request only those resources of types following R in the ordering.

6.2.3 Deadlock Avoidance

A safe state is one in which there is at least one sequence of resource allocations to processes that does not result in a deadlock (i.e., all of the processes can be run to completion).

An unsafe state is, of course, a state that is not safe

  • If a system is in safe state -> no deadlocks
  • If a system is in unsafe state -> possibility of deadlock
  • Avoidance -> ensure that a system will never enter an unsafe state

Two approaches to deadlock avoidance:

  • Do not start a process if its demands might lead to deadlock.
  • Do not grant an incremental resource request to a process if this allocation might lead to deadlock.
6.2.3.1 Deadlock Avoidance in Single Instance of Resources
  • Where every resource type has a single instance of resource, the RAG can be used
  • Claim edge Pi → Rj indicated that process Pi may request resource Rj ; represented by a dashed line
  • After the cycle check, if it is confirmed that there will be no circular wait, the claim edge is converted to a request edge.
  • Otherwise, it will be rejected.
  • Request edge converted to an assignment edge when the resource is allocated to the process
  • When a resource is released by a process, assignment edge reconverts to a claim edge

3

Banker’s Algorithm

The banker’s algorithm has two parts:

  • Safety Test algorithm that checks the current state of the system for its safe state.
  • Resource request algorithm that verifies whether the requested resources, when allocated to the process, affect the safe state. If it does, the request is denied.

Data Structures for the Banker’s Algorithm

4

6.2.4 Deadlock Detection

6.2.5 Recovery from Deadlock

There are two options for breaking a deadlock.

  • Process Termination / Abort Process
  • Resource Preemption
6.2.5.1 Process Termination
  • Abort all deadlocked processes
  • Abort one process at a time until the deadlock cycle is eliminated
6.2.5.2 Resource Preemption
  • Select a victim – a process, whose execution has just started and requires many resources to complete, will be the right victim for preemption (minimize cost).
  • Rollback – return the process to some safe state (safe checkpoint), restart it from that state
  • Starvation – it may be possible that the same process is always chosen for resource preemption, resulting in a starvation situation. Thus, it is important to ensure that the process will not starve. This can be done by fixing the number of times a process can be chosen as a victim.
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
根据提供的引用内容,我无法确定你具体想要了解什么关于"Cpt VSCode"。但是,我可以为你提供一些关于VSCode的基本介绍和演示。 Visual Studio Code(简称VSCode)是一款由微软开发的免费开源的代码编辑器。它支持多种编程语言,并提供了丰富的功能和插件生态系统,使开发者能够更高效地编写代码。 以下是VSCode的一些常用功能和操作演示: 1. 安装VSCode:前往VSCode官方网站(https://code.visualstudio.com/),根据你的操作系统下载并安装VSCode。 2. 打开文件或文件夹:在VSCode中,你可以通过点击菜单栏的"文件"选项,然后选择"打开文件"或"打开文件夹"来打开你的代码文件或整个项目文件夹。 3. 编辑代码:在VSCode中,你可以直接在编辑器中编写代码。VSCode提供了代码高亮、自动补全、代码片段等功能,以提高编码效率。 4. 调试代码:VSCode内置了强大的调试功能,你可以通过设置断点、观察变量值等方式来调试你的代码。 5. 使用插件:VSCode拥有丰富的插件生态系统,你可以根据自己的需求安装各种插件来扩展VSCode的功能。你可以通过点击菜单栏的"扩展"选项来搜索、安装和管理插件。 6. 版本控制:VSCode集成了Git版本控制工具,你可以在VSCode中进行代码的版本控制操作,如提交代码、查看提交历史等。 希望以上介绍和演示对你有所帮助。如果你有任何进一步的问题,请随时提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小豪GO!

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值