国产成人精品三级麻豆,色综合天天综合高清网,亚洲精品夜夜夜,国产成人综合在线女婷五月99播放,色婷婷色综合激情国产日韩


linux有名管道通信實驗

分享到:
           

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

    1.實驗目的

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

    2.實驗內(nèi)容

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

    3.實驗步驟

    (1)畫出流程圖。該實驗流程圖如圖1所示。


圖1 實驗流程圖

    (2)編寫代碼。該實驗源代碼如下:

    /* 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 /* 多路復用輸入文件數(shù)目 */
    #define TIME_DELAY 60 /* 超時值秒數(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)建兩個有名管道 */
        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);
            }
        }

        /* 以只讀非阻塞方式打開兩個管道文件 */
        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;
        }

        /* 取出兩個文件描述符中的較大者 */
        maxfd = MAX(MAX(fds[0], fds[1]), fds[2]);
        /* 初始化讀集inset,并在讀文件描述符集中加入相應的描述集 */
        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)測試該文件描述符是否準備就緒,并調(diào)用select()函數(shù)對相關(guān)文件描述符做相應操作* /
        while(FD_ISSET(fds[0],&inset) || FD_ISSET(fds[1],&inset) || FD_ISSET(fds[2],
        &inset))
        {
            /* 文件描述符集的備份,以免每次都進行初始化 */
            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)編譯并運行該程序。

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

    4.實驗結(jié)果

    實驗運行結(jié)果如下:

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

    $ cat > in1
    SELECT CALL
    TEST PROGRAMME
    END

    $ cat > in2
    select call
    test programme
    end

    本文選自華清遠見嵌入式培訓教材《從實踐中學嵌入式Linux應用程序開發(fā)》

   熱點鏈接:

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

更多新聞>>