- Process concept:为了描述程序在并发执行时对系统资源的共享,我们需要一个描述程序执行时动态特征的概念,这就是进程(A program in execution)
- Allow multiple programs to be loaded into memory and to be executed concurrently
- The basic characteristics of OS is concurrence and sharing, several programs share the resource of computer and running crossways
-
- Resource competition
- Cooperate
- Communication
- Sequential execution: only one program executes at a time, and this program has the complete control of system and has access to all the system's resources
-
- 特点
-
- 顺序性
- 封闭性
- 可再现性:初始条件相同则结果相同
- 并发环境:一定时间内,物理机器上有两个或两个以上的程序同处于开始运行但尚未结束的状态,并且次序不是事先确定的
-
- 特征
-
- 间断性(异步性)
- 失去封闭性
- 失去可再现性:失去封闭性 -----> 失去可再现性:外界环境在程序的两次执行期间发生变化,失去原有的可重复特征
- 引入多进程,提高了对硬件资源的利用率,但又带来了额外的控件和时间开销,增加了OS的复杂性
- A process includes
-
- Program code
- Program counter
- Stack
- Data section
- 进程与程序
-
- 进程是动态的,程序是静态的
- 进程是暂时的,程序是永久的
- 进程与程序的组成不同
- 进程与程序的对应关系:通过多次执行,一个程序可对应多个进程;通过调用关系,一个进程也可以包括多个程序
- 进程的特征
-
- 结构特征
-
- 进程实体 = 程序段 + 相关的数据段 + PCB
- 动态性
-
- 进程的实质是进程实体的一次执行过程,因此动态性是进程的最基本的特征
- 并发性
-
- 多个进程实体同存在于内存中,且能在一段时间内同时运行,是最重要的特征
- 独立性
-
- 指进程实体是一个能独立运行,独立分配资源和独立接收调度的基本单位
- 异步性
-
- 进程按各自独立的,不可预知的速度向前推进
- 进程的类型
-
- 系统进程
- 用户进程
- 两者区别
-
- 资源的使用
- I/O操作
-
- 用户进程不能直接做I/O操作,而系统进程可以做显式的、直接的I/O操作
- CPU工作状态
-
- 系统进程在管态下活动,而用户进程则在用户态(目态)下活动
- Process state
-
- New: the process is being created
-
- OS已完成为创建一进程所必要的工作
-
- 构造了进程标识符
- 创建了管理进程所需的表格(PCB?)
- 还没有允许执行该进程
-
- 因为资源有限
- Ready: the process is waiting to be assigned to a processor
-
- 一旦得到CPU,就立即可以运行
- Running: instruction are being executed
-
- 在系统中,总只有一个进程处于此状态
- Waiting: the processor is waiting for some event to occur
-
- 进程正在等待某个事件的发生(如等待I/O的完成),而暂停执行
- 即使给它CPU事件,它也无法执行
- Terminated: the processor has finished execution
-
- 表格和其他信息暂时由辅助程序保留
- 当数据不再需要后,进程(和它的表格)被删除
- 挂起状态(调节负载,对换,父进程,操作系统,终端用户)
-
- 终端用户的需要
-
- 当终端用户在自己的程序运行期间,发现有可疑问题时,汪汪希望暂停使自己的进程静止下来,以便研究其执行情况或对程序进行修改
-
- 如果进程处于执行状态,则暂停执行
- 如果进程处于就绪状态,则暂时不接受调度
- 父进程的需求
-
- 父进程常常希望考察和修改子进程,或者需协调各子进程间的活动,要挂起自己的子进程
- 操作系统的需要
-
- 操作系统有时需要挂起某些进程,检查运行中资源的使用情况及进行记账,以便改善系统的运行性能
- 对换的需要
-
- 为了缓和内存紧张的情况,将内存中处于阻塞状态的进程换至外存上
- 负荷调节的需要
-
- 当实时系统中的工作负荷较重,可能影响到对实时任务的控制时,可由系统把一些不重要或不紧迫的进程挂起,一保证系统仍然能正常运行
-
- 活动就绪 -----> 静止就绪
-
- 当进程处于未被挂起的就绪状态时,称此为活动就绪状态
- 当用suspend将该进程挂起后,该进程便转变为静止就绪状态,处在Readys状态的进程,不再被调度执行
- 活动阻塞 -----> 静止阻塞
-
- 当进程处于未被挂起的阻塞状态时,称它处在活动阻塞状态
- 当用suspend将其挂起后,进程变为静止阻塞状态,处于改状态的进程,在其所期待的事件出现以后,将从静止阻塞变为静止就绪
- 静止就绪 -----> 活动就绪
-
- 使用Activate
- 静止阻塞 -----> 活动阻塞
-
- 使用Activate
- Process control block
- 是进程管理和控制的最重要的数据结构,系统用它来记录进程的外部特征,描述进程的运动变化过程
- PCB是系统感知进程存在的唯一标识,进程与PCB是一一对应的
- Information associated with each process
-
- Process state
- Program counter
- CPU register
- CPU scheduling information
- Memory-management information
- I/O status information
- PCB表:系统把PCB组织在一起,并被放在内存的固定区域,就构成了PCB表
-
- PCB表的个数决定了系统中最多可同时存在的进程个数,称为系统的并发度
- 组织方式
-
- 链接方式
-
- 索引方式
-
- Schedulers
- Long-term scheduler: selects which processes should be brought into the ready queue
-
- Frequently
- Controls the degree of multiprogramming
- Short-term scheduler: selects which process should be executed next and allocates CPU
-
- Infrequently
- Midium term
-
- 为了缓和内存紧张的情况,将内存中处于阻塞状态的进程换至外存上,降低多道程序的度
- Process creation
- Parent process creates children processes, which, in turn create other processes, forming a tree of processes.
-
Resource sharing
-
- Parent and children share all resources.
- Children share subset of parent’s resources.
- Parent and child share no resources.
- Execution
-
- Parent and children execute concurrently.
- Parent waits until children terminate.
- Address space
-
- Child is duplicate of parent.
- Child has a program loaded into it.
#include<stdio.h>
void main(int argc, char *argv[]){
int pid;
pid= fork(); /*fork another process*/
if (pid < 0) {/* error occurred */
fprintf(stderr, “Fork Failed”);
exit(-1);
}
else if (pid == 0) { /* child process */
execlp(“/bin/ls”,”ls”,NULL);
} else { /* parent process */
wait(NULL);
printf(“Child Complete”);
exit(0);
}
}
-
- UNIX系统:
-
-
pid=fork();
-
从系统调用fork返回时,CPU在父进程中时,pid值为所创建子进程的进程号(>0),若在子进程中时,pid的值为零。
-
- Process termination
- Process executes last statement and asks the operating system to decide it (exit).
-
Output data from child to parent (via wait).
-
- Process' resources are deallocated by operating system.
- Parent may terminate execution of children processes (abort).
-
- Child has exceeded allocated resources.
- Task assigned to child is no longer required.
- Parent is exiting
-
- Operating system does not allow child to continue if its parent terminates.
- Cascading termination.
- Process blocking
- 进程唤醒
- Cooperating process
- Can affect or be affected by the execution of another process
- Advantages
-
- Information sharing
- Computation speed-up
- Modularity
- Convenience
- Interprocess communication(IPC)
- To exchange data and information
- Two modes
-
- Shared memory
-
- The form of the exchanged data and location are determined by the communicating processes and are not under the OS's control
- Message passing
- Direct communication
-
- Process must name each other explicitly
-
- send(P, message)
- receive(Q, message)
- Indirect communication
-
- Messages are directed and received from mailboxes(also referred to as ports)
-
- Each mailbox has an unique ID
- Process can communicate only if they share a mailbox
- Operations
-
- Create a new mailbox
- Send and receive messages through mailbox
- Destory a mailbox
- Mailbox sharing(P1 sends, P2,P3 receives?)
-
- Solutions
-
- Allow a link to be associated with at most two processes
- Allow only one process at a time to execute a receive operation
- Allow the system to select arbitrary receiver, and the sender will be notified who the receiver was.
- Buffering
-
- Queue of messages attached to the link, implemented in one of three ways
-
- Zero capacity
-
- Sender has to wait for receiver
- Bounded capacity
-
- Sender has to wait if link is full
- Unbounded capacity
-
- Sender never waits