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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

进程间通信(二)

發布時間:2024/4/11 编程问答 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 进程间通信(二) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

進程間通信(二)

文章目錄

  • 進程間通信(二)
    • 一、管道的讀寫規則
    • 二、命名管道
    • 三、命名管道的寫入規則
    • 四、舉例

一、管道的讀寫規則

  • 1.當管道沒有數據可以讀取時:

  • O_NONBLOCK disable :read調用阻塞,即進程暫停執行,一直等到有數據到來位置

  • O_NONBLOCK enable :read調用返回-1,error為EAGIN

  • 2.當管道滿的時候,往管道中寫數據:

  • O_NONBLOCK disable :write調用會阻塞,直到有進程讀取數據

  • O_NONBLOCK enable : write調用返回-1, error為EAGIN

  • 3.如果所有管道寫端對應的文件描述符被關閉,則read返回0;

  • 4.如果所有管道的讀端對應的文件描述符都被關閉,則write操作會產生信號SIGPIPE,進而可能導致write進程退出

  • 5.當要寫入的數據量不大于PIPE_BUF時,linux保證寫入的原子性

  • 6.當要寫入的數據量大于PIPE_BUF時,Linux將不再保證寫入的原子性。

  • 7.管道的特點:

  • 只能用于具有共同祖先的進程(具有親緣關系的進程)之間的通信,通常一個管道有一個進程創建,然后該進程調用fork函數,這樣父子進程都可以使用該管道,就可以完成父子進程間的通信

  • 管道提供流式的服務

  • 一般而言,進程退出,管道釋放,所以管道的生命周期隨進程。

  • 注意,所有引用管道的進程都退出,管道才能釋放

  • 一般而言,內核會對管道操作進行同步與互斥

  • 管道是半雙工的,數據只能單方向流動;需要雙向流動時,需要建立兩個管道

二、命名管道

  • 1.命名管道的引入

  • 根據上面可以得知管道的一個限制條件是只能用于具有親緣關系的進程之間進行通信。

  • 如果我們想在不相關的進程間進行通信,進行數據交換,可以使用FIFO文件完成,即命名管道。

  • 命名管道是一種特殊的管道文件(P)。

  • 2.創建命名管道

  • a.程序內部創建:

int mkfifo(const char *pathname, mode_t mode);
  • b.命令行創建:
mkfifo filename
  • 3.命名管道與匿名管道的區別
命名管道匿名管道
可以有命令行和mkfifo函數創建,打開用open由pipe函數創建
適用于同主機上的任意兩個進程間的通信只適用于具有親緣關系的進程之間進行通信

命名管道和匿名管道的主要區別還是在于它們的創建和打開方式不同,一旦這些工作完成,那么它們之間具有相同的語義。

三、命名管道的寫入規則

  • 1.如果當前打開操作是為讀而打開FIFO時

  • O_NONBLOCK disable : 阻塞到有相應進程為寫而打開FIFO

  • O_NONBLOCK enable : 立即返回成功

  • 2.如果當前打開操作是為寫而打開FIFO時

  • O_NONBLOCK disable :阻塞到有相應進程為讀而打開FIFO

  • O_NONBLOCK enable : 立即返回失敗,錯誤碼為ENXIO

四、舉例

  • 1.用命名管道實現文件的拷貝:
    把文件的內容寫入到管道
#include <stdio.h> #include <stdlib.h> #include <fcntl.h> #include <unistd.h> #include <sys/types.h> #include <sys/stat.h> #include <errno.h> #include <string.h>#define ERR_EXIT(m)\do\ {\perror(m);\exit(EXIT_FAILURE);\ }while(0)int main() {//讀取文件,寫入命名管道mkfifo("tp",0664);int infd = open("abc", O_RDONLY);if(infd == -1){//ERR_EXIT("open");perror("open is error");exit(0);}int outfd = open("tp",O_WRONLY);if(outfd == -1){//ERR_EXIT("open");perror("open is error");exit(1);}char buf[10];int n = read(infd,buf,10);if(n > 0 ) {write(outfd,buf,n);}close(infd);close(outfd);return 0; }

把管道的內容寫到文件

#include <stdio.h> #include <stdlib.h> #include <fcntl.h> #include <unistd.h> #include <sys/types.h> #include <sys/stat.h> #include <errno.h> #include <string.h>#define ERR_EXIT(m)\do\ {\perror(m);\exit(EXIT_FAILURE);\ }while(0)int main() {//讀取管道,寫入目標文件int outfd = open("abc.back",O_WRONLY | O_TRUNC,0664);if(outfd == -1){perror("open is error");exit(1);}int infd = open("tp",O_RDONLY);if(infd == -1){perror("open is error");exit(1);}char buf[10];int n = read(infd,buf,10);if(n >0){write(outfd,buf,n);}close(infd);close(outfd);unlink("tp");return 0; }
  • 2.用命名管道實現server&client通信
    server端:
#include <stdio.h> #include <unistd.h> #include <stdlib.h> #include <fcntl.h> #include <sys/stat.h> #include <sys/types.h> #include <string.h>int main() {umask(0);if(mkfifo("mypipe",0664) < 0){perror("make fifo is error");exit(1);}int rfd = open("mypipe",O_RDONLY);if(rfd < 0){perror("open pipe is perror");exit(1);}char buf[1024];while(1){buf[0] = 0;printf("please wait ...!\n");ssize_t s = read(rfd,buf,sizeof(buf)-1);if(s > 0){buf[s-1] = 0;printf("client say: %s",buf);}else if (s == 0){printf("client is quit!\n");exit(1);}else{perror("read is orrer");exit(1);}}close(rfd);return 0; }

client端:

#include <stdio.h> #include <unistd.h> #include <stdlib.h> #include <sys/stat.h> #include <sys/types.h> #include <string.h> #include <fcntl.h>int main() {int wfd = open("mypipe",O_WRONLY);if(wfd < 0){perror("open pipe is error");exit(1);}char buf[1024];while(1){buf[0] = 0;printf("Please Enter!\n");fflush(stdout);ssize_t s = read(0,buf,sizeof(buf)-1);if(s <= 0){perror("read is error");continue;}else if(s > 0){buf[s] = 0;write(wfd,buf,sizeof(buf));}}close(wfd);return 0; }

總結

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

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