zoukankan      html  css  js  c++  java
  • IPC_管道

    1.管道特点:

      1)单向数据通信

      2)匿名管道-常用于(父子进程/有血缘关系的进程之间)

      3)命名管道-常用于(无血缘关系进程之间通信)

      4)提供一种流式服务(发送和接受不接受字节数的大小,可取任意大小)

      5)生命周期,随进程(进程退出,管道消失)

      6)提供同步与互斥功能,保证数据传输的正确性

    2.利用管道进行进程间通信

    //.client
    int
    main() 8 { 9 int fd = open("./mypipe",O_WRONLY); 10 if(fd<0){ 11 perror("open"); 12 return 1; 13 } 14 char buf[1024]; 15 while(1){ 16 printf("Please Enter## "); 17 fflush(stdout); 18 ssize_t s=read(0,buf,sizeof(buf)-1); 19 if(s>0){ 20 buf[s]=0; 21 write(fd,buf,strlen(buf)); 22 } 23 } 24 close(fd); 25 return 0; 26 }
    //server.c
    7
    int main() 8 { 9 umask(0); 10 if(mkfifo("./mypipe",S_IFIFO | 0666)<0){ 11 perror("myfifo"); 12 return 1; 13 } 14 int fd = open("./mypipe",O_RDONLY); 15 if(fd<0){ 16 perror("open"); 17 return 2; 18 } 19 char buf[1024]; 20 while(1){ 21 ssize_t s=read(fd,buf,sizeof(buf)-1); 22 if(s>0){ 23 buf[s]=0; 24 printf("client# %s",buf); 25 } 26 else if(0==s){ 27 printf("client is quit!,server begin quit! "); 28 break; 29 } 30 else{ 31 perror("read"); 32 return 3; 33 } 34 } 35 close(fd); 36 return 0; 37 }

    3管道容量的大小

    安心下来做技术,笑是最大的福气
  • 相关阅读:
    Git分支管理策略
    嵌入式文件系统构建工具 busybox / buildroot / openwrt
    nodejs与c语言交互应用实例
    python与c语言交互应用实例
    websocket programming base on nodejs
    Using Bluetooth LE with Go
    nodejs
    linux ipc/its
    SAMA5D3 Xplained Board
    BlueZ
  • 原文地址:https://www.cnblogs.com/JN-PDD/p/6905956.html
Copyright © 2011-2022 走看看