Stay Hungry,Stay Foolish!

CPU Scheduling in Operating Systems

CPU Scheduling in Operating Systems

https://www.geeksforgeeks.org/cpu-scheduling-in-operating-systems/?ref=leftbar-rightbar

CPU调度是一个过程,当一个进程被挂起时候,例如等待输入输出,将分配CPU资源给另外一个进程。

 

Scheduling of processes/work is done to finish the work on time. CPU Scheduling is a process that allows one process to use the CPU while another process is delayed (in standby) due to unavailability of any resources such as I / O etc, thus making full use of the CPU. The purpose of CPU Scheduling is to make the system more efficient, faster, and fairer.

Tutorial on CPU Scheduling Algorithms in Operating System

Tutorial on CPU Scheduling Algorithms in Operating System

Whenever the CPU becomes idle, the operating system must select one of the processes in the line ready for launch. The selection process is done by a temporary (CPU) scheduler. The Scheduler selects between memory processes ready to launch and assigns the CPU to one of them.

 

进程是程序的实例,通过单个或者多个线程执行。

What is a process?

In computing, a process is the instance of a computer program that is being executed by one or many threads. It contains the program code and its activity. Depending on the operating system (OS), a process may be made up of multiple threads of execution that execute instructions concurrently.

 

进程启动后被加载到内存中,为了CPU能够高效操作, 因为内存访问速度比文件要快。

进程在内存中包括四个部分:

  • 代码块 -- 程序执行逻辑
  • 静态数据块 -- 代码中全局变量
  • 堆 -- 存储代码运行中动态开辟的内存区块
  • 栈 -- 对应函数中定义的局部变量

How is process memory used for efficient operation?

The process memory is divided into four sections for efficient operation:

  • The text category is composed of integrated program code, which is read from fixed storage when the program is launched.
  • The data class is made up of global and static variables, distributed and executed before the main action.
  • Heap is used for flexible, or dynamic memory allocation and is managed by calls to new, delete, malloc, free, etc.
  • The stack is used for local variables. The space in the stack is reserved for local variables when it is announced.

To know further, you can refer to our detailed article on States of a Process in Operating system.

 

进程调度 -- 进程管理器从CPU中删除活动的进程, 选择另外一个进程绑定到CPU。

在多道程序系统中, 进程调度是必须的。因为系统允许两个以上的进程同时加载到内存,需要保证这些程序都能被执行。

What is Process Scheduling?

Process Scheduling is the process of the process manager handling the removal of an active process from the CPU and selecting another process based on a specific strategy.

Process Scheduling is an integral part of Multi-programming applications. Such operating systems allow more than one process to be loaded into usable memory at a time and the loaded shared CPU process uses repetition time.

There are three types of process schedulers:

  • Long term or Job Scheduler
  • Short term or CPU Scheduler 
  • Medium-term Scheduler

 

调度决定了哪一个程序能够在CPU上工作。

进程调度 -- OS分配CPU时间给每个程序, 充分利用CPU, 减少程序响应时间。

OS配置系统从一个进程到另外一个进程, 这个过程叫“上下文切换”。

Why do we need to schedule processes?

  • Scheduling is important in many different computer environments. One of the most important areas is scheduling which programs will work on the CPU. This task is handled by the Operating System (OS) of the computer and there are many different ways in which we can choose to configure programs.
  • Process Scheduling allows the OS to allocate CPU time for each process. Another important reason to use a process scheduling system is that it keeps the CPU busy at all times. This allows you to get less response time for programs. 
  • Considering that there may be hundreds of programs that need to work, the OS must launch the program, stop it, switch to another program, etc. The way the OS configures the system to run another in the CPU is called “context switching”. If the OS keeps context-switching programs in and out of the provided CPUs, it can give the user a tricky idea that he or she can run any programs he or she wants to run, all at once.
  • So now that we know we can run 1 program at a given CPU, and we know we can change the operating system and remove another one using the context switch, how do we choose which programs we need. run, and with what program?
  • That’s where scheduling comes in! First, you determine the metrics, saying something like “the amount of time until the end”. We will define this metric as “the time interval between which a function enters the system until it is completed”. Second, you decide on a metrics that reduces metrics. We want our tasks to end as soon as possible.

 

进程调度又叫CPU调度, 角度不同叫法不同, 进程被调度到CPU上, CPU被调度给某个进程。

在多道程序系统中, 如果有一个程序在IO阻塞期间仍然占用CPU, 在程序等待的这段时间,CPU显然被浪费掉了。

所以进程调度的目的就是提高资源利用率。

同时也可以防止死锁。

保证公平性。

提高吞吐量。

最小化程序周期。

最小化等待时间。

最小化响应时间。

 

What is the need for CPU scheduling algorithm?

CPU scheduling is the process of deciding which process will own the CPU to use while another process is suspended. The main function of the CPU scheduling is to ensure that whenever the CPU remains idle, the OS has at least selected one of the processes available in the ready-to-use line.

In Multiprogramming, if the long-term scheduler selects multiple I / O binding processes then most of the time, the CPU remains an idle. The function of an effective program is to improve resource utilization.

