fork.c 代码分析笔记

verifiy_area

long last_pid=0; //全局变量,用来记录眼下最大的pid数值

void verify_area(void * addr,int size) // addr 是虚拟地址 ,size是须要写入的字节大小
{
unsigned long start; start = (unsigned long) addr; //把地址强制类型转换之后,赋值给start
size += start & 0xfff; //取addr在当前虚拟地址中4M页面的偏移值,加上size能够得到要求写入虚拟地址的末端
start &= 0xfffff000; //取addr所在虚拟地址页面的起始处,即该页在数据段中偏移量
start += get_base(current->ldt[2]); //get_base得到当前进程数据段的线性基地址。加上start偏移量,于是实现了虚拟地址转化到线性地址
while (size>0) {
size -= 4096;
write_verify(start);//对线性地址start进行操作。验证该地址处页面是否可写
start += 4096;
}
}

copy_mem

int copy_mem(int nr,struct task_struct * p) //复制内存页表。把进程p的数据段copy到nr*TASK的线性地址处
{
unsigned long old_data_base,new_data_base,data_limit;
unsigned long old_code_base,new_code_base,code_limit; code_limit=get_limit(0x0f);//0x0f任务代码段选择符 ,get_limit 得到代码段的段限长
data_limit=get_limit(0x17);//0x17任务数据段选择符。get_limit 得到数据段的段限长
old_code_base = get_base(current->ldt[1]); //得到代码段的基地址
old_data_base = get_base(current->ldt[2]); //得到数据段的基地址
if (old_data_base != old_code_base) //linux 0.12 是 I&D的模式
panic("We don't support separate I&D");
if (data_limit < code_limit) //要求数据段不小于代码段
panic("Bad data_limit");
new_data_base = new_code_base = nr * TASK_SIZE; //更新代码段的基地址
p->start_code = new_code_base; //更新当前进程代码段的基地址
set_base(p->ldt[1],new_code_base);//把new_code_base 写入到ldt[1]
set_base(p->ldt[2],new_data_base);
if (copy_page_tables(old_data_base,new_data_base,data_limit)) {
//把old_date_base 线性地址的数据拷贝到new_data_base处
//copy_page_tables 成功返回0,错误返回-1.假设失败。就以页为单位。free掉new_date_base涉及的内存页
free_page_tables(new_data_base,data_limit);
return -ENOMEM;
}
return 0;
}

copy_process

/*
* Ok, this is the main fork-routine. It copies the system process
* information (task[nr]) and sets up the necessary registers. It
* also copies the data segment in it's entirety.
*/
int copy_process(int nr,long ebp,long edi,long esi,long gs,long none,
long ebx,long ecx,long edx, long orig_eax,
long fs,long es,long ds,
long eip,long cs,long eflags,long esp,long ss)
{
struct task_struct *p;
int i;
struct file *f; p = (struct task_struct *) get_free_page(); //申请一页空内存,由p指向它
if (!p)
return -EAGAIN;
task[nr] = p; //把新进程指针copy到task数组里面
*p = *current; /* NOTE! this doesn't copy the supervisor stack */
p->state = TASK_UNINTERRUPTIBLE;//copy进程的时候,p进程状态设置为TASK_UNINTERRIPTIBLE
p->pid = last_pid;
p->counter = p->priority; //执行时间数
p->signal = 0; //初始没有接受不论什么信号,信号图为空
p->alarm = 0;
p->leader = 0; //fork出的进程不继承session leader,保证session leader仅仅有一个 /* process leadership doesn't inherit */
p->utime = p->stime = 0; //用户态时间和内核态时间
p->cutime = p->cstime = 0;//子进程用户态时间和内核态时间
p->start_time = jiffies; //进程p開始时间
p->tss.back_link = 0;//改动任务状态TSS数据
p->tss.esp0 = PAGE_SIZE + (long) p; //任务内核态的栈指针
p->tss.ss0 = 0x10;
p->tss.eip = eip; //指令代码指针
p->tss.eflags = eflags;
p->tss.eax = 0;
p->tss.ecx = ecx;
p->tss.edx = edx;
p->tss.ebx = ebx;
p->tss.esp = esp;
p->tss.ebp = ebp;
p->tss.esi = esi;
p->tss.edi = edi;
p->tss.es = es & 0xffff;
p->tss.cs = cs & 0xffff;
p->tss.ss = ss & 0xffff;
p->tss.ds = ds & 0xffff;
p->tss.fs = fs & 0xffff;
p->tss.gs = gs & 0xffff;
p->tss.ldt = _LDT(nr); //_LDT宏计算出nr进程的LDT描写叙述符的选择符
p->tss.trace_bitmap = 0x80000000;
if (last_task_used_math == current)
__asm__("clts ; fnsave %0 ; frstor %0"::"m" (p->tss.i387));
if (copy_mem(nr,p)) { //把 进程p的内容copy到nr*TASK的线性地址处
task[nr] = NULL; //失败就把task数组的相应指针置为NULL说明进程创建失败
free_page((long) p); //善后,把p相关的内存页释放
return -EAGAIN;
}
for (i=0; i<NR_OPEN;i++) //假设以上copy_mem成功,则把parent 打开的文件也让child继承
if (f=p->filp[i])
f->f_count++;
if (current->pwd)//引用+1
current->pwd->i_count++;
if (current->root)
current->root->i_count++;
if (current->executable)
current->executable->i_count++;
if (current->library)
current->library->i_count++;
set_tss_desc(gdt+(nr<<1)+FIRST_TSS_ENTRY,&(p->tss));
set_ldt_desc(gdt+(nr<<1)+FIRST_LDT_ENTRY,&(p->ldt));
p->p_pptr = current;
p->p_cptr = 0;
p->p_ysptr = 0;
p->p_osptr = current->p_cptr;
if (p->p_osptr)
p->p_osptr->p_ysptr = p;
current->p_cptr = p;
p->state = TASK_RUNNING; /* do this last, just in case */
return last_pid;
}

