操作系统——进程与线程,以及先来先服务FCFS、优先级调度Priority Scheduling、短作业优先SJF、时间片流转RR简单题目介绍

预备知识

当我们在电脑(包括台式主机、手机、pad等)打开一个程序时,这时会创建一个进程此处就是等下要说明的进程状态中的新建
以微信为例,当我们打开微信之后***此处就创建了一个微信进程***,能够与多个用户进行交流,同时能够看公众号之类的,其实这些都是在微信这个进程里创建新的线程。
这时可以打开任务管理器进行查看。可以看到一个个进程。
在这里插入图片描述
下拉微信,可以看到微信下的线程。
在这里插入图片描述
这是就能引出一个进程和线程的性质

线程的独立性,某进程中的线程对其他进程不可见

顾名思义就是:进程内的线程可以相互通信,但是进程A和进程B的线程无法通信。

下面来介绍进程的状态转化

在这里插入图片描述

  • 新建-》就绪:
    直接创建之后就进入就绪态,新建态还在被创建,创建工作尚未完成,比如说内存不足。
  • 就绪-》运行:获得处理器资源
  • 运行-》阻塞:请求某一外设或者等待某事件的发生
  • 阻塞-》就绪:请求外设结束或者等待的时间发生了

进程调度算法

1、先来先服务(FCFS)

先来的先进行服务直到完成(这个不具备抢占特性)。

2、优先级调度(Priority Scheduling)

根据优先级进行调度(一般来说是有抢占性的,特殊情况会进行说明)。

3、短作业有限(SJF)

从当前作业中选取一个时间最短的作业进行调度,直到完成或者阻塞。

4、时间片轮转调度算法(RR)

进程一个一个享用处理器资源,谁也不亏待。

其他:高相应比调度算法,多级队列调度算法

调度算法应用到题目中

