天天看點

Linux C程式設計--fork()詳解

在Linux系統下學習一個系統函數最好的方法就是閱讀其源碼,首先,給出fork函數的源碼

/*
*  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;
}
           

以下給出說明:

 fork函數
#include <sys/types.h>       
#include <unistd.h>  pid_t fork(void);      

fork

調用失敗則傳回-1,調用成功的傳回值見下面的解釋。我們通過一個例子來了解

fork

是怎樣建立新程序的。

這個程式的運作過程如下圖所示。

Linux C程式設計--fork()詳解
  1. 父程序初始化。
  2. 父程序調用

    fork

    ,這是一個系統調用,是以進入核心。
  3. 核心根據父程序複制出一個子程序,父程序和子程序的PCB資訊相同,使用者态代碼和資料也相同。是以,子程序現在的狀态看起來和父程序一樣,做完了初始化,剛調用了

    fork

    進入核心,還沒有從核心傳回。
  4. 現在有兩個一模一樣的程序看起來都調用了

    fork

    進入核心等待從核心傳回(實際上

    fork

    隻調用了一次),此外系統中還有很多别的程序也等待從核心傳回。是父程序先傳回還是子程序先傳回,還是這兩個程序都等待,先去排程執行别的程序,這都不一定,取決于核心的排程算法。
  5. 如果某個時刻父程序被排程執行了,從核心傳回後就從

    fork

    函數傳回,儲存在變量

    pid

    中的傳回值是子程序的id,是一個大于0的整數,是以執下面的

    else

    分支,然後執行

    for

    循環,列印

    "This is the parent\n"

    三次之後終止。
  6. 如果某個時刻子程序被排程執行了,從核心傳回後就從

    fork

    函數傳回,儲存在變量

    pid

    中的傳回值是0,是以執行下面的

    if (pid == 0)

    分支,然後執行

    for

    循環,列印

    "This is the child\n"

    六次之後終止。

    fork

    調用把父程序的資料複制一份給子程序,但此後二者互不影響,在這個例子中,

    fork

    調用之後父程序和子程序的變量

    message

    n

    被賦予不同的值,互不影響。
  7. 父程序每列印一條消息就睡眠1秒,這時核心排程别的程序執行,在1秒這麼長的間隙裡(對于計算機來說1秒很長了)子程序很有可能被排程到。同樣地,子程序每列印一條消息就睡眠1秒,在這1秒期間父程序也很有可能被排程到。是以程式運作的結果基本上是父子程序交替列印,但這也不是一定的,取決于系統中其它程序的運作情況和核心的排程算法,如果系統中其它程序非常繁忙則有可能觀察到不同的結果。另外,讀者也可以把

    sleep(1);

    去掉看程式的運作結果如何。
  8. 這個程式是在Shell下運作的,是以Shell程序是父程序的父程序。父程序運作時Shell程序處于等待狀态,當父程序終止時Shell程序認為指令執行結束了,于是列印Shell提示符,而事實上子程序這時還沒結束,是以子程序的消息列印到了Shell提示符後面。最後光标停在

    This is the child

    的下一行,這時使用者仍然可以敲指令,即使指令不是緊跟在提示符後面,Shell也能正确讀取。

fork

函數的特點概括起來就是“調用一次,傳回兩次”,在父程序中調用一次,在父程序和子程序中各傳回一次。從上圖可以看出,一開始是一個控制流程,調用

fork

之後發生了分叉,變成兩個控制流程,這也就是“fork”(分叉)這個名字的由來了。子程序中

fork

的傳回值是0,而父程序中

fork

的傳回值則是子程序的id(從根本上說

fork

是從核心傳回的,核心自有辦法讓父程序和子程序傳回不同的值),這樣當

fork

函數傳回後,程式員可以根據傳回值的不同讓父程序和子程序執行不同的代碼。

fork

的傳回值這樣規定是有道理的。

fork

在子程序中傳回0,子程序仍可以調用

getpid

函數得到自己的程序id,也可以調用

getppid

函數得到父程序的id。在父程序中用

getpid

可以得到自己的程序id,然而要想得到子程序的id,隻有将

fork

的傳回值記錄下來,别無它法。

fork

的另一個特性是所有由父程序打開的描述符都被複制到子程序中。父、子程序中相同編号的檔案描述符在核心中指向同一個

file

結構體,也就是說,

file

結構體的引用計數要增加。

繼續閱讀