OSTEP Chapter 4. The Abstraction: The Process

4. The Abstraction: The Process

process: it is a running program.

HOW TO PROVIDE THE ILLUSION OF MANY CPUS?

by virtualizing the CPU. By running one process, then stopping it and running another, and so forth.

mechanisms: low-level methods or protocols that implement a needed piece of functionality. For example, context switch. time-sharing mechanism.

Policies are algorithms for making some kind of decision within the OS.

scheduling policy decidion which programs to run on a CPU.

4.1 The Abstraction: A Process

machine state: what a program can read or update when it is running.

machine state component: memory, registers(program counter, PC; instruction pointer, IP; stack pointer; frame pointer)

4.2 Process API

  • Create: create new processes.
  • Destroy: destroy processes forcefully.
  • Wait: wait for a process to stop running.
  • Miscellaneous Control: e.g. suspend a process and then resume it.
  • Status: get some status information about a process.

4.3 Process Creation: A Little More Detail

how programs are transformed into processes?

  • load its code and any static data (e.g., initialized variables) into memory, into the address space of the process. Programs initially reside on disk in some kind of executable format;
  • Some memory must be allocated for the program’s run-time stack (or just stack).The OS may also allocate some memory for the program’s heap.
  • The OS will also do some other initialization tasks, particularly as re- lated to input/output (I/O).
  • to start the program running at the entry point, namely main().

4.4 Process States

In a simplified view, a process can be in one of three states

  • Running
  • Ready
  • Blocked
          scheduled
  Running    ---->     Ready
     |       <----       ^
     |    descheduled    |
I/O: initiate         I/O: done
     |                   |
      ----> Blocked -----

4.5 Data Structures

The OS is a program, it has some key data struc- tures that track various relevant pieces of information. the OS likely will keep some kind of process list for all processes.

// the registers xv6 will save and restore
// to stop and subsequently restart a process
struct context {
  int eip;
  int esp;
  int ebx;
  int ecx;
  int edx;
  int esi;
  int edi;
  int ebp;
};
// the different states a process can be in
enum proc_state { UNUSED, EMBRYO, SLEEPING,
                  RUNNABLE, RUNNING, ZOMBIE };
// the information xv6 tracks about each process
// including its register context and state
struct proc {
    char *mem;
    uint sz;
    char *kstack;
    enum proc_state state;
    int pid;
    struct proc *parent;
    void *chan;
    // Start of process memory
    // Size of process memory
    // Bottom of kernel stack
    // for this process
    // Process state
    // Process ID
    // Parent process
    // If non-zero, sleeping on chan
    // If non-zero, have been killed
    int killed;
    struct file *ofile[NOFILE]; // Open files
    struct inode *cwd;
    struct context context;
    struct trapframe *tf;
    // Current directory
    // Switch here to run process
    // Trap frame for the
    // current interrupt
};

Homework

下载作业地址

https://zhanghuimeng.github.io/post/ostep-ch-04-homework-simulation-process-run-py/

进程有四种不同的状态: RUNNING、READY、WAITING、DONE

每个进程由多个指令组成。每个指令只会做两件事,使用 CPU 或进行 IO

通过命令 -l PROCESS_LIST 指定进程列表,列表格式是 X1:Y1,X2:Y2 ,X 表示指令的数,Y 表示指令是使用 CPU 的概率(0-100) 如果是100则全部是使用CPU 如果是0则全部是 IO 操作。 -L IO_LENGTH 指定 I/0 需要的时间。

  1. ./process-run.py -l 5:100,5:100 有两个进程 各需要五个时间片的 CPU 操作
Produce a trace of what would happen when you run these processes:
Process 0
  cpu
  cpu
  cpu
  cpu
  cpu

Process 1
  cpu
  cpu
  cpu
  cpu
  cpu

结果,很明显需要 5+5=10 个时间片。CPU 占用率是 100%。

