Operating System-Process(1)

本文阐述操作系统的核心概念之一:进程(Process),主要内容:

  1. 什么是进程
  2. 进程的创建(Creation)
  3. 进程的终止(Termination)
  4. 进程的状态(State)

一、什么是进程

1.1 基本信息

进程是执行程序的一个实例,是对正在运行的程序的抽象(Abstraction),包含当前运行程序的所有程序计数器(PC),寄存器以及变量:程序运行所需要的指令和数据。

如果说程序是一个菜谱,那么进程就是做菜的过程。

我当前系统运行的进程实例(windows 10)

如果一个程序同时运行多次,操作系统会创建多个进程(互不影响),对于操作系统来说就是有多个进程恰好在运行相同的程序而已。上图的Google Chrome就是一个例子(多个实例,互不影响,但是消耗内存资源。。)

1.2 Why Process

为什么操作系统要引入进程的概念?上面提到了隔离,但是隔离不是进程引入的首要驱动力,引入进程的首要驱动力是:增加系统的响应时间。

CPU同一个时间只能做一件事情,只能有一个进程运行在CPU上,但是为什么我们感觉电脑在同时作者很多事情呢?我们一遍在Word中打字,一遍右下角收到邮件通知。

  1. Word是一个进程(当然,打开多个则是多个进程)
  2. 邮件是一个进程

操作系采用自己的调度策略让CPU在Word和Email之间切换,由于切换的非常快,所以用户感觉CPU同时在作者很多事情,从而达到了增加系统响应的效果。

用户感觉

a、b、c进程在同时执行

实际执行

a、b、c同一时间只有一个在执行

1.3 进程调度

用一个例子说明:有一个爸爸准备给儿子做一个蛋糕,他有一份做蛋糕的菜谱,他正在按照食谱做着蛋糕,突然他儿子进来对他说:“爸爸,我要喝牛奶”。这位爸爸停下手中正在做的蛋糕,去给儿子倒牛奶,然后在微波炉加热,最后把牛奶给了儿子。最终这位爸爸又回到原来做蛋糕的地方,继续完成蛋糕。

  1. 蛋糕菜谱: 是程序,先做什么,后做什么
  2. 做蛋糕的过程:是进程A
  3. 给儿子热牛奶的过程:是进程B
  4. 这位爸爸:CPU
  5. 儿子突然要喝牛奶:中断

CPU根据现有的情况(策略)在进程A、B之间切换。上面爸爸倒完牛奶后继续做蛋糕。这里的继续其实就是CPU在进程之间切换之前先保存当前进程的状态,当再次切换到这个进程时,会从之前保存的状态处进行运行(由此也能看出进程切换的开销)。

二、进程的创建(Creation)

2.1 进程创建时机

  1. 在系统启动时创建进程(boot)。这个时候创建的都是系统运行必须的进程,有些用于和UI进行交互,有些属于后台进程,比如Email接受进程
  2. 由现有的进程通过调用系统的Create Call进行创建。比如在一个web系统中,可以另外创建一个excel进程完成数据导入到excel的功能
  3. 当用户发起一个请求时进行创建,比如当用户点击鼠标,在command中输入时会创建相应的进程对用户的请求进行响应

2.2 创建过程

以上的所说的三种创建进程的时机最终创建进程的方式是一致的:由一个已有的进程跳过一个系统调用完成进程的创建。

  1. Unix:Fork
  2. Windows:CreateProcess

该系统调用会告知操作系统去创建一个进程。

发起系统调用的进程为父进程,被创建的进程为子进程,父子进程之间是相互独立的,他们的地址空间完全不同。

2.3 进程的层级关系(Hierarchy)

上面做了阐述,新的进程是已有进程通过像OS发送创建进程的call而完成的。被创建的进程暂且叫child process。这个child process接下来可能也会再次给os发请求创建子进程。。。。。以此类推,将会有一个进程继承链。

上面这种成为一个链的进程在unix中会作为一个进程组而存在。但是在Windows中,这些进程其实是完全独立的。继承关系在进程中不是很重要。

