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

首頁 > 學院 > 開發設計 > 正文

unix網絡編程卷2:管道和FIFO

2019-11-09 20:26:40
字體:
來源:轉載
供稿:網友

管道和FIFO

管道(Pipe):管道可用于具有親緣關系進程間的通信,允許一個進程和另一個與它有共同祖先的進程之間進行通信。命名管道(named pipe):命名管道克服了管道沒有名字的限制,因此,除具有管道所具有的功能外,它還允許無親緣關系進程間的通信。命名管道在文件系統中有對應的文件名。命名管道通過命令mkfifo或系統調用mkfifo來創建。#include <unistd.h>int pipe(int fd[2])

該函數返回兩個文件描述符 fd[[0]和fdp[1],前者用于讀,后者用于寫。

#include <iostream>#include <stdio.h>#include <stdlib.h>#include <unistd.h>#include <sys/types.h>#include <sys/wait.h>#include <fcntl.h>#include <string.h>void client(int readfd, int writefd);void server(int readfd, int writefd);int main(int argc,char **argv){ int pipe1[2], pipe2[2]; int childpid; pipe(pipe1); pipe(pipe2); if( (childpid = fork()) == 0) //child { close(pipe1[1]); close(pipe2[0]); server(pipe1[0],pipe2[1]); exit(0); } close(pipe1[0]); close(pipe2[1]); client(pipe2[0],pipe1[1]); waitpid(childpid, NULL, 0); exit(0);}void server(int readfd, int writefd){ int fd; size_t n; char buff[4097]; if( (n = read(readfd, buff, 4096)) == 0) { std::cout<< "read file error " << std::endl; return; } buff[n] = '/0'; std::cout << "the server get filename from client:" << buff << std::endl; if( (fd = open(buff, O_RDONLY)) < 0) { std::cout << "the file file can't open file" << std::endl; write(writefd, buff , n); } else { while( (n = read(fd, buff, 4096)) > 0) { std::cout << "the server read file:" << buff; write(writefd, buff , n); } close(fd); }}void client(int readfd, int writefd){ size_t len; size_t n; char buff[4097]; std::cout<< "pls input filename" << std::endl; fgets(buff, 4096, stdin); len = strlen(buff); if( buff[len-1] == '/n') len--; write(writefd, buff, len); while( (n = read(readfd, buff ,4096)) > 0) { std::cout << "the client read message from server:" <<buff; }}

有一個test文件 文件有一行數據this is a test 這里寫圖片描述


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 鄂托克旗| 石泉县| 南充市| 象州县| 彭州市| 晋江市| 新巴尔虎左旗| 通渭县| 威远县| 山丹县| 灵武市| 延津县| 邵东县| 合山市| 白玉县| 特克斯县| 准格尔旗| 安化县| 新巴尔虎左旗| 武义县| 调兵山市| 澎湖县| 平江县| 孝感市| 乌兰浩特市| 迁安市| 太康县| 神池县| 甘谷县| 连山| 岱山县| 七台河市| 衡阳市| 福鼎市| 承德市| 元谋县| 潜江市| 贺州市| 湾仔区| 梨树县| 闸北区|