-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
240 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
|
||
```golang | ||
|
||
type hchan struct { | ||
qcount uint //Channel 中的元素个数; | ||
dataqsiz uint //Channel 中的循环队列的长度 | ||
buf unsafe.Pointer //Channel 的缓冲区数据指针 | ||
elemsize uint16 | ||
closed uint32 | ||
elemtype *_type | ||
sendx uint // Channel 的发送操作处理到的位置 | ||
recvx uint // Channel 的接收操作处理到的位置 | ||
recvq waitq | ||
sendq waitq | ||
|
||
lock mutex | ||
} | ||
|
||
type waitq struct { | ||
first *sudog // 表示一个在等待列表中的 Goroutine, | ||
last *sudog | ||
} | ||
|
||
|
||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
接口也是 Go 语言中的一种类型,它能够出现在变量的定义、函数的入参和返回值中并对它们进行约束,不过 Go 语言中有两种略微不同的接口, | ||
|
||
|
||
- runtime.iface 表示第一种接口,带有一组方法的接口, | ||
- runtime.eface 表示第二种不包含任何方法的接口 | ||
|
||
```golang | ||
type eface struct { // 16 字节 | ||
_type *_type | ||
data unsafe.Pointer | ||
} | ||
|
||
type _type struct { | ||
size uintptr //类型大小 | ||
ptrdata uintptr //含有所有指针类型前缀大小 | ||
hash uint32 //类型hash值;避免在哈希表中计算 | ||
tflag tflag //额外类型信息标志 | ||
align uint8 //该类型变量对齐方式 | ||
fieldalign uint8 //该类型结构字段对齐方式 | ||
kind uint8 //类型编号 | ||
alg *typeAlg //算法表 存储hash和equal两个操作。map key便使用key的_type.alg.hash(k)获取hash值 | ||
// gcdata stores the GC type data for the garbage collector. | ||
// If the KindGCProg bit is set in kind, gcdata is a GC program. | ||
// Otherwise it is a ptrmask bitmap. See mbitmap.go for details. | ||
gcdata *byte //gc数据 | ||
str nameOff // 类型名字的偏移 | ||
ptrToThis typeOff | ||
} | ||
``` | ||
|
||
|
||
```golang | ||
type iface struct { // 16 字节 | ||
tab *itab | ||
data unsafe.Pointer | ||
} | ||
|
||
// 非空接口的类型信息 | ||
type itab struct { | ||
//inter 和 _type 确定唯一的 _type类型 | ||
inter *interfacetype // 接口自身定义的类型信息,用于定位到具体interface类型 | ||
_type *_type // 接口实际指向值的类型信息-实际对象类型,用于定义具体interface类型 | ||
hash int32 //_type.hash的拷贝,用于快速查询和判断目标类型和接口中类型是一致 | ||
_ [4]byte | ||
fun [1]uintptr //动态数组,接口方法实现列表(方法集),即函数地址列表,按字典序排序 | ||
//如果数组中的内容为空表示 _type 没有实现 inter 接口 | ||
|
||
} | ||
|
||
// 非空接口类型,接口定义,包路径等。 | ||
type interfacetype struct { | ||
typ _type | ||
pkgpath name | ||
mhdr []imethod // 接口方法声明列表,按字典序排序 | ||
} | ||
|
||
// 接口的方法声明,一种函数声明的抽象 | ||
// 比如:func Print() error | ||
type imethod struct { | ||
name nameOff // 方法名 | ||
ityp typeOff // 描述方法参数返回值等细节 | ||
} | ||
|
||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
```golang | ||
type maptype struct { | ||
typ _type | ||
key *_type | ||
elem *_type | ||
bucket *_type // internal type representing a hash bucket | ||
keysize uint8 // size of key slot | ||
valuesize uint8 // size of value slot | ||
bucketsize uint16 // size of bucket | ||
flags uint32 | ||
} | ||
|
||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
|
||
Do not communicate by sharing memory; | ||
instead, share memory by communicating. | ||
|
||
不要以共享内存的方式来通信,相反,要通过通信来共享内存。 | ||
|
||
|
||
|
||
|
||
- M 代表着一个内核线程,也可以称为一个工作线程。goroutine就是跑在M之上的 | ||
|
||
|
||
- P 代表着(Processor)处理器 它的主要用途就是用来执行goroutine的,一个P代表执行一个Go代码片段的基础(可以理解为上下文环境),所以它也维护了一个可运行的goroutine队列,和自由的goroutine队列,里面存储了所有需要它来执行的goroutine。 | ||
|
||
|
||
- G 代表着goroutine 实际的数据结构(就是你封装的那个方法),并维护者goroutine 需要的栈、程序计数器以及它所在的M等信息。 | ||
|
||
|
||
- Seched 代表着一个调度器 它维护有存储空闲的M队列和空闲的P队列,可运行的G队列,自由的G队列以及调度器的一些状态信息等 | ||
|
||
|
||
[1.14 抢占式调度](http://xiaorui.cc/archives/6535) | ||
|
||
|
||
```golang | ||
func initsig(preinit bool) { | ||
// 预初始化 | ||
if !preinit { | ||
signalsOK = true | ||
} | ||
//遍历信号数组 | ||
for i := uint32(0); i < _NSIG; i++ { | ||
t := &sigtable[i] | ||
//略过信号:SIGKILL、SIGSTOP、SIGTSTP、SIGCONT、SIGTTIN、SIGTTOU | ||
if t.flags == 0 || t.flags&_SigDefault != 0 { | ||
continue | ||
} | ||
... | ||
setsig(i, funcPC(sighandler)) | ||
} | ||
} | ||
|
||
|
||
|
||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,90 @@ | ||
操作系统划分为 | ||
- 内核空间 | ||
# 操作系统 | ||
|
||
- 用户空间 | ||
|
||
用户是通过用户空间与操作系统打交道的, | ||
|
||
程序员开发使用的应用程序也位于用户空间 | ||
|
||
- 用户空间 | ||
用户空间不能直接访问内核,但是可以通过系统调用来访问 | ||
|
||
- 内核空间 | ||
# 进程 | ||
|
||
计算机已运行的实体 | ||
|
||
进程 | ||
一个进程是通过另一个进程创建的,通过fork系统调用实现的 | ||
产生的新的进程就是子进程 | ||
所有的进程都有一个父进程,init进程除外 | ||
|
||
wait 系统调用用来暂停父进程,直到子进程退出 | ||
|
||
进程0负责系统初始化工作,并创建进程1,进程1 也叫做init进程 | ||
|
||
init进程,mingetty、agetty程序分别在虚拟终端和串口终端进行监听 | ||
|
||
--- | ||
|
||
进程状态 | ||
- 运行 | ||
进程被创建后,就变为运行进程 | ||
意味着内核已经为cpu执行进程建立起所有的结构,并获取所有必要的信息 | ||
- 就绪 | ||
进程准备变为运行进程但还没有被选中运行时,处于就绪状态 | ||
- 睡眠 | ||
- 死亡 | ||
- 僵尸进程:进程已经结束,但是其父进程没有调用它的wait 。除了进程描述符仍被保留外,其他的资源都被释放并回收 | ||
|
||
|
||
|
||
|
||
任何进程都可以通过调用wait、waitpid来获取子进程的退处状态。即使子进程已经终止,也需要为父进程保留少量信息。 | ||
|
||
|
||
--- | ||
进程描述符 | ||
|
||
每个进程都有一个进程描述符,存放描述进程的所有信息 | ||
内核采用循环双向链表来存放所有进程描述符 | ||
|
||
|
||
进程的生命周期中,进程描述符记录的信息 | ||
- 进程的属性 | ||
- 进程间的关系 | ||
- 文件管理 | ||
- 信号管理 | ||
- 进程的信任状态 | ||
- 资源限制 | ||
- 与调度相关的字段 | ||
|
||
|
||
--- | ||
|
||
进程相关的字段 | ||
|
||
1、state记录进程的状态 | ||
2、pid 进程的唯一标识符 | ||
|
||
|
||
--- | ||
操作系统通过系统调用fork、vfork、clone来完成进程的创建。 | ||
这三个系统调用最终都调用了内核函数do_fork | ||
|
||
## fork函数 | ||
|
||
返回了两次,一次实在父进程,一次实在子进程。 | ||
如果在子进程中返回,将返回0。 | ||
如果在父进程中返回,将返回子进程PID。 | ||
fork 函数被调用时,该函数把包括该系统调用表索引在哪的一些必要信息放入适当的寄存器 | ||
|
||
|
||
## vfork 函数 | ||
|
||
vfork 函的父进程一直阻塞,直到子进程调用exit或exec为止 | ||
|
||
## clone 函数 | ||
|
||
把一个指向函数的指针和该函数的参数作为自己的参数。 | ||
由do_fork 创建的子进程刚一诞生就调用这个函数 | ||
|
||
|