第四章

advertisement
Chapter 4: Processes
 Process Concept
 Process Scheduling
 Operations on Processes
 Cooperating Processes
 Interprocess Communication
 Communication in Client-Server Systems
Operating System Concepts
4.1
Chapter four
Process Concept
 An operating system executes a variety of programs:
 Batch system – jobs(作业)
 Time-shared systems – user programs or tasks(任务)
 Textbook uses the terms job and process almost
interchangeably.
 Process – a program in execution; a program is a passive
entity,while a process is an active entity(进程是有生命的,
程序是无生命的)
 A process must include:
 Text section(program code,程序代码)
 Current activity(program counter&processor register,当前状态)
 Stack(存放临时数据:如函数参数、返回地址)
 data section(数据区,存放进程执行时用到的数据)
Operating System Concepts
4.2
Chapter four
Process State
 进程是活动着的程序,任何进程在任何时刻都处于某一状态(state):
 New(新建): 进程刚被创建,还没提交给系统
 Running(正在运行): 占有CPU.
 Waiting(等待): 进程因为等待某事件的发生如I/O完成,不能继续执行.
 Ready(就绪): 万事具备,只欠东风(被执行).
 Terminated(终止): 进程已经执行完.
 实际系统中进程的状态划分略有差异,如 LINUX:
 TASK_RUNNING(就绪或执行):
R
 TASK_INTERRUPTIBLE(可中断等待).
S
 TASK_UNINTERRUPTIBLE(不可中断等待):
D
 TASK_STOPPED(暂停).
T
 TASK_ZOMBIE(僵死)
Z
#ps –el 详细列出当前系统中所有的进程,其中的S属性即为进程状态
#ps ax 按BSD格式列出进程,其中的STAT属性还列出以下属性:
W: has no resident pages
<: high-priority process
N: low_priority task
L: has pages locked into memory(for real-time)
Operating System Concepts
4.3
Chapter four
diagram of process state(5 states)
Operating System Concepts
4.4
Chapter four
Linux进程状态图
do_fork()
收到 SIG_KILL 或 SIG_CONT
后,执行 wake_up( )
TASK_RUNNING
所申请资源有效时,
wake_up()或
wake_up_interruptible()
TASK_INTERRUPTIBLE
TASK_UNINTERRUPTIBLE
申请资源未果,
sleep_on()
interruptible_s leep_on()
schedule()
所申请资源有效,
或收到 signal 后
时间片到
schedule( )
申请资源未果,
schedule( )
拥有 CPU
跟踪系统调用,执行 syscall_trace( )
sys_exit( )
schedule( )
TASK_STOPPED
Operating System Concepts
do_exit( )
4.5
TASK_ZOMBIE
Chapter four
Process Control Block (PCB,进程控制块)
OS为了管理、控制进程,设置PCB,存储进程相关信息
 Process state(状态)
 Program counter(程序计数器)
 CPU registers(寄存器值)
 CPU scheduling information(调度信息)
 Memory-management information(存储管理信息)
 Accounting information(记帐信息,如CPU time)
 I/O status information(I/O状态信息,如打开的文件
)
Operating System Concepts
4.6
Chapter four
Process Control Block (PCB)
Operating System Concepts
4.7
Chapter four
CPU Switch From Process to Process
Operating System Concepts
4.8
Chapter four
进程调度队列(Process Scheduling Queues)
 作业队列(Job queue) – set of all processes in the
system.
 就绪队列(Ready queue) – set of all processes
residing in main memory, ready and waiting to
execute.
 设备队列(Device queues) – set of processes waiting
for an I/O device.
 OS调度进程,使进程在各队列间迁移( migrate)
Operating System Concepts
4.9
Chapter four
Ready Queue And Various I/O Device Queues
Operating System Concepts
4.10
Chapter four
进程在各队列间迁移(方块表示队列,圆圈表示资源)
Operating System Concepts
4.11
Chapter four
调度(schedulers,选择进程在各队列间迁移)
 Long-term scheduler (or job scheduler,作业调度)
– selects which processes should be brought into
the ready queue.
- always appears on batch systems, not present
on time-sharing OSs like UNIX.(往往在批处理系
统中出现,以控制系统中的并行作业数)
 Short-term scheduler (or CPU scheduler,CPU调
度) – selects which process should be executed
next and allocates CPU.
 Medium-term scheduler(swapping,对换) – swap
in, swap out, partially executed processes
Operating System Concepts
4.12
Chapter four
对换调度(Medium Term Scheduling)
Operating System Concepts
4.13
Chapter four
Schedulers (Cont.)
 Short-term scheduler is invoked very frequently
