主頁(yè) > 知識(shí)庫(kù) > Linux 進(jìn)程通信之FIFO的實(shí)現(xiàn)

Linux 進(jìn)程通信之FIFO的實(shí)現(xiàn)

熱門標(biāo)簽:濰坊寒亭400電話辦理多少錢 四川保險(xiǎn)智能外呼系統(tǒng) 宜賓銷售外呼系統(tǒng)軟件 高德地圖標(biāo)注公司需要錢 地圖標(biāo)注能更改嗎 廈門防封電銷電話卡 外呼系統(tǒng)全國(guó) 地圖標(biāo)注員有發(fā)展前景嗎 云南電商智能外呼系統(tǒng)哪家好

FIFO通信(first in first out)

FIFO 有名管道,實(shí)現(xiàn)無(wú)血緣關(guān)系進(jìn)程通信。

  • 創(chuàng)建一個(gè)管道的偽文件
    • a.mkfifo testfifo 命令創(chuàng)建
    • b.也可以使用函數(shù)int mkfifo(const char *pathname, mode_t mode);
  • 內(nèi)核會(huì)針對(duì)fifo文件開(kāi)辟一個(gè)緩沖區(qū),操作fifo文件,可以操作緩沖區(qū),實(shí)現(xiàn)進(jìn)程間通信–實(shí)際上就是文件讀寫

man 3 mkfifo

#include <sys/types.h>
#include <sys/stat.h>
int mkfifo(const char *pathname, mode_t mode);

注意事項(xiàng):

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.

打開(kāi)fifo文件時(shí)候,read端會(huì)阻塞等待write端open,write端同理,也會(huì)阻塞等待另外一段打開(kāi)。

代碼示例:
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;
}

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

標(biāo)簽:滁州 回訪 廣安 廊坊 湛江 德州 巴彥淖爾 紅河

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《Linux 進(jìn)程通信之FIFO的實(shí)現(xiàn)》,本文關(guān)鍵詞  Linux,進(jìn)程,通信,之,FIFO,的,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《Linux 進(jìn)程通信之FIFO的實(shí)現(xiàn)》相關(guān)的同類信息!
  • 本頁(yè)收集關(guān)于Linux 進(jìn)程通信之FIFO的實(shí)現(xiàn)的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章