Chapter 3. Process Management

  • Unit 3.1 Processes
  • Unit 3.2 Managing Processes
  • Unit 3.3 Scheduling
  • Unit 3.4 Race Conditions
  • Unit 3.5 IPC Problems
  • Unit 3.6 IPC Programming
  • Unit 3.7 Threads
  • Unit 3.8 Deadlocks

Unit 3.1
Processes

Presenter Notes

本节幻灯片


Unit objectives

After completing this unit, you should be able to:

  • Discuss what is process.
  • Discuss the difference between the program and the process.
  • Discuss the process hierarchies.
  • Discuss the process lifecycle.
  • Discuss the process states.
  • Use the system calls to control the process.

Program vs process

What's program?

  • Program is just the static text .
  • Program can be loaded into memory and executed, called process.
  • A program can be executed many times at once.

What's process?

  • A process is an instance of a computer program that is being executed.
  • It contains the program code and its current activity.

Presenter Notes

  • program program==code+data; 一个进程可以对应多个程序,一个程序也可以变成多个进程。程序可以作为一种软件资源长期保存,以文件的形式存放在硬盘
  • process: 相应的程序+CPU上下文+一组系统资源 动态性, 独立性, 并发性 一个CPU,任何时刻最多只能有一个进程去使用它

The Process Model

  • (a) Multiprogramming of four programs.
  • (b) Conceptual model of four independent, sequential processes.
  • (c) Only one program is active at once.

Presenter Notes

在进程模型中,计算机上所有可运行的软件,通常也包括操作系统,被组织成若干顺序进程(sequential process),简称进程(process)。操作系统中最核心的概念是进程, 进程也是并发程序设计中的一个最重要、 最基本的概念。进程是一个动态的过程, 即进程有生命周期, 它拥有资源, 是程序的执行过程, 其状态是变化的。一个进程就是一个正在执行程序的实例,包括程序计数器、寄存器和变量的当前值。从概念上说,每个进程拥有它自己的虚拟CPU。当然,实际上真正的CPU在各进程之间来回切换。但为了理解这种系统,考虑在(伪)并行情况下运行的进程集,要比我们试图跟踪CPU如何在程序间来回切换简单得多


Multiprogramming

  • The modern operating systems have the ability to execute many processes concurrently.

Concurrently?

  • In case of one CPU with one core, processes can only be executed sequentially. Only one process is active at once.
  • On a macroscale the processes can be considered to executed concurrently.
  • At the microscopic level, they are executed sequentially.

Presenter Notes

用多道程序设计可以提高CPU的利用率。严格地说,如果进程用于计算的平均时间是进程在内存中停留时间的20%,且内存中同时有5个进程,则CPU将一直满负载运行。然而,这个模型在现实中过于乐观,因为它假设这5个进程不会同时等待I/O。

更好的模型是从概率的角度来看CPU的利用率。假设一个进程等待I/O操作的时间与其停留在内存中时间的比为p。当内存中同时有n个进程时,则所有n个进程都在等待I/O(此时CPU空转)的概率是pn


A Process consists of three parts

  • Process Control Block ( Process Table Entry)

  • Program (Text)
  • Data

Presenter Notes

进程由以下三部分组成:①进程控制块PCB;②数据段;③正文段。

  • 进程控制块 PCB (Process Control Block): 存放进程的管理和控制信息的数据结构称为进程控制块。它是进程管理和控制的最重要的数据结构,每一个进程均有一个PCB,在创建进程时,建立PCB,伴随进程运行的全过程,直到进程撤消而撤消。
  • 正文段
  • 数据段

Process Hierarchies

- In UNIX, all processes are started by other processes.

- This is called a parent/child relationship.

- The first process, called "init", is the exception. It's created by the kernel when system boot.

- Processes forms a hierarchy, called "process tree".

- Windows has no concept of process hierarchy.

Presenter Notes

在某些系统,当进程创建了另外一个进程后,父进程和子进程就以某种形式保持关联,子进程如果继续创建新的进程,那么就组成了一个层次结构。 在Unix中,进程和它的所有子女以及后裔组成一个进程组,进程组中的进程,可以统一接受一个信号。 在 windows中,没有进程树的概念。


Creating a process

!C
pid_t val;
val=fork();
if (val > 0) {          //parent's code here;
    int stat_val;
    pid_t child_pid;
    child_pid = waitpid(val,&stat_val,0);
    printf("Child has finished: PID = %d\n", child_pid);
    if(WIFEXITED(stat_val))
        printf("Child exited with code %d\n"
                    , WEXITSTATUS(stat_val));
    else
        printf("Child terminated abnormally\n");
    exit(exit_code);
} else if (val == 0) {  //child's code here;
    execlp("ls","ls","-l",NULL);
} else if (val <  0) {  // fork() error
    fprintf(stderr,"fork failed!\n");
    exit(1);
}