find_empty_process

int find_empty_process(void) //为新进程获取不反复的pid
{
int i; repeat:
if ((++last_pid)<0) last_pid=1;
for(i=0 ; i<NR_TASKS ; i++)
if (task[i] && ((task[i]->pid == last_pid) ||
(task[i]->pgrp == last_pid))) //假设last_pid存在,那么repeat再測试 ++last_pid
goto repeat;
//在已经把lastpid变成全部进程都不同的pid之后,以下继续
for(i=1 ; i<NR_TASKS ; i++) //找出一个空暇进程。返回它的索引 i
if (!task[i])
return i;
return -EAGAIN; //假设64个进程都存在,那么报错
}

《linux 内核全然剖析》 fork.c 代码分析笔记的更多相关文章

  1. 《linux 内核全然剖析》sched.c sched.h 代码分析笔记

    版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/u011368821/article/details/25129835 sched.c sched.h ...

  2. 《linux 内核全然剖析》 sys.c 代码分析

    sys.c 代码分析 setregid /* * This is done BSD-style, with no consideration of the saved gid, except * th ...

  3. 《linux 内核全然剖析》 笔记 CODE_SPACE 宏定义分析

    在memory.c里面.遇到一个宏定义,例如以下: #define CODE_SPACE(addr) ((((addr)+4095)&~4095) < \ current->sta ...

  4. 《linux 内核全然剖析》编译linux 0.12 内核 Ubuntu 64bits 环境

    我×.. . 最终好了,大概3 4个小时吧...各种毛刺问题.终究还是闯过来了.. .. ubuntu2@ubuntu:~/Downloads/linux-0.00-050613/linux-0.00 ...

  5. 《linux 内核全然剖析》 chapter 2 微型计算机组成结构

    微型计算机组成结构 系统的基本组成: 软件是一种控制硬件操作和动作的指令流. 2.1 微型计算机的组成原理 当中CPU通过地址线,数据线,和控制信号线组成的内部总线与系统其它部分进行数据通信.地址线用 ...

  6. 《linux 内核全然剖析》 chapter 4 80x86 保护模式极其编程

    80x86 保护模式极其编程       首先我不得不说.看这章真的非常纠结...看了半天.不知道这个东西能干嘛.我感觉唯一有点用的就是对于内存映射的理解...我假设不在底层给80x86写汇编的话.我 ...

  7. 《linux 内核全然剖析》 mktime.c

    tm结构体的定义在time.h里面 struct tm { int tm_sec; int tm_min; int tm_hour; int tm_mday; int tm_mon; int tm_y ...

  8. 《linux 内核全然剖析》 include/asm/io.h

    include/asm/io.h #define outb(value,port) \ __asm__ ("outb %%al,%%dx"::"a" (valu ...

  9. 《Linux内核设计与实现》 Chapter4 读书笔记

    <Linux内核设计与实现> Chapter4 读书笔记 调度程序负责决定将哪个进程投入运行,何时运行以及运行多长时间,进程调度程序可看做在可运行态进程之间分配有限的处理器时间资源的内核子 ...

随机推荐

  1. Angular 学习笔记——sublime

    div.row>div.col-md-12*10 就等于 <div class="row">    <div class="col-md-12&q ...

  2. iOS开发:Framework的创建

    转载自:http://jonzzs.cn/2017/06/01/iOS%20开发笔记/[iOS%20开发]将自己的框架打包成%20Framework%20的方法/ 环境:Xcode 8 创建 Fram ...

  3. bat 同步windows系统时间

    需要使用管理员权限运行 net start w32timew32tm /config /updatew32tm /resync /rediscovernet stop w32timepause

  4. 网络编程 TCP学习

    上传txt文本 通过socket向服务端发送数据 然后用serversocket 接收socket 通过流读取数据保存 服务端在发送确认信息并在client输出 client import java. ...

  5. MySQL几个性能指标

    近期参加了一个DBA MySQL的分享,主要从MySQL的性能指标分析.同步及运维三个方面分享一些经验,其中,一些经验值还是值得记录下来的: 对于一个MySQL实例,CRUD上限经验值如下: Quer ...

  6. Rosbridge 的使用

      参考文献: http://rosclub.cn/post-569.html ROS与Android的窃窃私语   1.简介 ROSbridge 顾名思义,是一个ROS当中的中间件,ROS 桥,是用 ...

  7. mybatis 遇到的问题

    顺序问题:在resultmap中,result必须在association之前.否则会报错 只查出一条记录的问题  :重名的id,要起别名.而且不能忽略. mybaits错误解决:There is n ...

  8. 超高逼格Log日志打印

    代码地址如下:http://www.demodashi.com/demo/12646.html 前言 Log日志的打印一直是一个比较头疼的事,怎样才能让自己的log显示更多信息,怎样才能让自己的log ...

  9. iOS语言本地化,中文显示

    尽管一直相信xcode肯定提供有语言本地化的设置地方,可是一直也没凑着去改.非常多的汉化,还是使用代码去控制:比方navagition的return使用代码改动为"返回"! 近期在 ...

  10. Android实用工具

    1 json类:hiJson 格式化json字符串 2 sqlite类:sqlitespy,SQLiteExpertSetup 3