精品丰满熟女一区二区三区_五月天亚洲欧美综合网_亚洲青青青在线观看_国产一区二区精选

  • <menu id="29e66"></menu>

    <bdo id="29e66"><mark id="29e66"><legend id="29e66"></legend></mark></bdo>

  • <pre id="29e66"><tt id="29e66"><rt id="29e66"></rt></tt></pre>

      <label id="29e66"></label><address id="29e66"><mark id="29e66"><strike id="29e66"></strike></mark></address>
      學(xué)習(xí)啦 > 學(xué)習(xí)電腦 > 操作系統(tǒng) > Linux教程 > Linux建立pipe管道函數(shù)的方法

      Linux建立pipe管道函數(shù)的方法

      時間: 孫勝652 分享

      Linux建立pipe管道函數(shù)的方法

        pipe是Linux系統(tǒng)中的管道,管道機(jī)制的主體是pipe函數(shù)的調(diào)用,那么Linux系統(tǒng)要如何建立pipe函數(shù)呢?下面學(xué)習(xí)啦小編就給大家介紹下Linux建立pipe函數(shù)的方法,一起來了解下吧。

        ● 無名管道

        主要用于父進(jìn)程與子進(jìn)程之間,或者兩個兄弟進(jìn)程之間。在linux系統(tǒng)中可以通過系統(tǒng)調(diào)用建立起一個單向的通信管道,且這種關(guān)系只能由父進(jìn)程來建立。因此,每個管道都是單向的,當(dāng)需要雙向通信時就需要建立起兩個管道。管道兩端的進(jìn)程均將該管道看做一個文件,一個進(jìn)程負(fù)責(zé)往管道中寫內(nèi)容,而另一個從管道中讀取。這種傳輸遵循“先入先出”(FIFO)的規(guī)則。

        ● 命名管道

        命名管道是為了解決無名管道只能用于近親進(jìn)程之間通信的缺陷而設(shè)計的。命名管道是建立在實際的磁盤介質(zhì)或文件系統(tǒng)(而不是只存在于內(nèi)存中)上有自己名字的文件,任何進(jìn)程可以在任何時間通過文件名或路徑名與該文件建立聯(lián)系。為了實現(xiàn)命名管道,引入了一種新的文件類型——FIFO文件(遵循先進(jìn)先出的原則)。實現(xiàn)一個命名管道實際上就是實現(xiàn)一個FIFO文件。命名管道一旦建立,之后它的讀、寫以及關(guān)閉操作都與普通管道完全相同。雖然FIFO文件的inode節(jié)點在磁盤上,但是僅是一個節(jié)點而已,文件的數(shù)據(jù)還是存在于內(nèi)存緩沖頁面中,和普通管道相同。

        1. 函數(shù)說明

        pipe(建立管道):

        1) 頭文件 #include《unistd.h》

        2) 定義函數(shù): int pipe(int filedes[2]);

        3) 函數(shù)說明: pipe()會建立管道,并將文件描述詞由參數(shù)filedes數(shù)組返回。

        filedes[0]為管道里的讀取端

        filedes[1]則為管道的寫入端。

        4) 返回值: 若成功則返回零,否則返回-1,錯誤原因存于errno中。

        錯誤代碼:

        EMFILE 進(jìn)程已用完文件描述詞最大量

        ENFILE 系統(tǒng)已無文件描述詞可用。

        EFAULT 參數(shù) filedes 數(shù)組地址不合法。

        2. 舉例

        [cpp] view plaincopy

        #include 《unistd.h》

        #include 《stdio.h》

        int main( void )

        {

        int filedes[2];

        char buf[80];

        pid_t pid;

        pipe( filedes );

        pid=fork();

        if (pid 》 0)

        {

        printf( “This is in the father process,here write a string to the pipe./n” );

        char s[] = “Hello world , this is write by pipe./n”;

        write( filedes[1], s, sizeof(s) );

        close( filedes[0] );

        close( filedes[1] );

        }

        else if(pid == 0)

        {

        printf( “This is in the child process,here read a string from the pipe./n” );

        read( filedes[0], buf, sizeof(buf) );

        printf( “%s/n”, buf );

        close( filedes[0] );

        close( filedes[1] );

        }

        waitpid( pid, NULL, 0 );

        return 0;

        }

        運行結(jié)果:

        [root@localhost src]# gcc pipe.c

        [root@localhost src]# 。/a.out

        This is in the child process,here read a string from the pipe.

        This is in the father process,here write a string to the pipe.

        Hello world , this is write by pipe.

        當(dāng)管道中的數(shù)據(jù)被讀取后,管道為空。一個隨后的read()調(diào)用將默認(rèn)的被阻塞,等待某些數(shù)據(jù)寫入。

        若需要設(shè)置為非阻塞,則可做如下設(shè)置:

        fcntl(filedes[0], F_SETFL, O_NONBLOCK);

        fcntl(filedes[1], F_SETFL, O_NONBLOCK);

        上面就是Linux建立pipe管道函數(shù)的方法介紹了,需要注意的是,pipe函數(shù)需要和fork()配合使用,否則起不到進(jìn)程間通信的作用。

      316074