日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > linux >内容正文

linux

Linux 进程通信之FIFO

發布時間:2023/11/30 linux 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Linux 进程通信之FIFO 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
  • FIFO通信(first in first out)
    FIFO 有名管道,實現無血緣關系進程通信。
    ----創建一個管道的偽文件
    a.mkfifo testfifo 命令創建
    b.也可以使用函數int mkfifo(const char *pathname, mode_t mode);
    ----內核會針對fifo文件開辟一個緩沖區,操作fifo文件,可以操作緩沖區,實現進程間通信–實際上就是文件讀寫
    man 3 mkfifo
  • #include <sys/types.h> #include <sys/stat.h> int mkfifo(const char *pathname, mode_t mode);

    注意事項:
    FIFOs
    Opening the read or write end of a FIFO blocks until the other end is also opened (by another process or thread). See
    fifo(7) for further details.
    打開fifo文件時候,read端會阻塞等待write端open,write端同理,也會阻塞等待另外一段打開。

    代碼示例:
    file_w.c 寫端

    #include <stdio.h> #include <stdlib.h> #include <sys/wait.h> #include <sys/types.h> #include <sys/stat.h> #include <unistd.h> #include <fcntl.h> #include <string.h>int main(int argc, char *argv[]) {if(argc != 2) {printf("./a.out filename1\n");return -1;}printf("begin open w\n");int o_ret = open(argv[1], O_WRONLY);printf("end open w\n");char buf[256];int num = 0;while (1) {memset(buf, '\0', sizeof(buf));sprintf(buf, "xiaoming--%d", num++);printf("strlen(buf) = %d\n", strlen(buf));write(o_ret, buf, strlen(buf));sleep(1);}close(o_ret);return 0; }

    file_r.c 讀端

    #include <stdio.h> #include <stdlib.h> #include <sys/wait.h> #include <sys/types.h> #include <sys/stat.h> #include <unistd.h> #include <fcntl.h> #include <string.h>int main(int argc, char *argv[]) {if(argc != 2) {printf("./a.out filename1\n");return -1;}printf("begin open r\n");int o_ret = open(argv[1], O_RDONLY);printf("end open r\n");char buf[256];int num = 0;while (1) {memset(buf, '\0', sizeof(buf));read(o_ret, buf, sizeof(buf));printf("strlen(buf) = %d\n", strlen(buf));printf("read is%s\n", buf);}close(o_ret);return 0; }

    總結

    以上是生活随笔為你收集整理的Linux 进程通信之FIFO的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。