操作系统——Processes 进程

本文介绍了操作系统的概念,强调其作为应用程序与硬件之间的媒介作用。操作系统的主要操作包括进程管理、内存管理、存储管理和保护与安全。重点讲解了进程的概念,包括进程状态、进程控制块(PCB)和进程调度,涉及调度器、上下文切换。还讨论了进程间的通信(IPC),如共享内存和消息传递系统。
摘要由CSDN通过智能技术生成

目录​​​​​​​

1. 导入——什么是操作系统?

Operating System (OS) Operations

2. The notion of a process

Process State

Process Control Block

3. Process Scheduling

Schedulers

 Context switch

4. Operations on Processes

5. Inter-Process Communication (IPC)

Shared-Memory Systems

Message-Passing Systems


1. 导入——什么是操作系统?

首先,我们可以将计算机系统大致分为四个部分(如下图Fig 1所示):

1) the hardware: CPU(central processing unit), the memory and the input/output (I/O) devices

2) the application programs: spreadsheets, compilers and Web browsers (应用程序就是对硬件资源进行合理分配来解决用户的计算问题)

3) the operating system: 针对不同的用户,控制和协调硬件在不同应用程序中的使用

4) the users

操作系统(operating system)像是空气(准确来说是一种媒介),它本身并不具备功能,就像空气只是给人类提供一个环境使得人类能够生存,操作系统也只是提供一个环境使得应用程序能够运行。

Fig 1

Operating System (OS) Operations

现代操作系统都是中断驱动的(interrupt driven),什么是中断驱动?中断驱动就是把CPU从等待中解脱出来,让它去忙其他事情,而不是一直在这里等待。对于每一种类型的中断,操作系统内都会有相对应的代码段来决定该采取什么行动。这里会专门给一套中断服务程序来解决中断。

因为用户和操作系统共享这台计算机的硬件和软件,因此,我们需要确保在一个程序中的错误只能对该程序的运行造成影响。因为是共享的,一个程序的错误可能会对很多进程造成不利影响。综上所述,我们设计的操作系统一定要确保一个程序中的异常不会对其他程序产生影响。

回归正题,介绍一下OS执行的最常用的操作:

1) Process Management: (本篇文章主要介绍的,后面会持续更新~)

  • Scheduling processes and threads on the CPUs 
  • Creating and deleting both user and system processes
  • Suspending and resuming processes
  • Providing mechanisms for process synchronization
  • Providing mechanisms for process communication

2) Memory Management

3) Storage Management

  • File-System Management
  • Mass-Storage Management
  • Caching
  • I/O Systems

4) Protetion and Security


2. The notion of a process

要想了解process,就要先了解program。

  • program: passive entity, such as a file containing a list of instructions stored on disk (often called an executable fifile).
  • process: active entity, with a program counter specifying the next instruction to execute and a set of associated resources.

Thus, A program becomes a process when an executable file is loaded into memory. In a nutshell, process is a program in execution. 

An operating system executes a variety of programs:

  1. Batch System executes jobs
  2. Time-shared System has use programs or tasks

Batch System: Computerized batch processing is a method of running software programs called jobs in batches automatically. While users are required to submit the jobs, no other interaction by the user is required to process the batch. Batches may automatically be run at scheduled times as well as being run contingent on the availability of computer resources.      将作业按照它们的性质分组(或分批),然后再成组(或成批)地提交给计算机系统,由计算机自动完成后再输出结果,从而减少作业建立和结束过程中的时间浪费。批处理可以在预定时间自动运行,也可以根据计算机资源的可用性来运行。

Time-shared System: In computing, time-sharing is the sharing of a computing resource among many users at the same time by means of multi-programming and multi-tasking.          在早期的计算机系统中,计算机处理多个用户发送出的指令的时候,处理的方案即为分时,即计算机把它的运行时间分为多个时间段,并且将这些时间段平均分配给用户们指定的任务。轮流地为每一个任务运行一定的时间,如此循环,直至完成所有任务。

