⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 fork.c.bak

📁 一份精简的linux内核源代码
💻 BAK
字号:
/* *  linux/kernel/fork.c * *  (C) 1991  Linus Torvalds *//* *  'fork.c' contains the help-routines for the 'fork' system call * (see also system_call.s), and some misc functions ('verify_area'). * Fork is rather simple, once you get the hang of it, but the memory * management can be a bitch. See 'mm/mm.c': 'copy_page_tables()' */#include <errno.h>#include <linux/sched.h>#include <linux/kernel.h>#include <asm/segment.h>#include <asm/system.h>extern void write_verify(unsigned long address);long last_pid=0;/*特权级0的代码在执行时不会理会用户空间中的页面是否是页保护的*/void verify_area(void * addr,int size)/*对add开始,长度为size的页面进行写验证*/{	unsigned long start;	start = (unsigned long) addr;	size += start & 0xfff;	start &= 0xfffff000;	start += get_base(current->ldt[2]);	while (size>0) {		size -= 4096;		write_verify(start);/*页面写验证*/		start += 4096;	}}int copy_mem(int nr,struct task_struct * p)/*设置新进程的代码和数据段基址、限长、并复制当前进程内存分页管理的页表*/{	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);	data_limit=get_limit(0x17);	old_code_base = get_base(current->ldt[1]);	old_data_base = get_base(current->ldt[2]);	if (old_data_base != old_code_base)		panic("We don't support separate I&D");	if (data_limit < code_limit)		panic("Bad data_limit");	new_data_base = new_code_base = nr * 0x4000000;	p->start_code = new_code_base;	set_base(p->ldt[1],new_code_base);	set_base(p->ldt[2],new_data_base);	if (copy_page_tables(old_data_base,new_data_base,data_limit)) {		free_page_tables(new_data_base,data_limit);		return -ENOMEM;	}	return 0;}/* *  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 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();/*为新进程申请一页内存来存放其PCB*/	if (!p)		return -EAGAIN;	task[nr] = p;	*p = *current;	/*将当前进程的PCB复制给新建的进程*//* NOTE! this doesn't copy the supervisor stack */	p->state = TASK_UNINTERRUPTIBLE;/*开始修改新进程的PCB,首先将新进程置为不可中断*/	p->pid = last_pid;/*当前进程的下一个进程设为新进程*/	p->father = current->pid;/*把当前进程设置为新进程的父进程*/	p->counter = p->priority;/*设置初始的进程运行时间片*/	p->signal = 0;	p->alarm = 0;	p->leader = 0;		/* process leadership doesn't inherit */	p->utime = p->stime = 0;	p->cutime = p->cstime = 0;	p->start_time = jiffies;/*进程开始时间(从开机到当前为止的时间长度)*/	p->tss.back_link = 0;	p->tss.esp0 = PAGE_SIZE + (long) p;/*新进程的内核堆栈栈底指针,指向保存PCB(task_struct数据结构)页面的末端*/	p->tss.ss0 = 0x10;                 /*新进程的内核堆栈段地址,被设为内核数据段选择符(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;/*新进程内核态堆栈被设为新进程PCB所在内存页面的顶端*/	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被设置为局部表描述符在gdt中的索引值*/	p->tss.trace_bitmap = 0x80000000;	if (last_task_used_math == current)/*如果当前进程使用了协处理器,则还需要把协处理器的完整状态保存到新进程的tss.i386结构中*/		__asm__("clts ; fnsave %0"::"m" (p->tss.i387));	if (copy_mem(nr,p)) {		task[nr] = NULL;		free_page((long) p);		return -EAGAIN;	}	for (i=0; i<NR_OPEN;i++)		if (f=p->filp[i])			f->f_count++;	if (current->pwd)/*更新当前进程工作目录所含的节点数*/		current->pwd->i_count++;	if (current->root)/*更新当前进程根目录所含的节点数*/		current->root->i_count++;	if (current->executable)/*更新当前进程打开文件的被复用次数*/		current->executable->i_count++;	set_tss_desc(gdt+(nr<<1)+FIRST_TSS_ENTRY,&(p->tss));/*在gdt中设置新任务的tss和ldt*/	set_ldt_desc(gdt+(nr<<1)+FIRST_LDT_ENTRY,&(p->ldt));	p->state = TASK_RUNNING;/*状态置为可运行*/	/* do this last, just in case */	return last_pid;/*返回新进程的进程号*/}int find_empty_process(void)/*找新进程号*/{	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) goto repeat;	for(i=1 ; i<NR_TASKS ; i++)		if (!task[i])			return i;	return -EAGAIN;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -