gogo专业大尺度亚洲高清人体,美女张开双腿让男生桶,亚洲av无码一区二区三区鸳鸯影院,久久久久国产精品人妻


linux有名管道通信實(shí)驗(yàn)

分享到:
           

    本文關(guān)鍵字: 有名管道,linux有名管道

    1.實(shí)驗(yàn)?zāi)康?/p>

    通過(guò)編寫(xiě)有名管道多路通信實(shí)驗(yàn),進(jìn)一步掌握管道的創(chuàng)建、讀寫(xiě)等操作,同時(shí)復(fù)習(xí)使用select()函數(shù)實(shí)現(xiàn)管道的通信。

    2.實(shí)驗(yàn)內(nèi)容

    這里采用管道函數(shù)創(chuàng)建有名管道(并不是在控制臺(tái)下輸入命令),而且使用select()函數(shù)替代poll()函數(shù)實(shí)現(xiàn)多路復(fù)用(使用select()函數(shù)是出于以演示為目的)。

    3.實(shí)驗(yàn)步驟

    (1)畫(huà)出流程圖。該實(shí)驗(yàn)流程圖如圖1所示。


圖1 實(shí)驗(yàn)流程圖

    (2)編寫(xiě)代碼。該實(shí)驗(yàn)源代碼如下:

    /* pipe_select.c */
    #include <fcntl.h>
    #include <stdio.h>
    #include <unistd.h>
    #include <stdlib.h>
    #include <string.h>
    #include <time.h>
    #include <errno.h>

    #define FIFO1 "in1"
    #define FIFO2 "in2"
    #define MAX_BUFFER_SIZE 1024 /* 緩沖區(qū)大小 */
    #define IN_FILES 3 /* 多路復(fù)用輸入文件數(shù)目 */
    #define TIME_DELAY 60 /* 超時(shí)值秒數(shù) */
    #define MAX(a, b) ((a > b)?(a):(b))

    int main(void)
    {
        int fds[IN_FILES];
        char buf[MAX_BUFFER_SIZE];
        int i, res, real_read, maxfd;
        struct timeval tv;
        fd_set inset,tmp_inset;

        fds[0] = 0;

        /* 創(chuàng)建兩個(gè)有名管道 */
        if (access(FIFO1, F_OK) == -1)
        {
            if ((mkfifo(FIFO1, 0666) < 0) && (errno != EEXIST))
            {
                printf("Cannot create fifo file\n");
                exit(1);
            }
        }
        if (access(FIFO2, F_OK) == -1)
        {
            if ((mkfifo(FIFO2, 0666) < 0) && (errno != EEXIST))
            {
                printf("Cannot create fifo file\n");
                exit(1);
            }
        }

        /* 以只讀非阻塞方式打開(kāi)兩個(gè)管道文件 */
        if((fds[1] = open (FIFO1, O_RDONLY|O_NONBLOCK)) < 0)
        {
            printf("Open in1 error\n");
            return 1;
        }
        if((fds[2] = open (FIFO2, O_RDONLY|O_NONBLOCK)) < 0)
        {
            printf("Open in2 error\n");
            return 1;
        }

        /* 取出兩個(gè)文件描述符中的較大者 */
        maxfd = MAX(MAX(fds[0], fds[1]), fds[2]);
        /* 初始化讀集inset,并在讀文件描述符集中加入相應(yīng)的描述集 */
        FD_ZERO(&inset);
        for (i = 0; i < IN_FILES; i++)
        {
            FD_SET(fds[i], &inset);
        }
        FD_SET(0, &inset);

        tv.tv_sec = TIME_DELAY;
        tv.tv_usec = 0;
        /* 循環(huán)測(cè)試該文件描述符是否準(zhǔn)備就緒,并調(diào)用select()函數(shù)對(duì)相關(guān)文件描述符做相應(yīng)操作* /
        while(FD_ISSET(fds[0],&inset) || FD_ISSET(fds[1],&inset) || FD_ISSET(fds[2],
        &inset))
        {
            /* 文件描述符集的備份,以免每次都進(jìn)行初始化 */
            tmp_inset = inset;
            res = select(maxfd + 1, &tmp_inset, NULL, NULL, &tv);
            switch(res)
            {
                case -1:
                {
                    printf("Select error\n");
                    return 1;
                }
                break;
                case 0: /* Timeout */
                {
                    printf("Time out\n");
                    return 1;
                }
                break;
                default:
                {
                    for (i = 0; i < IN_FILES; i++)
                    {
                        if (FD_ISSET(fds[i], &tmp_inset))
                        {
                            memset(buf, 0, MAX_BUFFER_SIZE);
                            real_read = read(fds[i], buf, MAX_BUFFER_SIZE);
                            if (real_read < 0)
                            {
                                if (errno != EAGAIN)
                                {
                                    return 1;
                                }
                            }
                            else if (!real_read)
                            {
                                close(fds[i]);
                                FD_CLR(fds[i], &inset);
                            }
                            else
                            {
                                if (i == 0)
                                { /* 主程序終端控制 */
                                    if ((buf[0] == 'q') || (buf[0] == 'Q'))
                                    {
                                        return 1;
                                    }
                                }
                                else
                                { /* 顯示管道輸入字符串 */
                                    buf[real_read] = '\0';
                                    printf("%s", buf);
                                }
                            }
                        } /* end of if */
                    } /* end of for */
                }
                break;
            } /* end of switch */
        } /* end of while */
        return 0;
    }

    (3)編譯并運(yùn)行該程序。

    (4)另外打開(kāi)兩個(gè)虛擬終端,分別輸入“cat > in1”和“cat > in2”,接著在該管道中輸入相關(guān)內(nèi)容,并觀(guān)察實(shí)驗(yàn)結(jié)果。

    4.實(shí)驗(yàn)結(jié)果

    實(shí)驗(yàn)運(yùn)行結(jié)果如下:

    $ ./pipe_select (必須先運(yùn)行主程序)
    SELECT CALL
    select call
    TEST PROGRAMME
    test programme
    END
    end
    q /* 在終端上輸入“q”或“Q”立刻結(jié)束程序運(yùn)行 */

    $ cat > in1
    SELECT CALL
    TEST PROGRAMME
    END

    $ cat > in2
    select call
    test programme
    end

    本文選自華清遠(yuǎn)見(jiàn)嵌入式培訓(xùn)教材《從實(shí)踐中學(xué)嵌入式Linux應(yīng)用程序開(kāi)發(fā)》

   熱點(diǎn)鏈接:

   1、有名管道(FIFO)
   2、linux 消息隊(duì)列
   3、linux 共享內(nèi)存
   4、linux下的信號(hào)量
   5、linux下的信號(hào)處理實(shí)例

更多新聞>>