If most operating systems change their status from performance to waiting then there may always be a chance of failure in the system. So in order to minimize this excess, the OS needs to schedule tasks in order to make full use of the CPU and avoid the possibility of deadlock.

Objectives of Process Scheduling Algorithm:

  • Utilization of CPU at maximum level.  Keep CPU as busy as possible.
  • Allocation of CPU should be fair.
  • Throughput should be Maximum. i.e. Number of processes that complete their execution per time unit should be maximized.
  • Minimum turnaround time, i.e. time taken by a process to finish execution should be the least.
  • There should be a minimum waiting time and the process should not starve in the ready queue.
  • Minimum response time. It means that the time when a process produces the first response should be as less as possible.

 

进程对应的几个CPU时间:

  • 到达时刻
  • 完成时刻
  • 执行时间
  • 周转时间

 

What are the different terminologies to take care of in any CPU Scheduling algorithm?

  • Arrival Time: 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.
  • Turn Around Time: Time Difference between completion time and arrival time.

Turn Around Time = Completion Time  –  Arrival Time

  • Waiting Time(W.T): Time Difference between turn around time and burst time.

Waiting Time = Turn Around Time  –  Burst Time

 

CPU调度算法的几个指标:

  • CPU利用率
  • 吞吐量
  • 周转时间
  • 等待时间
  • 响应时间

Things to take care while designing a CPU Scheduling algorithm?

Different CPU Scheduling algorithms have different structures and the choice of a particular algorithm depends on a variety of factors. Many conditions have been raised to compare CPU scheduling algorithms.

The criteria include the following: 

  • CPU utilization: The main purpose of any CPU algorithm is to keep the CPU as busy as possible. Theoretically, CPU usage can range from 0 to 100 but in a real-time system, it varies from 40 to 90 percent depending on the system load.
  • Throughput: The average CPU performance is the number of processes performed and completed during each unit. This is called throughput. The output may vary depending on the length or duration of the processes.
  • Turn round Time: For a particular process, the important conditions are how long it takes to perform that process. The time elapsed from the time of process delivery to the time of completion is known as the conversion time. Conversion time is the amount of time spent waiting for memory access, waiting in line, using CPU, and waiting for I / O.
  • Waiting Time: The Scheduling algorithm does not affect the time required to complete the process once it has started performing. It only affects the waiting time of the process i.e. the time spent in the waiting process in the ready queue.
  • Response Time: In a collaborative system, turn around time is not the best option. The process may produce something early and continue to computing the new results while the previous results are released to the user. Therefore another method is the time taken in the submission of the application process until the first response is issued. This measure is called response time.

 

调度算法分为两大类:

  • 抢占调度
  • 非抢占调度

What are the different types of CPU Scheduling Algorithms?

There are mainly two types of scheduling methods:

  • Preemptive Scheduling: Preemptive scheduling is used when a process switches from running state to ready state or from the waiting state to the ready state.
  • Non-Preemptive Scheduling: Non-Preemptive scheduling is used when a process terminates , or when a process switches from running state to waiting state.
Different types of CPU Scheduling Algorithms

Different types of CPU Scheduling Algorithms

 

系统对首先对程序分个大类:

  • 系统进程 -- 优先级最高
  • 交互进程 -- 对用户体验有关, 也需要较高优先级
  • 后台进程 -- 一般优先级可设置较低

 

9. Multiple Queue Scheduling:

Processes in the ready queue can be divided into different classes where each class has its own scheduling needs. For example, a common division is a foreground (interactive) process and a background (batch) process. These two classes have different scheduling needs. For this kind of situation Multilevel Queue Scheduling is used. 

The description of the processes in the above diagram is as follows:

  • System Processes: The CPU itself has its process to run, generally termed as System Process.
  • Interactive Processes: An Interactive Process is a type of process in which there should be the same type of interaction.
  • Batch Processes: Batch processing is generally a technique in the Operating system that collects the programs and data together in the form of a batch before the processing starts.

Advantages of multilevel queue scheduling:

  • The main merit of the multilevel queue is that it has a low scheduling overhead.

Disadvantages of multilevel queue scheduling:

  • Starvation problem
  • It is inflexible in nature

To learn about how to implement this CPU scheduling algorithm, please refer to our detailed article on Multilevel Queue Scheduling.

 

 

Preemptive and Non-Preemptive Scheduling

https://www.geeksforgeeks.org/preemptive-and-non-preemptive-scheduling/

抢占式调度 --- 当前正在执行的进程还未执行结束, 但是分配给它的时间片已到, 需要将此进程转入ready状态,进入ready队列等待, 然后根据策略选择另外一个进程执行。

非抢占式调度 --- 没有时间片轮转的概念, 当前进程一直占用CPU, 直到进程执行结束, 或者由于IO进入挂起状态, 才根据策略选择另外一个进程执行。

 

1. Preemptive Scheduling: 
Preemptive scheduling is used when a process switches from running state to ready state or from the waiting state to ready state. The resources (mainly CPU cycles) are allocated to the process for a limited amount of time and then taken away, and the process is again placed back in the ready queue if that process still has CPU burst time remaining. That process stays in the ready queue till it gets its next chance to execute. 