The structure of a process in memory  (如下图Fig 2所示)

  • Stack: Stack contains temporary data, such as function parameters, return addresses, and local variables.
  • Heap: Heap is memory that is dynamically allocated during process run time.
  • Data: A data section contains global variables.
  • Text: A text section includes the current activity, as represented by the value of the program counter and the contents of the processor’s registers.
Fig 2

Note: A process itself can be an execution environment for other code. (进程本身是可以为代码提供运行环境的) 例如:Java编程环境。在大多数情况下,一个可执行的Java程序是在JVM中执行的。JVM的执行作为一个进程,它解释了java代码转成本机机器指令并代表java代码做出行动。java 命令运行JVM作为一个进程,在虚拟机里依次执行java程序。

Process State

As a process executes, it changes state. The state of a process is defifined 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 (such as an I/O completion or reception of a signal).

Ready

The process is waiting to be assigned to a processor.

Terminated

The process has fifinished execution.

It is important to realize that only one process can be running on any processor at any instant. Many processes may be ready and waiting. (任何时刻,在任何处理器上只能运行一个进程)

Fig 3  Diagram of process state
</
  • 5
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
好的,进程调度算法是操作系统中非常重要的一部分。常见的进程调度算法有先来先服务(FCFS)、短作业优先(SJF)、高响应比优先(HRRN)、时间片轮转(RR)等。下面是这些算法的 Python 实现: 1. 先来先服务(FCFS): ```python def FCFS(processes): waiting_time = 0 turn_around_time = 0 completion_time = 0 for process in processes: completion_time += process['burst_time'] turn_around_time += completion_time - process['arrival_time'] waiting_time += turn_around_time - process['burst_time'] return waiting_time / len(processes), turn_around_time / len(processes) ``` 2. 短作业优先(SJF): ```python def SJF(processes): processes = sorted(processes, key=lambda x: x['burst_time']) waiting_time = 0 turn_around_time = 0 completion_time = 0 for process in processes: completion_time += process['burst_time'] turn_around_time += completion_time - process['arrival_time'] waiting_time += turn_around_time - process['burst_time'] return waiting_time / len(processes), turn_around_time / len(processes) ``` 3. 高响应比优先(HRRN): ```python def HRRN(processes): waiting_time = 0 turn_around_time = 0 completion_time = 0 for i, process in enumerate(processes): if i == 0: completion_time = process['burst_time'] else: response_ratio_list = [] for j in range(i): response_ratio = (completion_time - processes[j]['arrival_time'] + processes[j]['burst_time']) / processes[j]['burst_time'] response_ratio_list.append(response_ratio) max_response_ratio_index = response_ratio_list.index(max(response_ratio_list)) selected_process = processes.pop(max_response_ratio_index) completion_time += selected_process['burst_time'] turn_around_time += completion_time - selected_process['arrival_time'] waiting_time += turn_around_time - selected_process['burst_time'] return waiting_time / len(processes), turn_around_time / len(processes) ``` 4. 时间片轮转(RR): ```python def RR(processes, time_slice): waiting_time = 0 turn_around_time = 0 completion_time = 0 while processes: for i in range(len(processes)): if processes[i]['burst_time'] > time_slice: completion_time += time_slice processes[i]['burst_time'] -= time_slice else: completion_time += processes[i]['burst_time'] turn_around_time += completion_time - processes[i]['arrival_time'] waiting_time += turn_around_time - processes[i]['burst_time'] processes.pop(i) break return waiting_time / len(processes), turn_around_time / len(processes) ``` 这里的 `processes` 是一个列表,其中每个元素是一个字典,表示一个进程的信息,如下所示: ```python processes = [ {'name': 'P1', 'arrival_time': 0, 'burst_time': 8}, {'name': 'P2', 'arrival_time': 1, 'burst_time': 4}, {'name': 'P3', 'arrival_time': 2, 'burst_time': 9}, ... ] ``` 在这个列表中,每个进程有一个名称、到达时间和执行时间。你可以根据自己的需要修改这些信息,来测试这些进程调度算法的实现。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

高纯度Coder

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

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

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

打赏作者

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

抵扣说明:

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

余额充值