date: 2024-03-15
title: 03-Process
status: DONE
tags:
- OS
- NOTE
- Lec3
archived: true
author:
- AllenYGY
created: 2024-03-20T17:14
updated: 2024-03-22
publish: True
Process
There can be several process for one program
ELFExecutable and Linkable Format
创建状态
运行状态
阻塞状态
就绪状态
终止状态
also called task control block
PCB 是内核的数据结构
PCB 是进程的唯一标志
State
PC
Register
All the PCBs together is how the kernel keeps track of which processes exist in memory, where they are in memory, what they are currently doing (Maybe State
)
If process has a single thread of execution
If a process has multiple threads of execution
调度
(algorithm inside the kernel, software) selects among available processes(i.e. in ready state) for next execution on CPU core
A context switch occurs when the CPU switches from one process to another.
保护现场
overhead
dependent on the complexity of OS
dependent on hardware support
Process Creation
Process Termination
Parent process
Parent and children
全共享
子进程共享父进程的资源
不共享
父进程等待子进程销毁后执行
倾泻式的
termination wait()system call
僵尸
A zombie process is living corpse, half alive and half dead
terminated, but still consumes system resources
- still has an entry in the process table
- where the entry is still needed to allow the parent process to read its child's exit status.
- once the exit status is read by parent via the wait system call, the zombie's entry is removed from the process table ("reaped“).
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
int main(){
pid_t pid = fork();
if (pid == 0) { /* Child */
printf("Running Child, PID = %d\n", getpid());
exit(0);
}
else {
printf("Terminating Parent, PID = %d\n", getpid());
while (1) ; /* Infinite loop */
}
return 0;
}
子程序结束却并未通过wait()回收
回收
Performed by parent on terminated child
Parent is given exit status information (by OS)
Kernel discards process
If any parent terminates without reaping a child, then child will be reaped by init or system process
So, only explicitly reaping is needed when parent is a long- running processes. e.g., shells and servers
孤儿
An orphan process is child process that is still running but parent process has finished or terminated.
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
int main(){
pid_t pid = fork();
if (pid == 0) { /* Child */
printf("Running Child, PID = %d\n", getpid());
while (1) ; /* Infinite loop */
} else {
printf("Terminating Parent, PID = %d\n", getpid());
exit(0);
}
return 0;
}
父进程结束了却没有回收子进程
IPC
Cooperating processes need interprocess communication (IPC)
User Control
Processes communicate through a shared memory
Unbounded-buffer
: no practical limit on the size of the buffer
Bounded-buffer
: buffer size is fixed Achieved by 循环队列
Kernel Control
Two operations message size is either fixed or variable
In Direct Communication, Processes must name each other explicitly.
原语
are defined as
P
Q
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
Messages are directed and received from mailboxes (also referred to as ports 端口
)
同步
Message passing may be either blocking or non-blocking
同步
异步
Different combinations possible
If both send and receive are blocking, this case is called rendezvous 会合
Queue of messages attached to the link, in kernel memory
Implemented in one of three ways
Acts as a conduit 管道
allowing two processes to communicate on the same computer
Anonymous Ordinary
Pipes
匿名进程不能通过外部进程访问
Named Pipes
套接字
Endpoint for communication 通讯的终结点
A number included at start of message packet to differentiate network services on a host
A data structure inside the kernel that represents the local end of a network connection
IP and Port
RPC
Use External Data Representation (XDR) format to account for different CPU architectures
Data Representation can be different in different CPU
-