linux vmplice 示例

#define _GNU_SOURCE
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>
#include <fcntl.h>
#include <sys/stat.h>
void check(int res, const char *fucname)
{
    if(res < 0)
    {
        perror(fucname);
        exit(1);
    }
}
int main()
{
    int res;
    int listenfd = socket(AF_INET, SOCK_STREAM, 0);
    struct sockaddr_in local;
    local.sin_family = AF_INET;
    local.sin_addr.s_addr = htonl(INADDR_ANY);
    local.sin_port = htons(5000);
    res = bind(listenfd, (struct sockaddr *)&local, sizeof(local));
    check(res, "bind");
    res = listen(listenfd, 3);
    check(res, "listen");
    int connfd = accept(listenfd, NULL, NULL);
    int pipefd[2] = {0x00};
    pipe(pipefd);
    struct iovec buff;
    buff.iov_base = malloc(4096);
    buff.iov_len = 4096;
    while(1)
    {
        splice(connfd, NULL, pipefd[1], NULL, 4096, SPLICE_F_MOVE | SPLICE_F_MORE);
        res = vmsplice(pipefd[0], &buff, 1, SPLICE_F_GIFT);
        check(res, "splice");
        char *pstr = (char *)buff.iov_base;
        fputs(pstr, stdout);
        memset(buff.iov_base, 0, 4096);
    }
    free(buff.iov_base);
    close(listenfd);
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值