Time     PID: 0     PID: 1        CPU        IOs
  1     RUN:cpu      READY          1
  2     RUN:cpu      READY          1
  3     RUN:cpu      READY          1
  4     RUN:cpu      READY          1
  5     RUN:cpu      READY          1
  6        DONE    RUN:cpu          1
  7        DONE    RUN:cpu          1
  8        DONE    RUN:cpu          1
  9        DONE    RUN:cpu          1
 10        DONE    RUN:cpu          1

Stats: Total Time 10
Stats: CPU Busy 10 (100.00%)
Stats: IO Busy  0 (0.00%)
  1. ./process-run.py -l 4:100,1:0 两个进程 一个需要 4 个时间片的 CPU 一个需要 一个 IO 请求并等待结束。I/O 操作默认需要 5 个时间片。(README里没看到。代码里看到的…)
Produce a trace of what would happen when you run these processes:
Process 0
  cpu
  cpu
  cpu
  cpu

Process 1
  io

结果

Time     PID: 0     PID: 1        CPU        IOs
  1     RUN:cpu      READY          1
  2     RUN:cpu      READY          1
  3     RUN:cpu      READY          1
  4     RUN:cpu      READY          1
  5        DONE     RUN:io          1
  6        DONE    WAITING                     1
  7        DONE    WAITING                     1
  8        DONE    WAITING                     1
  9        DONE    WAITING                     1
 10*       DONE       DONE

Stats: Total Time 10
Stats: CPU Busy 5 (50.00%)
Stats: IO Busy  4 (40.00%)

P0 先执行四个时间片,P1 再执行一个 IO 操作,然后等待 5 个时间片,并在第 5 个时间片结束。
所以 P0 的 CPU 使用率 5/10, IO 使用率 5/10

  1. ./process-run.py -l 1:0,4:100 将第二题的两个进程倒过来。看到下面 Important behaviors 有说,当一个进程结束或者发起I/O操作后,会进行进程的切换。
Produce a trace of what would happen when you run these processes:
Process 0
  io

Process 1
  cpu
  cpu
  cpu
  cpu

Important behaviors:
  System will switch when the current process is FINISHED or ISSUES AN IO
  After IOs, the process issuing the IO will run LATER (when it is its turn)

结果 ./process-run.py -l 1:0,4:100 -c -p

Time     PID: 0     PID: 1        CPU        IOs
  1      RUN:io      READY          1
  2     WAITING    RUN:cpu          1          1
  3     WAITING    RUN:cpu          1          1
  4     WAITING    RUN:cpu          1          1
  5     WAITING    RUN:cpu          1          1
  6*       DONE       DONE

Stats: Total Time 6
Stats: CPU Busy 5 (83.33%)
Stats: IO Busy  4 (66.67%)

因为在等待 IO 的时候 另一个进程在占用 CPU 进行计算,所以提高了 CPU 和 IO 的使用率

  1. ./process-run.py -l 1:0,4:100 -S SWITCH_ON_END -S 有两个选项,SWITCH_ON_IO, SWITCH_ON_END 代表进程切换的时机。SWITCH_ON_END 表示一个进程结束才切换。默认是 SWITCH_ON_IO
Process 0
  io

Process 1
  cpu
  cpu
  cpu
  cpu

结果 ./process-run.py -l 1:0,4:100 -c -p -S SWITCH_ON_END

Time     PID: 0     PID: 1        CPU        IOs
  1      RUN:io      READY          1
  2     WAITING      READY                     1
  3     WAITING      READY                     1
  4     WAITING      READY                     1
  5     WAITING      READY                     1
  6*       DONE    RUN:cpu          1
  7        DONE    RUN:cpu          1
  8        DONE    RUN:cpu          1
  9        DONE    RUN:cpu          1

Stats: Total Time 9
Stats: CPU Busy 5 (55.56%)
Stats: IO Busy  4 (44.44%)

此时在 P0 发起 IO 操作之后 不会切换进程而是会一直等到 P0 结束。所以耗时会更加。注意 IO 结束的最后一个时间片是不占用 CPU 和 IO 的。

  1. ./process-run.py -l 1:0,4:100 -c -p -S SWITCH_ON_IO 此时其实和题 3 相同 因为 SWITCH_ON_IO 是默认选项。
  2. ./process-run.py -l 3:0,5:100,5:100,5:100 -S SWITCH_ON_IO -I IO_RUN_LATER -I 有两个选项,IO_RUN_LATER 表 IO 结束之后,等待正在运行的进程结束之后,再进行发起 IO请求的进程。为默认值。IO_RUN_IMMEDIATE 表示 IO 结束之后,立刻切换到发起 IO 的进程。
