国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > Linux Shell > 正文

linux C語言開發管道通信實例詳解

2020-07-27 18:58:52
字體:
來源:轉載
供稿:網友

linux C語言開發管道通信

   Linux系統本身為進程間通信提供了很多的方式,比如說管道、共享內存、socket通信等。管道的使用十分簡單,在創建了匿名管道之后,我們只需要從一個管道發送數據,再從另外一個管道接受數據即可。

#include <stdio.h> #include <unistd.h> #include <stdlib.h> #include <string.h>  int pipe_default[2];  int main() {  pid_t pid;  char buffer[32];   memset(buffer, 0, 32);  if(pipe(pipe_default) < 0)  {   printf("Failed to create pipe!/n");   return 0;  }   if(0 == (pid = fork()))  {   close(pipe_default[1]);   sleep(5);   if(read(pipe_default[0], buffer, 32) > 0)   {    printf("Receive data from server, %s!/n", buffer);   }   close(pipe_default[0]);  }  else  {   close(pipe_default[0]);   if(-1 != write(pipe_default[1], "hello", strlen("hello")))   {    printf("Send data to client, hello!/n");   }   close(pipe_default[1]);   waitpid(pid, NULL, 0);  }   return 1; } 

    下面我們就可以開始編譯運行了,老規矩分成兩步驟進行:(1)輸入gcc pipe.c -o pipe;(2)然后輸入./pipe,過一會兒你就可以看到下面的打印了。

[test@localhost pipe]$ ./pipe Send data to client, hello! Receive data from server, hello! 

感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 阿克苏市| 昌图县| 安平县| 牟定县| 天水市| 临泽县| 正宁县| 大厂| 海兴县| 富锦市| 香河县| 南丰县| 沂南县| 新乐市| 运城市| 河北省| 台山市| 灵石县| 天水市| 宜宾县| 共和县| 长寿区| 卓资县| 平湖市| 沙湾县| 汾阳市| 镇江市| 广元市| 泽普县| 白朗县| 大荔县| 琼海市| 贵港市| 电白县| 东乡族自治县| 三台县| 东丰县| 台山市| 雷山县| 乌拉特后旗| 启东市|