2024年4月1日发(作者:)
linux源代码-fork.c源代码
/*
* linux/kernel/fork.c
* //--fork()用于创建子进程
* (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
#include
#include
#include
#include
//--写页面验证,若页面不可写,则复制页面
extern void write_verify(unsigned long address);
long last_pid=0;
//--进程空间区域写前验证函数
void verify_area(void * addr,int 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 * TASK_SIZE;
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.
*/ //--fork()子程序,它复制系统进程信息,设置寄存器,
复制数据段(代码段)
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(); //--为新任务数据结构分配
内存
if (!p)
return -EAGAIN;
task[nr] = p;
*p = *current; /* NOTE! this doesn't copy the supervisor stack */
p->state = TASK_UNINTERRUPTIBLE;
p->pid = last_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->_link = 0;
p->0 = PAGE_SIZE + (long) p;
p->0 = 0x10;
p-> = eip;
p-> = eflags;
p-> = 0;
p-> = ecx;
p-> = edx;
p-> = ebx;
p-> = esp;
p-> = ebp;
p-> = esi;
p-> = edi;
p-> = es & 0xffff;
p-> = cs & 0xffff;
p-> = ss & 0xffff;
p-> = ds & 0xffff;
p-> = fs & 0xffff;
p-> = gs & 0xffff;
p-> = _LDT(nr);
p->_bitmap = 0x80000000;
if (last_task_used_math == current)
__asm__("clts ; fnsave %0 ; frstor %0"::"m" (p->tss.i387));
if (copy_mem(nr,p)) {
task[nr] = NULL;
free_page((long) p);
return -EAGAIN;
}
for (i=0; i 对应文件的打开次数增1 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++; if (current->library) current->library->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->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; } int find_empty_process(void) //--为新进程取得不重复的进程号 last_pid { int i; repeat: if ((++last_pid)<0) last_pid=1; for(i=0 ; i if (task[i] && ((task[i]->pid == last_pid) || (task[i]->pgrp == last_pid))) goto repeat; for(i=1 ; i if (!task[i]) return i; return -EAGAIN; }
发布者:admin,转转请注明出处:http://www.yc00.com/web/1711967149a1985942.html
评论列表(0条)