Process 0
  io
  io
  io

Process 1
  cpu
  cpu
  cpu
  cpu
  cpu

Process 2
  cpu
  cpu
  cpu
  cpu
  cpu

Process 3
  cpu
  cpu
  cpu
  cpu
  cpu

结果 ./process-run.py -l 3:0,5:100,5:100,5:100 -c -p -S SWITCH_ON_IO -I IO_RUN_LATER

Time     PID: 0     PID: 1     PID: 2     PID: 3        CPU        IOs
  1      RUN:io      READY      READY      READY          1
  2     WAITING    RUN:cpu      READY      READY          1          1
  3     WAITING    RUN:cpu      READY      READY          1          1
  4     WAITING    RUN:cpu      READY      READY          1          1
  5     WAITING    RUN:cpu      READY      READY          1          1
  6*      READY    RUN:cpu      READY      READY          1
  7       READY       DONE    RUN:cpu      READY          1
  8       READY       DONE    RUN:cpu      READY          1
  9       READY       DONE    RUN:cpu      READY          1
 10       READY       DONE    RUN:cpu      READY          1
 11       READY       DONE    RUN:cpu      READY          1
 12       READY       DONE       DONE    RUN:cpu          1
 13       READY       DONE       DONE    RUN:cpu          1
 14       READY       DONE       DONE    RUN:cpu          1
 15       READY       DONE       DONE    RUN:cpu          1
 16       READY       DONE       DONE    RUN:cpu          1
 17      RUN:io       DONE       DONE       DONE          1
 18     WAITING       DONE       DONE       DONE                     1
 19     WAITING       DONE       DONE       DONE                     1
 20     WAITING       DONE       DONE       DONE                     1
 21     WAITING       DONE       DONE       DONE                     1
 22*     RUN:io       DONE       DONE       DONE          1
 23     WAITING       DONE       DONE       DONE                     1
 24     WAITING       DONE       DONE       DONE                     1
 25     WAITING       DONE       DONE       DONE                     1
 26     WAITING       DONE       DONE       DONE                     1
 27*       DONE       DONE       DONE       DONE

Stats: Total Time 27
Stats: CPU Busy 18 (66.67%)
Stats: IO Busy  12 (44.44%)

每次当前进程结束,会回到 IO 发起的进程,也就是 P0。最后当 P1 P2 P3 都结束的时候,还在等待 IO 。所以猜想每次 IO 结束都立刻返回发起 IO 的进程,是不是可以提高 CPU 使用率。

  1. 情况和 6 一样。查看结果 ./process-run.py -l 3:0,5:100,5:100,5:100 -c -p -S SWITCH_ON_IO -I IO_RUN_IMMEDIATE 可以发现上面猜想的正确性。
Time     PID: 0     PID: 1     PID: 2     PID: 3        CPU        IOs
  1      RUN:io      READY      READY      READY          1
  2     WAITING    RUN:cpu      READY      READY          1          1
  3     WAITING    RUN:cpu      READY      READY          1          1
  4     WAITING    RUN:cpu      READY      READY          1          1
  5     WAITING    RUN:cpu      READY      READY          1          1
  6*     RUN:io      READY      READY      READY          1
  7     WAITING    RUN:cpu      READY      READY          1          1
  8     WAITING       DONE    RUN:cpu      READY          1          1
  9     WAITING       DONE    RUN:cpu      READY          1          1
 10     WAITING       DONE    RUN:cpu      READY          1          1
 11*     RUN:io       DONE      READY      READY          1
 12     WAITING       DONE    RUN:cpu      READY          1          1
 13     WAITING       DONE    RUN:cpu      READY          1          1
 14     WAITING       DONE       DONE    RUN:cpu          1          1
 15     WAITING       DONE       DONE    RUN:cpu          1          1
 16*       DONE       DONE       DONE    RUN:cpu          1
 17        DONE       DONE       DONE    RUN:cpu          1
 18        DONE       DONE       DONE    RUN:cpu          1

Stats: Total Time 18
Stats: CPU Busy 18 (100.00%)
Stats: IO Busy  12 (66.67%)

原文地址:https://www.cnblogs.com/wenruo/p/11196436.html

时间: 2024-11-08 02:00:58

OSTEP Chapter 4. The Abstraction: The Process的相关文章

The NoSQL System

Unlike most of the other projects in this book, NoSQL is not a tool, but an ecosystem composed of several complimentary and competing tools. The tools branded with the NoSQL monicker provide an alternative to SQL-based relational database systems for

NoSQL生态系统(nosql ecosystem)

Unlike most of the other projects in this book, NoSQL is not a tool, but an ecosystem composed of several complimentary and competing tools. The tools branded with the NoSQL monicker provide an alternative to SQL-based relational database systems for

(转)一些经典的计算机书籍

以下列表中的计算机书籍(中文版)来自微博:@程序员的那些事 粉丝的推荐.按推荐次数,从高到低往下排.如果大家还有其他计算机相关的经典书籍推荐,请在评论中留言,或者在这条微博的评论中留言,我们将继续扩充这个列表.1. 算法导论(第2版)2. 代码大全(第2版)3. C++ Primer中文版(第4版)4. 设计模式:可复用面向对象软件的基础5. 浪潮之巅6. Java编程思想(第4版)7. Java核心技术 卷1:基础知识8. Java核心技术 卷2:高级特性9. 人月神话10. Linux内核编

Oracle创建Physical Standby Database案例

The following is the detail steps of how to create a physical standby database: This case is created, operated and followed on the steps from oracle online help documentation. The configuration of the two sites server as following: Primary Database:

Spring 4 官方文档学习 Spring与Java EE技术的集成

本部分覆盖了一下内容: Chapter 28, Remoting and web services using Spring -- 使用Spring进行远程和web服务 Chapter 29, Enterprise JavaBeans (EJB) integration -- EJB集成 Chapter 30, JMS (Java Message Service) -- JMS (Java 消息服务) Chapter 31, JMX Chapter 32, JCA CCI Chapter 33,

进程控制(Note for apue and csapp)

1. Introduction We now turn to the process control provided by the UNIX System. This includes the creation of new processes, program execution, and process termination. We also look at the various IDs that are the property of the process - real, effe

tomcat(17)启动tomcat

[0]README 1)本文部分文字描述转自"how tomcat works",旨在学习"tomcat(17)启动tomcat"的相关知识: 2)本文重点关注启动Tomcat时会用到的两个类,分别是Catalina类和 Bootstrap类:(干货--引入tomcat启动时用到了两个类,分别是Catalina类和 Bootstrap类) 2.1)org.apache.catalina.startup.Catalina类:用于启动和关闭Server对象,并负责解析T

[转载] 架构领域学习资料

原文: http://qing.blog.sina.com.cn/2244218960/85c41050330031zq.html 系统架构领域的一些学习材料 标签:架构学习材料系统systemresearch 系统架构是一个工程和研究相结合的领域,既注重实践又依赖理论指导,入门容易但精通很难,有时候还要讲点悟性,很具有“伪科学”的特征.要在此领域进阶,除了要不断设计并搭建实际系统,也要注意方法论和设计理念的学习和提炼. 经常有同学询问如何学习,特贴一篇学习材料,供大家参考.09年时写的,在系统

系统架构领域的一些学习材料

标签:架构学习材料系统systemresearch 转载:http://qing.blog.sina.com.cn/2244218960/85c41050330031zq.html?utm_source=tuicool&sudaref=www.tuicool.com 系统架构是一个工程和研究相结合的领域,既注重实践又依赖理论指导,入门容易但精通很难,有时候还要讲点悟性,很具有"伪科学"的特征.要在此领域进阶,除了要不断设计并搭建实际系统,也要注意方法论和设计理念的学习和提炼.