Algorithms based on preemptive scheduling are: Round Robin (RR),Shortest Remaining Time First (SRTF), Priority (preemptive version), etc. 

2. Non-Preemptive Scheduling: 
Non-preemptive Scheduling is used when a process terminates, or a process switches from running to the waiting state. In this scheduling, once the resources (CPU cycles) are allocated to a process, the process holds the CPU till it gets terminated or reaches a waiting state. In the case of non-preemptive scheduling does not interrupt a process running CPU in the middle of the execution. Instead, it waits till the process completes its CPU burst time, and then it can allocate the CPU to another process. 

Algorithms based on non-preemptive scheduling are: Shortest Job First (SJF basically non preemptive) and Priority (non preemptive version), etc. 

 

Thread in Operating System

https://www.geeksforgeeks.org/thread-in-operating-system/?ref=lbp

线程是进程中一次执行路径。

一个进程可以包含多个线程。

线程又叫轻量进程, 由于CPU调度对于进程切换上下文来说, 是重量的; 这里才引入线程, 在进程中做轻量切换。

 

进程提供统一内存空间, 线程共享此空间,

进程之间不同共享内存空间,线程之间复用进程提供的 代码区、数据区、OS资源;

但是线程有用独立的 PC 寄存器 栈空间。

优点:

  • 提高响应性 -- 哪一个线程完成,就可以先报结果。
  • 上下文切换快速 , 与进程切换上下文相比。
  • 有效利用多核 -- 线程是调度的基本单位
  • 资源共享 --- 进程是资源分配的基本单位
  • 通信容易
  • 增强吞吐

What is a Thread?
A thread is a path of execution within a process. A process can contain multiple threads.
Why Multithreading?
A thread is also known as lightweight process. The idea is to achieve parallelism by dividing a process into multiple threads. For example, in a browser, multiple tabs can be different threads. MS Word uses multiple threads: one thread to format the text, another thread to process inputs, etc. More advantages of multithreading are discussed below
Process vs Thread?
The primary difference is that threads within the same process run in a shared memory space, while processes run in separate memory spaces.
Threads are not independent of one another like processes are, and as a result threads share with other threads their code section, data section, and OS resources (like open files and signals). But, like process, a thread has its own program counter (PC), register set, and stack space.
Advantages of Thread over Process
1. Responsiveness: If the process is divided into multiple threads, if one thread completes its execution, then its output can be immediately returned.

2. Faster context switch: Context switch time between threads is lower compared to process context switch. Process context switching requires more overhead from the CPU.

3. Effective utilization of multiprocessor system: If we have multiple threads in a single process, then we can schedule multiple threads on multiple processor. This will make process execution faster.

4. Resource sharing: Resources like code, data, and files can be shared among all threads within a process.
Note: stack and registers can’t be shared among the threads. Each thread has its own stack and registers.

5. Communication: Communication between multiple threads is easier, as the threads shares common address space. while in process we have to follow some specific communication technique for communication between two process.

6. Enhanced throughput of the system: If a process is divided into multiple threads, and each thread function is considered as one job, then the number of jobs completed per unit of time is increased, thus increasing the throughput of the system.
Types of Threads
There are two types of threads.
User Level Thread
Kernel Level Thread
Refer User Thread vs Kernel Thread for more details.

 

How Linux handles threads and process scheduling

https://stackoverflow.com/questions/8463741/how-linux-handles-threads-and-process-scheduling

Linux内核抽象出一个数据结构 task_struct, 每一个实例都对应一个线程, 包括一个进程的 多线程, 或者单线程。

The Linux kernel scheduler is actually scheduling tasks, and these are either threads or (single-threaded) processes.

So a task (a task_struct inside the kernel), in the context of the scheduler, is the thing being scheduled, and can be some kernel thread like kworker or kswapd, some user thread of a multi-threaded process (like firefox), or the single-thread of a single-threaded process (like bash), identified with that single-threaded process.

A process is a non-empty finite set (sometimes a singleton) of threads sharing the same virtual address space (and other things like file descriptors, working directory, etc etc...). See also credentials(7), capabilities(7) etc....

Threads on Linux are kernel threads (in the sense of being managed by the kernel, which also creates its own threads), created by the Linux specific clone syscall (which can also be used to create processes on Linux). The pthread_create function is probably built (on Linux) above clone inside NPTL and Gnu Libc (which integrated NPTL on Linux) and musl-libc.

 

python demo

https://pythongeeks.org/multithreading-in-python/

import threading

def even():#creating second function
    for i in range(0,20,2):
        print(i)
def odd():
    for i in range(1,20,2):
        print(i)

# creating a thread for each function
trd1 = threading.Thread(target=even)
trd2 = threading.Thread(target=odd)

trd1.start() # starting the thread 1 

trd2.start() # starting the thread 2

print('End')

 

output -- 其中可以看到多线程调度的痕迹

0
2
4
6
8
10
12
141
End
3
5
7
9
11
13
15
17
16
18
19

 

posted @ 2022-06-05 23:29  lightsong  阅读(51)  评论(0编辑  收藏  举报
Life Is Short, We Need Ship To Travel