linux-fork.c

來源:互聯網
上載者:User
/* *  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 <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;                                //--進程空間地區寫前驗證函式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->tss.back_link = 0;    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);    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)) {        task[nr] = NULL;        free_page((long) p);        return -EAGAIN;    }    for (i=0; i<NR_OPEN;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<NR_TASKS ; i++)            if (task[i] && ((task[i]->pid == last_pid) ||                        (task[i]->pgrp == last_pid)))                goto repeat;    for(i=1 ; i<NR_TASKS ; i++)        if (!task[i])            return i;    return -EAGAIN;}|xGv00|fcc8d4de8197f69fde70263fb4d52380

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.