Presenter Notes

(1) 系统初始化会创建新的进程 (2) 当一个正在运行的进程中,若执行了创建进程的系统调用,那么也会创建新的进程 (3) 用户发出请求,创建一个进程 (4) 初始化一个批处理作业时,也会创建新的线程 从本质上来说在技术上只有一种创建新进程的方法,即在一个已经存在的进程中,通过系统调用来创建一个新的进程。如Linux中可以使用fork函数来创建新进程。windows中可以用createProcess函数来创建新进程。


System Calls

  • fork() vfork()
  • execl() execv() execle() execve() execlp() execvp()
  • wait() waitpid()
  • exit()
  • system()

Presenter Notes

操作系统的主要功能是为应用程序的运行创建良好的环境,为了达到这个目的,内核提供一系列具备预定功能的多内核函数,通过一组称为系统调用的(system call)的接口呈现给用户。系统调用把应用程序的请求传给内核,调用相应的的内核函数完成所需的处理,将处理结果返回给应用程序,如果没有系统调用和内核函数,用户将不能编写大型应用程序。


Process states

  • Running: runnable , has processor
  • Ready: runnable but no processor available
  • Blocked: waiting for something, so has to sleep
  • 1: The process blocks for resource or event.
  • 2: Time's up and the scheduler picks another runnable process.
  • 3: The scheduler picks a runnable process to run.
  • 4: The blocked processes is waked up by the other process.

Presenter Notes

一个进程并不是总是占用着CPU的,那么如何描述一个进程的当前状态呢?一般来说一个进程被创建开始,一直到它的生命结束为止,它只可能存在三个阶段:运行、就绪、阻塞。当然在具体实现中有的操作系统的状态个数可能不是三个。如可能把就绪和阻塞统称为暂停状态,或者是新增两个状态:创建和结束。但最核心的还是运行、就绪、阻塞。 A 运行状态 进程占用着CPU,并且在CPU上运行。显然处于这种状态的进程数量<=CPU的数目。若只有一个CPU那么任何时刻最多只能有一个进程处于运行状态。 B 就绪状态 进程已经具备了运行的条件,但由于CPU正忙着运行其他的进程,所以暂时不能运行。不过只要把CPU分给它,它就立刻可以运行。正所谓万事俱备只欠东风。 C 阻塞状态 进程因为某种事件的发生暂时不能运行的状态。例如它正等待某个输入输出的操作完成,或者它与其他线程之间存在同步关系,需要等待其他进程给它输入数据。这种情况下即使CPU已经空闲下来,这个进程还是不能运行。 D 状态切换 运行可转化为阻塞、就绪。 阻塞可转化为就绪。 就绪可转化为运行。


Scheduling

  • Both running and ready are called runnable.
  • All runnable processes are in a queue.
  • The scheduler (in the kernel) uses the scheduling algorithm to pick up a runnable process to run and a special pointer to point to it.

Presenter Notes

当计算机操作系统是多道程序设计系统时,通常就会有多个进程或线程同时竞争CPU。只要有两个或者更多的进程处于就绪状态,这种情况就会发生。如果只有一个CPU可以用。那么就必须需要选择下一个要运行的进程。在操作系统中,完成选择工作的这一部分叫做调度程序(scheduler),该程序使用的算法称为调度算法(scheduling algorithm)


Process termination

  • A process can be terminated for the following reasons.
    • The process terminates itself when done.
    • The process terminates voluntarily with error exit.
    • The process terminates involuntarily with fatal exit.
    • The process is terminated by a signal from another process.
  • When a process terminates itself, it falls into "zombie" state.

Presenter Notes

进程在创建之后,它开始运行,完成其工作。但永恒是不存在的,进程也一样。迟早这个新的进程会终止,通常由下列条件引起: 1、正常退出(自愿的) 2、出错退出(自愿的) 3、严重错误(非自愿) 4、被其它进程杀死(非自愿)


Unit summary

Having completed this unit, you should be able to:

  • Discuss what is process.
  • Discuss the difference between the program and the process.
  • Discuss the process hierarchies.
  • Discuss the process lifecycle.
  • Discuss the process states.
  • Use the system calls to control the process.

Reference

  • Chapter 2: Processes and threads, Modern Operating Systems . Forth Edition, Andrew S. Tanenbaum
  • Chapter 8: Controling processes. Advanced Programming in the UNIX Environment, Third Edition. W. Richard Stevens,Stephen A. Rago

results matching ""

    No results matching ""