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