三、进程的终止(termination)

一个进程创建后最终都会终止的,进程会在以下几种条件下被终止,相对比较好理解:

  1. 正常终止:当进程完成自己的事情之后告诉操作系统(System Call)终止进程
  2. 出现错误的时候终止。当进程发现错误时,比如资源不存在或者所运行的程序有bug。比如被0除。
  3. 被其他进程Kill。比如在windows下可以通过任务管理器直接Kill系统的进程

在有些操作系统中当一个进程终止时,有其创建的进程也会被Kill 掉。

四、进程的状态(State)

4.1 状态

进程被创建后有三个状态:

  1. Running(运行中)
  2. Blocked(被阻塞)
  3. Ready(就绪)

Blocked是指进程在等待外部的刺激(与CPU无关),比如Console程序,一直在等待用户输入。

Ready是指进程可以运行,没有等待任何资源,但是CPU被其他进程占用,没有CPU资源

4.2 状态切换

如图:

Running-》Blocked:进程在运行过程中需要其他外部资源。

Blocked-》Ready:进程需要的资源得到了满足

Ready-》Running:由CPU的Scheduler决定,CPU可以给当前进程使用

Running-》Ready:由CPU的Scheduler决定,CPU分配给其他进程使用(比如上面的例子,爸爸停下正在做的蛋糕去给儿子热牛奶,这时候蛋糕处于ready状态)

时间: 2024-07-28 16:26:39

Operating System-Process(1)的相关文章

[Operating System] {Udacity} P2L1: Processes and Process Management

V0-Vmax => max size of the process address space heap and data may not be contiguous stack: last in first out any access of the process to x will access the correct physical location where x is stored. not all processes requires the entire address sp

Operating System: Three Easy Pieces --- Process (Note)

1. How can the operating system provide the illusion of a nearly-endless supply of said CPUs? The OS creates this illusion by virtualizing the CPU. The basic technique, known as the time- sharing the CPU, allows users to run as many concurrent proces

Linux Operating System

Linux is a Unix-like computer operating system assembled under the model of free and open source software developmentand distribution .The defining component of Linux is the Linux kernel , andoperating system kernel was first released October 5 , 199

General-Purpose Operating System Protection Profile

1 Protection Profile Introduction ? This document defines the security functionality expected to be provided by a general-purpose operating system capable of operating in a networked environment. It also provides a set of assurance components that de

Operating System Engineering

Operating System Engineering Course Meeting Times Lectures: 2 sessions / week, 1.5 hours / session Prerequisites 6.033 Computer System Engineering 6.170 Software Studio 6.004 Computation Structures Description 6.828 Operating System Engineering studi

POJ #2448 A New Operating System

Time Limit: 20000MS   Memory Limit: 65536K Total Submissions: 1165   Accepted: 110 Case Time Limit: 5000MS Description May is a lovely girl. Due to her filial piety, she wants to give a present on her mother's birthday. Because both her parents are t

Operating system management of address-translation-related data structures and hardware lookasides

An approach is provided in a hypervised computer system where a page table request is at an operating system running in the hypervised computer system. The operating system determines whether the page table request requires the hypervisor to process.

Operating System: Three Easy Pieces --- Mechanism: Limited Direct Execution (Note)

In order to virtualize the CPU, the operating system needs to somehow share the physical CPU among many jobs  running seemingly at the same time. The basic idea is simple: run one process for a little while, then run another, and so forth. By time sh

osquery An Operating System Instrumentation Framewor

catalog 1. Getting Started 2. install guide for OS X and Linux 3. Features Overview 4. Logging 5. query example 1. Getting Started osquery is an operating system instrumentation framework for OS X and Linux. The tools make low-level operating system

InnoDB: Operating system error number 87 in a file operation. 错误87的解决方法

InnoDB: Operating system error number 87 in a file operation. 错误87的解决方法 140628  8:10:48 [Note] Plugin 'FEDERATED' is disabled.140628  8:10:48 InnoDB: The InnoDB memory heap is disabled140628  8:10:48 InnoDB: Mutexes and rw_locks use Windows interlock