(milliseconds)  (must be fast).(频繁发生,毫秒级)
 Long-term scheduler is invoked very infrequently (seconds,
minutes)  (may be slow).
 The long-term scheduler controls the degree of
multiprogramming.
 Processes can be described as either:
 I/O-bound process(I/O约束的进程) – spends more time
doing I/O than computations, many short CPU bursts.
 CPU-bound process(CPU约束的进程) – spends more time
doing computations; few very long CPU bursts.
Operating System Concepts
4.14
Chapter four
进程上下文切换(Context Switch)
 When CPU switches to another process, the
system must save the state of the old process and
load the saved state for the new process.(进程切
换时必须保存当前进程的状态,恢复调入进程的状
态)
 Context-switch time is overhead; the system does
no useful work while switching.(很费时,如果系
统频繁进行进程切换,将严重影响系统性能)
 Time dependent on hardware support.
Operating System Concepts
4.15
Chapter four
Operation on process:Process Creation
 Parent process create 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.
Operating System Concepts
4.16
Chapter four
Process Creation (Cont.)
 Address space
 Child duplicate of parent.
 Child has a program loaded into it.
 UNIX examples
 fork system call creates new process
 exec system call used after a fork to replace the
process’ memory space with a new program.
Operating System Concepts
4.17
Chapter four
Processes Tree on a UNIX System
Pagedaemon:2#进程, swapper:3#进程,init :1#进程
Operating System Concepts
4.18
Chapter four
#include <stdio.h>
结果:
void main()
parent waiting…
{
child process executing…
int pid;
<ls result>
pid = fork(); //system call
child complete
if (pid < 0 ) { //error ocurrred
printf(“fork failed.”);
exit(-1);
//system call
}
else if (pid == 0 ) { //child process
printf(“child process executing…\n”);
execlp(“/bin/ls”,”ls”,NULL); //system call
}
else {
//parent process
printf(“parent wating…\n”);
wait(NULL); //system call, wait for children completion
printf(“child complete.”);
exit(0);
}
}
Operating System Concepts
4.19
Chapter four
Operation on process: Process Termination
 Process executes last statement and asks the operating
system to delete 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.(级联终止)
Operating System Concepts
4.20
Chapter four
Cooperating Processes
 Independent process cannot affect or be affected
by the execution of another process.
 Cooperating process can affect or be affected by
the execution of another process
 Advantages of process cooperation
 Information sharing
 Computation speed-up
 Modularity
 Convenience
Operating System Concepts
4.21
Chapter four
Example of cooperating processes:
Producer-Consumer Problem
 Paradigm for cooperating processes, producer
process produces information that is consumed
by a consumer process.
 unbounded-buffer places no practical limit on the
size of the buffer.(无限缓冲)
 bounded-buffer assumes that there is a fixed
buffer size.(有限缓冲)
Operating System Concepts
4.22
Chapter four
Bounded-Buffer – Shared-Memory Solution
 Shared data
#define BUFFER_SIZE 10
Typedef struct {
...
} item;
item buffer[BUFFER_SIZE];
int in = 0;
int out = 0;
Operating System Concepts
4.23
Chapter four
Bounded-Buffer
Producer:
item nextProduced;
while (1) {
while (((in + 1) % BUFFER_SIZE) ==
out)
; /* do nothing */
buffer[in] = nextProduced;
in = (in + 1) % BUFFER_SIZE;
Consumer:
}
item nextConsumed;
Solution is correct, but can only
while (1) {
use BUFFER_SIZE-1 elements(why?)
while (in == out)
; /* do nothing */
nextConsumed = buffer[out];
out = (out + 1) % BUFFER_SIZE;
}
Operating System Concepts
4.24
Chapter four
Interprocess communication(IPC)
 Mechanism for processes to communicate and to synchronize
their actions.
 signal(信号机制):给其他进程发送异步事件信号。如:
# kill –9 pid :向 pid 进程发送9号信号(无条件终止)
# kill –l :显示系统中所有的信号
 # ls –l | wc –l :求出当前目录下的文件数
 pipe(管道技术):一个进程的输出作为另外一个进程的输入,
实现相关进程(如父子进程)通信,可看作一个临时文件。如
:
 无名管道(pipe):#ls –l | grep ‘^d’,实现相同父进程的子进
程间通信。
 命名管道(named pipe,FIFO):是有名字的管道,在OS中作为一个
