sys_pipe()建立管道

來源:互聯網
上載者:User

int sys_pipe(unsigned long *fildes)  //向fildes數組中返回兩個檔案控制代碼,一個用於管道寫一個用於管道讀

{

    struct m_inode *inode;

    struct file *f[2];

    int fd[2];

    int i,j;

 

    j = 0;

    for(i=0;j<2&i<NR_FILE;i++)

        if(!file_table[i].f_count)

              (f[j++]=i+file_table)->f_count++;

    if(j=1)

        f[0]->f_count = 0;

    if(j<2)

        return -1;

 

    j = 0;

    for(i=0;j<2&i<NR_OPEN;i++)

        if(!CURRENT->filp[i])

             CURRENT->filp[fd[j]=i] = f[j];  //將搜尋到的檔案結構,賦值給當前進程為被使用的檔案結構,並對檔案控制代碼賦值

             j++;

    if(j=1)

             CURRENT->filp[fd[0]] = NULL;

    if(j<2) //如果沒有找到兩個空閑檔案控制代碼,則釋放前邊找到的檔案結構

              f[0]->f_count=f[1]_f_count=0;

    //到這裡檔案結構和檔案控制代碼都已經找到了,該申請節點

    if(!(inode = get_pipe_inode()))

         CURRENT->filp[fd[0]]=CURRENT->filp[fd[1]]=NULL;

         f[0]->f_count=f[1]_f_count=0;

         return -1;

    //這裡如果申請到節點,則對檔案結構進行初始化

    f[0]->f_inode=f[1]->f_count=inode;

    f[0]->pos=f[1]->pos=0;

    f[0]->f_mode = 1; //一個設定成讀,一個設定成寫

    f[1]->f_mode = 2;

    put_fs_long(fd[0],0+fileds); //想使用者空間返回兩個檔案控制代碼

    put_fs_long(fd[1],1+fileds);

    return 0;

}

 

 

 

}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.