linux的無名管道的實現

來源:互聯網
上載者:User

在這個之前參考了《Linux環境處理序間通訊》,這篇文章,這裡解釋下為什麼linux的管道只能在父子處理序間通訊,因為父進程首先建立一條管道,在執行fork()函數後,子進程繼承了父進程在核心空間的檔案描敘字,故可通訊,jicama os沒有管道的概念,在將來可能也不會打算支援管道,倒是fifo可能仍然會考慮。這個取自早期的linux版本,程式碼解析如下,如有不對的地方懇請指正:
#include <signal.h>

#include <linux/sched.h>
#include <linux/mm.h> /* for get_free_page */
#include <asm/segment.h>

#define PIPE_HEAD(inode) (((long *)((inode).i_zone))[0])
#define PIPE_TAIL(inode) (((long *)((inode).i_zone))[1])
#define PIPE_SIZE(inode) ((PIPE_HEAD(inode)-PIPE_TAIL(inode))&(PAGE_SIZE-1))
#define PIPE_EMPTY(inode) (PIPE_HEAD(inode)==PIPE_TAIL(inode))
#define PIPE_FULL(inode) (PIPE_SIZE(inode)==(PAGE_SIZE-1))
#define INC_PIPE(head) /
__asm__("incl %0/n/tandl $4095,%0"::"m" (head))

int read_pipe(struct m_inode * inode, char * buf, int count)
{
 char * b=buf;

 while (PIPE_EMPTY(*inode)) {
  //沒有可讀的內容,喚醒寫進程,一個隊列就一個進行一個進程?
  //LINUX為什麼不做成一個隊列形式的呢?
  wake_up(&inode->i_wait);
  //讀+寫,故i_count必須為2
  if (inode->i_count != 2) /* are there any writers left? */
   return 0;
   //當前(讀)進程進入睡眠狀態,等待寫入進程
  sleep_on(&inode->i_wait);
  }
 //直到達到目標的count數,並且剩餘的位元組數字大於0
 while (count>0 && !(PIPE_EMPTY(*inode))) {
  count --;
  //把內容通知給使用者程式
  put_fs_byte(((char *)inode->i_size)[PIPE_TAIL(*inode)],b++);
  //隊尾向後移動
  INC_PIPE( PIPE_TAIL(*inode) );
  }
 //事情辦完了,喚醒等待隊列中的進程
 wake_up(&inode->i_wait);
 return b-buf;
}
 
int write_pipe(struct m_inode * inode, char * buf, int count)
{
 char * b=buf;

 //喚醒讀的進程
 wake_up(&inode->i_wait);
 if (inode->i_count != 2) { /* no readers */
 //沒有讀的進程,向核心發送SIGPIPE故障的signal訊號
  current->signal |= (1<<(SIGPIPE-1));
  return -1;
  }

 //直到buf的內容全部寫完
 while (count-->0) {

  while (PIPE_FULL(*inode)) {
   //如果裡面的內容全部寫滿了,喚醒讀的進程
   wake_up(&inode->i_wait);
   if (inode->i_count != 2) {
    //沒有讀的進程,向核心發送SIGPIPE故障的signal訊號
    current->signal |= (1<<(SIGPIPE-1));
    //返回寫入的位元組數
    return b-buf;
    }
   //當前(寫)進程進入睡眠狀態
   sleep_on(&inode->i_wait);
   }
  //把使用者緩衝的內容寫入到核心
  ((char *)inode->i_size)[PIPE_HEAD(*inode)] = get_fs_byte(b++);
  //隊頭向後移動
  INC_PIPE( PIPE_HEAD(*inode) );
  //喚醒寫進程
  wake_up(&inode->i_wait);
  }
 wake_up(&inode->i_wait);
 return b-buf;
}

struct m_inode * get_pipe_inode(void)
{
 struct m_inode * inode;
 
 //取得一個空的節點項
 if (!(inode = get_empty_inode()))
  return NULL;

 //分配一個頁給核心緩衝使用,用於存放寫入的資料
 //posix標準規定緩衝區的大小高於512位元組即可
 if (!(inode->i_size=get_free_page())) {
  inode->i_count = 0;
  return NULL;
  }
 inode->i_count = 2; /* sum of readers/writers */
 PIPE_HEAD(*inode) = PIPE_TAIL(*inode) = 0;
 inode->i_pipe = 1;
 return inode;
}

int sys_pipe(unsigned long * fildes)
{
 struct m_inode * inode;
 struct file * f[2];
 int fd[2];
 int i,j;

 j=0;
 //嘗試在系統檔案表池(file_table pool)中同時獲得2個檔案描敘項
 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; //沒有2個閒置檔案描敘項可用

 j=0;
 for(i=0;j<2 && i<NR_OPEN;i++)
  if (!current->filp[i]) {
  //把申請到的2個檔案描敘項加入到當前的進程項中
   current->filp[ fd[j]=i ] = f[j];
   j++;
   }

 if (j==1)
  current->filp[fd[0]]=NULL; //當前進程只有一個項可用了,釋放剛才的標記
 if (j<2) {
  //把系統檔案描敘項(file_table pool)中申請的2個節點釋放掉
  f[0]->f_count=f[1]->f_count=0;
  return -1;
  }

//好了,目前我們申請到了2個檔案描敘項,並成功把它放到了當前的進程表中

//申請一個pipe模式的索引節點
 if (!(inode=get_pipe_inode())) {
  //如失敗,則釋放前面申請的資源,這裡我們如果用goto errn語句會否使程式更加簡潔點呢?
  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_inode = inode; //檔案描敘符的索引節點
 f[0]->f_pos = f[1]->f_pos = 0; //檔案讀寫位置
 f[0]->f_mode = 1;  /* 首個檔案描敘符為read 模式*/
 f[1]->f_mode = 2;  /* 第2個檔案描敘符為write模式 */
 put_fs_long(fd[0],0+fildes); //把結果通知給使用者程式
 put_fs_long(fd[1],1+fildes);
 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.