在这里插入图片描述
在这里插入图片描述

  • 11
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
服务调度算法(FCFS): ```python def fcfs(processes): """ First-Come, First-Served (FCFS) scheduling algorithm Input: processes: a list of dictionaries containing process information Each dictionary should have the following keys: - 'pid': process ID - 'arrival_time': arrival time of the process - 'burst_time': burst time of the process Output: a list of tuples containing the order in which the processes are executed Each tuple should have the following format: (process ID, start time, end time) """ # Sort the processes by arrival time sorted_processes = sorted(processes, key=lambda x: x['arrival_time']) # Initialize the start and end times of the first process start_time = sorted_processes[0]['arrival_time'] end_time = start_time + sorted_processes[0]['burst_time'] # Initialize the order list with the first process order = [(sorted_processes[0]['pid'], start_time, end_time)] # Loop through the remaining processes and update the start and end times for i in range(1, len(sorted_processes)): if sorted_processes[i]['arrival_time'] > end_time: # If there is a gap between processes, update the start time start_time = sorted_processes[i]['arrival_time'] else: # If the next process arrives before the current process finishes, # update the end time start_time = end_time end_time = start_time + sorted_processes[i]['burst_time'] # Add the process to the order list order.append((sorted_processes[i]['pid'], start_time, end_time)) return order ``` 进程优先调度算法(SJF): ```python def sjf(processes): """ Shortest-Job-First (SJF) scheduling algorithm Input: processes: a list of dictionaries containing process information Each dictionary should have the following keys: - 'pid': process ID - 'arrival_time': arrival time of the process - 'burst_time': burst time of the process Output: a list of tuples containing the order in which the processes are executed Each tuple should have the following format: (process ID, start time, end time) """ # Sort the processes by arrival time and burst time sorted_processes = sorted(processes, key=lambda x: (x['arrival_time'], x['burst_time'])) # Initialize the start and end times of the first process start_time = sorted_processes[0]['arrival_time'] end_time = start_time + sorted_processes[0]['burst_time'] # Initialize the order list with the first process order = [(sorted_processes[0]['pid'], start_time, end_time)] # Loop through the remaining processes and update the start and end times for i in range(1, len(sorted_processes)): if sorted_processes[i]['arrival_time'] > end_time: # If there is a gap between processes, update the start time start_time = sorted_processes[i]['arrival_time'] else: # If the next process arrives before the current process finishes, # update the end time start_time = end_time end_time = start_time + sorted_processes[i]['burst_time'] # Add the process to the order list order.append((sorted_processes[i]['pid'], start_time, end_time)) return order ``` 时间片轮转调度算法(RR): ```python def rr(processes, quantum): """ Round-Robin (RR) scheduling algorithm Input: processes: a list of dictionaries containing process information Each dictionary should have the following keys: - 'pid': process ID - 'arrival_time': arrival time of the process - 'burst_time': burst time of the process quantum: the time quantum for the algorithm Output: a list of tuples containing the order in which the processes are executed Each tuple should have the following format: (process ID, start time, end time) """ # Sort the processes by arrival time sorted_processes = sorted(processes, key=lambda x: x['arrival_time']) # Initialize the start and end times of the first process start_time = sorted_processes[0]['arrival_time'] end_time = min(start_time + sorted_processes[0]['burst_time'], sorted_processes[1]['arrival_time']) if len(sorted_processes) > 1 else start_time + sorted_processes[0]['burst_time'] # Initialize the order list with the first process order = [(sorted_processes[0]['pid'], start_time, end_time)] # Initialize the queue with the remaining processes queue = sorted_processes[1:] # Loop through the queue until all processes are executed while queue: # Get the next process in the queue current_process = queue.pop(0) # If the process has not arrived yet, skip it if current_process['arrival_time'] > end_time: queue.append(current_process) continue # Calculate the remaining burst time for the current process remaining_time = current_process['burst_time'] start_time = end_time # Loop through the time slices until the process finishes while remaining_time > 0: # If the time slice is smaller than the remaining time, update the end time if remaining_time > quantum: end_time = start_time + quantum remaining_time -= quantum else: end_time = start_time + remaining_time remaining_time = 0 # Add the process to the order list order.append((current_process['pid'], start_time, end_time)) # Update the start time for the next time slice start_time = end_time # Check if there are any new processes that have arrived for process in queue: if process['arrival_time'] <= end_time: queue.remove(process) queue.append(current_process) current_process = process remaining_time = current_process['burst_time'] break return order ``` 优先级调度算法(抢占式): ```python def priority_preemptive(processes): """ Priority scheduling algorithm (preemptive) Input: processes: a list of dictionaries containing process information Each dictionary should have the following keys: - 'pid': process ID - 'arrival_time': arrival time of the process - 'burst_time': burst time of the process - 'priority': priority of the process (higher number = higher priority) Output: a list of tuples containing the order in which the processes are executed Each tuple should have the following format: (process ID, start time, end time) """ # Sort the processes by arrival time and priority sorted_processes = sorted(processes, key=lambda x: (x['arrival_time'], -x['priority'])) # Initialize the start and end times of the first process start_time = sorted_processes[0]['arrival_time'] end_time = start_time + sorted_processes[0]['burst_time'] # Initialize the order list with the first process order = [(sorted_processes[0]['pid'], start_time, end_time)] # Initialize the queue with the remaining processes queue = sorted_processes[1:] # Loop through the queue until all processes are executed while queue: # Get the highest-priority process in the queue current_process = max(queue, key=lambda x: x['priority']) # If the process has not arrived yet, skip it if current_process['arrival_time'] > end_time: queue.remove(current_process) continue # Calculate the remaining burst time for the current process remaining_time = current_process['burst_time'] start_time = end_time # Loop through the remaining processes to check if there is a higher-priority process for process in queue: if process['arrival_time'] <= end_time and process['priority'] > current_process['priority']: # If there is a higher-priority process, preempt the current process queue.remove(process) queue.append(current_process) current_process = process remaining_time = current_process['burst_time'] break # Loop through the time slices until the process finishes while remaining_time > 0: # If the next process has a higher priority, preempt the current process if queue and max(queue, key=lambda x: x['priority'])['priority'] > current_process['priority']: break # If the time slice is smaller than the remaining time, update the end time if remaining_time > 1: end_time += 1 remaining_time -= 1 else: end_time += 1 remaining_time = 0 # Add the process to the order list order.append((current_process['pid'], start_time, end_time)) return order ``` 高响应比优先调度算法: ```python def hrrn(processes): """ Highest-Response-Ratio-Next (HRRN) scheduling algorithm Input: processes: a list of dictionaries containing process information Each dictionary should have the following keys: - 'pid': process ID - 'arrival_time': arrival time of the process - 'burst_time': burst time of the process Output: a list of tuples containing the order in which the processes are executed Each tuple should have the following format: (process ID, start time, end time) """ # Sort the processes by arrival time sorted_processes = sorted(processes, key=lambda x: x['arrival_time']) # Initialize the start and end times of the first process start_time = sorted_processes[0]['arrival_time'] end_time = start_time + sorted_processes[0]['burst_time'] # Initialize the order list with the first process order = [(sorted_processes[0]['pid'], start_time, end_time)] # Initialize the queue with the remaining processes queue = sorted_processes[1:] # Loop through the queue until all processes are executed while queue: # Calculate the response ratio for each process in the queue response_ratios = [] for process in queue: wait_time = max(0, end_time - process['arrival_time']) response_ratio = (wait_time + process['burst_time']) / process['burst_time'] response_ratios.append(response_ratio) # Get the process with the highest response ratio index = response_ratios.index(max(response_ratios)) current_process = queue.pop(index) # Calculate the remaining burst time for the current process remaining_time = current_process['burst_time'] start_time = end_time # Loop through the time slices until the process finishes while remaining_time > 0: # If the next process has a higher response ratio, preempt the current process response_ratios = [] for process in queue: wait_time = max(0, end_time - process['arrival_time']) response_ratio = (wait_time + process['burst_time']) / process['burst_time'] response_ratios.append(response_ratio) if queue and max(response_ratios) > ((end_time - current_process['arrival_time']) / current_process['burst_time']): break # If the time slice is smaller than the remaining time, update the end time if remaining_time > 1: end_time += 1 remaining_time -= 1 else: end_time += 1 remaining_time = 0 # Add the process to the order list order.append((current_process['pid'], start_time, end_time)) return order ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值