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

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 运维知识 > linux >内容正文

linux

linux网络编程九:splice函数,高效的零拷贝

發(fā)布時間:2023/11/30 linux 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 linux网络编程九:splice函数,高效的零拷贝 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

1. splice函數(shù)

??? #include <fcntl.h>
??? ssize_t splice(int fd_in, loff_t *off_in, int fd_out, loff_t *off_out, size_t len, unsigned int flags);


splice用于在兩個文件描述符之間移動數(shù)據(jù), 也是零拷貝。

fd_in參數(shù)是待輸入描述符。如果它是一個管道文件描述符,則off_in必須設(shè)置為NULL;否則off_in表示從輸入數(shù)據(jù)流的何處開始讀取,此時若為NULL,則從輸入數(shù)據(jù)流的當(dāng)前偏移位置讀入。

fd_out/off_out與上述相同,不過是用于輸出。

len參數(shù)指定移動數(shù)據(jù)的長度。

flags參數(shù)則控制數(shù)據(jù)如何移動:

??? SPLICE_F_NONBLOCK:splice 操作不會被阻塞。然而,如果文件描述符沒有被設(shè)置為不可被阻塞方式的 I/O ,那么調(diào)用 splice 有可能仍然被阻塞。
??? SPLICE_F_MORE:告知操作系統(tǒng)內(nèi)核下一個 splice 系統(tǒng)調(diào)用將會有更多的數(shù)據(jù)傳來。
??? SPLICE_F_MOVE:如果輸出是文件,這個值則會使得操作系統(tǒng)內(nèi)核嘗試從輸入管道緩沖區(qū)直接將數(shù)據(jù)讀入到輸出地址空間,這個數(shù)據(jù)傳輸過程沒有任何數(shù)據(jù)拷貝操作發(fā)生。

2. 使用splice時, fd_in和fd_out中必須至少有一個是管道文件描述符。

調(diào)用成功時返回移動的字節(jié)數(shù)量;它可能返回0,表示沒有數(shù)據(jù)需要移動,這通常發(fā)生在從管道中讀數(shù)據(jù)時而該管道沒有被寫入的時候。

失敗時返回-1,并設(shè)置errno


3. 代碼:通過splice將客戶端的內(nèi)容讀入到管道中, 再從管道中讀出到客戶端,從而實現(xiàn)高效簡單的回顯服務(wù)。整個過程未執(zhí)行recv/send,因此也未涉及用戶空間到內(nèi)核空間的數(shù)據(jù)拷貝。

??? //使用splice實現(xiàn)的回顯服務(wù)器
??? #include <stdio.h>
??? #include <stdlib.h>
??? #include <unistd.h>
??? #include <sys/socket.h>
??? #include <netinet/in.h>
??? #include <arpa/inet.h>
??? #include <assert.h>
??? #include <errno.h>
??? #include <string.h>
??? #include <fcntl.h>
??? ?
??? ?
??? int main(int argc, char **argv)
??? {
??? ?
?? ??? ?if (argc <= 2) {
?? ??? ??? ?printf("usage: %s ip port\n", basename(argv[0]));
?? ??? ??? ?return 1;
?? ??? ?}
?? ??? ?
?? ??? ?const char *ip = argv[1];
?? ??? ?int port = atoi(argv[2]);
??? ?
?? ??? ?struct sockaddr_in address;
?? ??? ?bzero(&address, sizeof(address));
?? ??? ?address.sin_family = AF_INET;
?? ??? ?address.sin_port = htons(port);
?? ??? ?inet_pton(AF_INET, ip, &address.sin_addr);
??? ?
?? ??? ?int sock = socket(PF_INET, SOCK_STREAM, 0);
?? ??? ?assert(sock >= 0);
?? ??? ?
?? ??? ?int reuse = 1;
?? ??? ?setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse));
??? ?
?? ??? ?int ret = bind(sock, (struct sockaddr*)&address, sizeof(address));
?? ??? ?assert(ret != -1);
??? ?
?? ??? ?ret = listen(sock, 5);
?? ??? ?assert(ret != -1);
?? ??? ?
?? ??? ?struct sockaddr_in client;
?? ??? ?socklen_t client_addrlength = sizeof(client);
?? ??? ?
?? ??? ?int connfd = accept(sock, (struct sockaddr*)&client, &client_addrlength);
?? ??? ?if (connfd < 0) {
?? ??? ??? ?printf("errno is: %s\n", strerror(errno));
?? ??? ?}
?? ??? ?else {
?? ??? ??? ?int pipefd[2];
?? ??? ??? ??? ??? ?
?? ??? ??? ?ret = pipe(pipefd);? //創(chuàng)建管道
?? ??? ??? ?assert(ret != -1);
?? ??? ??? ?
??????????????????? //將connfd上的客戶端數(shù)據(jù)定向到管道中
?? ??? ??? ?ret = splice(connfd, NULL, pipefd[1], NULL,
?? ??? ??? ??? ??? ??? ??? ?32768, SPLICE_F_MORE | SPLICE_F_MOVE);
?? ??? ??? ?assert(ret != -1);
?? ??? ??? ?
??????????????????? //將管道的輸出定向到connfd上
?? ??? ??? ?ret = splice(pipefd[0], NULL, connfd, NULL,
?? ??? ??? ??? ??? ??? ??? ?32768, SPLICE_F_MORE | SPLICE_F_MOVE);
?? ??? ??? ?assert(ret != -1);?? ??? ??? ??? ?
?? ??? ??? ?
?? ??? ??? ?close(connfd);
?? ??? ?}
??? ?
?? ??? ?
?? ??? ?close(sock);
??? ?
??? ?
??? ?
??? ?
?? ??? ?return 0;
??? }
?

總結(jié)

以上是生活随笔為你收集整理的linux网络编程九:splice函数,高效的零拷贝的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。