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: TrueProcess
There can be several process for one program

ELFExecutable and Linkable Format


创建状态运行状态阻塞状态就绪状态终止状态
also called task control blockPCB 是内核的数据结构PCB 是进程的唯一标志StatePCRegisterAll 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; 
}
父进程结束了却没有回收子进程
IPCCooperating processes need interprocess communication (IPC)

User ControlProcesses 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 ControlTwo operations message size is either fixed or variable
In Direct Communication, Processes must name each other explicitly.
原语 are defined as 
P QLinks 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
- 