天天看點

一起talk C栗子吧(第九十回:C語言執行個體--使用管道進行程序間通信三)

各位看官們,大家好,上一回中咱們說的是使用管道進行程序間通信的例子,這一回咱們說的例子是:使用管道進行程序間通信,不過使用管道的方式不相同。閑話休提,言歸正轉。讓我們一起talk C栗子吧!

我們在前面章回中介紹了三種管道,這一回我們介紹第三種管道及其使用方法。最主要還是讓大家明白如何使用管道進行程序間的通信。

第三種管道我稱之為真正意義上的管道,該管道還有另外一個名字:命名管道(FIFO)。在介紹它之前,我們先介紹一個函數:mkfifo.

mkfifo函數的原型

int mkfifo(const char *filename, mode_t mode)
           
  • 該函數用來建立一個管道檔案;
  • 該函數的第一個參數是字元串,用來表示管道檔案的名字;
  • 該函數的第二個參數是檔案通路權限,用來表示管道檔案的權限,例如:0764;
  • 該函數會傳回一個檔案描述符,可以通過該檔案描述符來操作管道;
  • 該函數執行成功時傳回0,否則傳回-1。

明白這個函數的用法後,我們接下來介紹命名管道的使用方法.

mkfifo函數的用法

  • 1.使用mkfifo函數建立一個命名管道;
  • 2.在程序A中使用open打開管道(打開方式為寫),這時會得到一個fd;
  • 3.使用write通過fd在管道中寫入資料;
  • 4.使用close關閉步驟2中得到的fd;
  • 5.在程序B中使用open打開管道(打開方式為讀),這時會得到一個fd;
  • 6.使用read通過fd從管道中讀取資料;
  • 7.使用close關閉步驟5中得到的fd;

我們可以看到,程序A在mkfifo建立的管道中寫入資料,程序B從該管道中讀取資料。程序A和B通過該管道實作了程序之間的通信,通信的内容為資料。

我們接下來使用具體的例子進行說明,下面是詳細的代碼:

int main()
{
    char input[] = "IPC by pipe";
    char output[BUFSIZ+];
    char p_name[] = "/tmp/test_fifo";
    int count = ;
    int fd;
    int stat_value;
    pid_t pid,pid_res;

    memset(output,'\0',sizeof(output));

    if(mkfifo(p_name,) == ) // create pipe
    {
        pid = fork();
        if(pid > )
        {
            printf("father running \n");
            fd = open(p_name,O_RDONLY); //open by read mode
            if(fd == -)
            {
                printf("open pipe file failed \n");
                return ;
            }
        }
        else if(pid == )
        {
            printf("son running \n");
            fd = open(p_name,O_WRONLY); //open by write mode
            if(fd == -)
            {
                printf("open pipe file failed \n");
                return ;
            }

            count = write(fd,input,strlen(input));    // write the dato into pipe
            printf("son process write %d characters,they are : %s \n",count,input);
            close(fd);
        }
        else
        {
            printf("create process failed \n");
            return ;
        }
    }
    else
    {
        printf("create fifo failed \n");
        return ;
    }

        pid_res = wait(&stat_value);

        if(pid_res > )
        {
            count = read(fd,output,BUFSIZ);       // read the data from pipe
            printf("father process read %d characters,they are: %s \n",count,output);

            close(fd);
        }

    return ;
}
           

通過上面的代碼,大家可以發現,我們首先建立了一個命名管道,然後用fork建立了子程序,并且在子程序中向管道中寫入資料,接着在父程序中讀取資料,不過父程序使用wait函數等待子程序寫入資料後才去管道中讀取資料。這便是程序之間互斥的應用。如果不這樣做的話,父程序從管道中讀取資料時,子程序還沒有把資料寫入管道。

看官們,正文中就不寫代碼了,詳細的代碼放到了我的資源中,大家可以點選這裡下載下傳使用。

下面是程式的運作結果,請大家參考:

./s                                                       //運作編譯後的程式
father running                                            //父程序在運作
son running                                               //子程序在運作
son process write  characters,they are : IPC by pipe    //子程序向管道中寫入資料
father process read  characters,they are: IPC by pipe   //父程序從管道中讀取資料
           

我們通過上面的程式運作結果可以看到,子程序在管道中寫入了資料“IPC by pipe”,父程序接着從管道中讀取了該資料,進而實作的了父子程序之間的資料傳輸,也就是程序之間的通信。

各位看官,關于使用信号進行程序間通信的例子咱們就說到這裡。欲知後面還有什麼例子,且聽下回分解 。

繼續閱讀