对象(文件,即FIFO文件)存在,实现任何进程间通信
# mkfifo pipename
# ls –l > pipename & wc –l < pipename
Operating System Concepts
4.25
Chapter four
共享内存(shared
memory,SM):通过将两个或多个进程地址空间的
一部分映射到相同的物理内存来实现共享内存机制,利用共享内存可
以实现灵活的进程间通讯机制。但是,内存被共享之后,对共享内存
的访问同步需要由其他 IPC 机制,例如信号量来实现。
消息传递(Message passing)
信号量(semaphore,第七章讲)
基于C/S的IPC(sockets,RPC,RMI)
消息队列、信号量、共享内存是UNIX SYSV中三个经典的IPC机制
信号及管道是UNIX中较早使用的IPC机制
Operating System Concepts
4.26
Chapter four
IPC:message passing
 Message system – processes communicate with
each other without resorting to shared variables.
 IPC facility provides two operations:
 send(message) – message size fixed or variable
 receive(message)
 If P and Q wish to communicate, they need to:
 establish a communication link between them
 exchange messages via send/receive
Operating System Concepts
4.27
Chapter four
Message-passing:Implementation Questions
 Direct or indirect communication
 Synchronization scheme
 Buffering methods
Operating System Concepts
4.28
Chapter four
Message-passing: Direct Communication
 Processes exchange messages directly
 Symmetric: processesmust name each other explicitly:
send (P, message) – send a message to process P
receive(Q, message) – receive a message from
process Q
 Asymmetric: only the sender names the recipient
send (P, message) – send a message to process P
receive(id, message) – receive all messages
associated with process “id”
 Properties of communication link
 Links are established automatically.
 A link is associated with exactly one pair of communicating
processes.
 Between each pair there exists exactly one link.
 The link may be unidirectional, but is usually bi-directional.
Operating System Concepts
4.29
Chapter four
Message-passing: Indirect Communication
 Messages are directed and received from
mailboxes (also referred to as ports).
 Each mailbox has a unique id.
 Processes can communicate only if they share a
mailbox.
 Properties of communication link
 Link established only if processes share a common
mailbox
 A link may be associated with many processes.
 Each pair of processes may share several
communication links.
 Link may be unidirectional or bi-directional.
Operating System Concepts
4.30
Chapter four
Message-passing: Indirect Communication
 Operations
 create a new mailbox
 send and receive messages through mailbox
 destroy a mailbox
 Primitives are defined as:
send(A, message) – send a message to
mailbox A
receive(A, message) – receive a message
from mailbox A
Operating System Concepts
4.31
Chapter four
Message-passing: Indirect Communication
 Mailbox sharing
 P1, P2, and P3 share mailbox A.
 P1, sends; P2 and P3 receive.
 Who gets the message?
 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 arbitrarily the receiver.
Sender is notified who the receiver was.
Operating System Concepts
4.32
Chapter four
Message-passing: Synchronization
 Message passing may be either blocking or




non-blocking.
Blocking is considered synchronous
Non-blocking is considered asynchronous
send and receive primitives may be either
blocking or non-blocking.
The most common case is non-blocking send
and blocking receive
Operating System Concepts
4.33
Chapter four
Message-passing: Buffering
 Queue of messages attached to the link;
implemented in one of three ways.
1. Zero capacity – 0 messages
Sender must wait for receiver
2. Bounded capacity – finite length of n messages
Sender must wait if link full.
3. Unbounded capacity – infinite length
Sender never waits.
Case 1 is also called no buffering
Case 2 and 3 are also called automatic buffering
Operating System Concepts
4.34
Chapter four
Linux中的消息队列(参见ipc目录)
Message queue is also called mailbox
Is an indirect message communication method
msgque
msgid_ds
msg_perms
msg
msg
msg_next
msg_first
msg_next
msg_type
msg_spot
msg_last
msg_stime
msg_ts
message
Msgque:消息队列数组,其下标subscription就是相应消息队列的id;
Msgid_ds:消息队列头,是对某个消息队列的描述,包括访问权限(msg_perms),
及对队列中消息的管理
Msg: 即消息节点
Operating System Concepts
4.35
Chapter four
消息队列数据结构:
static struct msqid_ds *msgque[MSGMNI]; /* ipc/msg.c */
struct msqid_ds {
/* 每个消息队列占一个 msqid_ds 结构,include/linux/msg.h */
struct ipc_perm msg_perm;
struct msg *msg_first; /* 指向消息队列的第一条消息 */
struct msg *msg_last;
/* 指向消息队列的最后一条消息 */
time_t msg_stime;
/* 最后发送时间 */
time_t msg_rtime;
/* 最后接收时间 */
time_t msg_ctime;
/* 最后修改时间 */
struct wait_queue *wwait; /* 写消息进程的等待队列 */
struct wait_queue *rwait; /* 读消息进程的等待队列 */
ushort msg_cbytes;
/* 队列中消息的字节数 */
ushort msg_qnum;
/* 队列中的消息数 */
ushort msg_qbytes;
/* 队列中消息的最大字节数 */
ushort msg_lspid;
/* 最后一个发送消息的进程的标识号 */
ushort msg_lrpid;
/* 最后一个接收消息的进程的标识号 */
};
struct msg { /* 每条消息占一个 msg 结构,include/linux/msg.h */
struct msg *msg_next;
/* 后继节点,后一条消息 */
long msg_type;
/* 消息类型 */
char *msg_spot;
/* 消息文本的地址 */
time_t msg_stime;
/* 发送此条消息的时间 */
short msg_ts;
/* 消息文本的长度 */
};
Operating System Concepts
4.36
Chapter four
struct ipc_perm
{
key_t key;
ushort uid;
ushort gid;
ushort cuid;
ushort cgid;
ushort mode;
ushort seq;
};
/*
/*
/*
/*
/*
/*
/*
整型,0 表示 private,非 0 表示 public */
资源拥有者的有效标识 */
资源拥有者所在组的有效标识 */
资源创建者的有效标识 */
资源创建者所在组的有效标识 */
访问模式,其意义同文件访问模式 */
序列号 */
如果 key 是 public,那么任何进程都可通过 key 找到该消息队列。
Operations on message queue:
Msgget
Msgsend
Msgrcv
Msgctl
Operating System Concepts
4.37
Chapter four
Client-Server Communication
 Communications may take place between
processes on different machines. Some
methods used:
 Sockets
 Remote Procedure Calls
 Remote Method Invocation (Java)
Operating System Concepts
4.38
Chapter four
Sockets(套接字)








Originated from BSD UNIX
A socket is defined as an endpoint for communication.
A socket is a concatenation of IP address and port(端口)
Port:一个16位的整数,1024以下为系统用,如23为telnet service
专用,21为ftp server专用,80为http server专用,应用程序应使
用1024以上的端口
The socket 161.25.19.8:1625 refers to port 1625 on host
161.25.19.8
Communication takes place between a pair of sockets.
Type of socket:
* connection-oriented(TCP,面向连接):可靠
* connectionless(UDP,无连接):可靠性较差,但方便
* multicast(多播):可以连接至多个节点)
Any socket may be either blocking or non-blocking
Operating System Concepts
4.39
Chapter four
Socket Communication
Operating System Concepts
4.40
Chapter four
A typical C/S communication using sockets
Server side:
getprotobyname
Client side:
gethostbyname
socket
getprotobyname
bind
socket
listen
connect
accept
recv
send
close
close
Operating System Concepts
4.41
Chapter four
Service side:
client side:
int sd,sd2;
int port = 1280;
sd=socket(AF_INET, SOCK_STREAM,0);
BindSocket( sd , nPort ) ;
ListenSocket( sd );
while(1){
sd2=AcceptSocket( sd );
SendData(sd2,databuf);
closesocket(sd2);
}
int sd;
int port = 1280;
sd=socket(AF_INET, SOCK_STREAM,0);
ConnectSocket(sd,port);
RecvData(sd,databuf);
closesocket(sd);
Operating System Concepts
4.42
Chapter four
Remote Procedure Calls(远程过程调用)
 Remote procedure call (RPC) abstracts procedure calls between






processes on networked systems.
操作系统中的很多服务(services)依赖于RPC来实现,如
windows2000中的COM+、DHCP、DNS server、remote storage
、telnet等)
Stubs(存根) –proxy for the actual procedure on the server.(隐藏
RPC调用的细节)
The client-side stub locates the server and packs and sends the
parameters
The server-side stub receives this message, unpacks the
parameters, and performs the procedure on the server.
一个RPC服务对应一个port(端口,就像socket中的port),如果
client不知道某个RPC的port,可以询问matchmaker(媒人
),matchmaker也是一个服务,但它占用固定的RPC端口
OSF-DCE:open software foundation-distributed computing
environment,一个RPC标准,要求RPC简洁、透明、高效
Operating System Concepts
4.43
Chapter four
Remote Procedure Calls
 Microsoft RPC:基于OSF RPC
- MIDL:Microsoft Interface Definition Language,接
口描述语言,用它来描述对远程过程的访问
- MIDL编译器生成相应的stub
- stub调用client side run-time library来发送请求及参
数
- server端完成相应的过程
Operating System Concepts
4.44
Chapter four
Operating System Concepts
4.45
Chapter four
Remote Method Invocation(远程方法调用)
 Remote Method Invocation (RMI) is a Java
mechanism similar to RPCs.(JAVA版的RPC)
 RMI allows a Java program on one machine to
invoke a method on a remote object.(允许应用程序
调用一个异地对象的方法)
Operating System Concepts
4.46
Chapter four
RMI的参数传递
Operating System Concepts
4.47
Chapter four
Download