如何在Linux中更改PIPE大小?

时间:2015-09-08 04:34:55

标签: c linux

我试图增加linux管道缓冲区大小。

这是我的代码:

#define _GNU_SOURCE

#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>

#define IO_BUFSIZE 4096

#include<stdio.h> //printf
#include<string.h>    //strlen
#include<sys/socket.h>    //socket
#include<arpa/inet.h> //inet_addr

typedef struct {
        loff_t offset;
        int size;
} msg_header;

int main(int argc , char *argv[])
{

   loff_t in_off = 0;
   loff_t out_off = 0;

   int in_fd = -1; //file id
   int err = -1;
   int splice_bytes = 0;
   int len1 = 0;
   int len2 = 0;
   int len3 = 0;

   in_fd = open("/home/wenji/splice-sample/libpfm-4.5.0.tar", O_RDONLY); //open a file

   int filedes[2];
   struct stat stbuf;

   if(pipe(filedes) < 0)
   {
        perror("pipe error");
   }

   //set the pipe size,
   fcntl(filedes[1], F_SETPIPE_SZ, 1048576);


   if(fstat(in_fd, &stbuf) < 0)
   {
        perror("wrong file id");
   }

   len1 = stbuf.st_size;

   printf("the file size len=%i\n", len1);

    int sock;
    struct sockaddr_in server;
    char message[1000] , server_reply[2000];
    msg_header e_block;

    //Create socket
    sock = socket(AF_INET , SOCK_STREAM , 0);
    if (sock == -1)
    {
        printf("Could not create socket");
    }
    puts("Socket created");

    server.sin_addr.s_addr = inet_addr("10.1.1.2");
    server.sin_family = AF_INET;
    server.sin_port = htons( 8888 );

    //Connect to remote server
    if (connect(sock , (struct sockaddr *)&server , sizeof(server)) < 0)
    {
        perror("connect failed. Error");
        return 1;
    }

    puts("Connected\n");


   printf("the file size = %i \n", len1);

    //keep communicating with server
    while(len1>0)
    {

        //record the offset
        e_block.offset = in_off;

        //first, splice data from file to pipe

        splice_bytes = splice(in_fd, &in_off, filedes[1], NULL, 32736, SPLICE_F_MOVE|SPLICE_F_MORE);
        if(splice_bytes < 0)
        {
                perror("splice error");
        }


        //record the size
        e_block.size = splice_bytes;

        //Send e-block header over
        if( send(sock , &e_block , sizeof(msg_header) , 0) < 0)
        {
            puts("Send failed");
            return 1;
        }

        printf("e-block size %i, offset %ld \n", e_block.size, e_block.offset);

        len2 = splice_bytes;
        while(len2 > 0) {
        //send the data over network
                len3 = splice(filedes[0],NULL, sock, 0, len2, SPLICE_F_MOVE|SPLICE_F_MORE);
                len2 = len2 - len3;
        }

        len1 -= splice_bytes;

     }

    close(sock);
    close(filedes[0]);
    close(filedes[1]);
    close(in_fd);

    return 0;
}

编译代码时,出现以下错误:

client.c: In function ‘main’:
client.c:45: error: ‘F_SETPIPE_SZ’ undeclared (first use in this function)
client.c:45: error: (Each undeclared identifier is reported only once
client.c:45: error: for each function it appears in.)

“#define _GNU_SOURCE”已包含在我的代码中。

这是我的“uname -r”

“Linux capecod 3.12.12#3 SMP Thu Feb 27 21:55:17 CST 2014 x86_64 x86_64 x86_64 GNU / Linux”

有人可以帮帮我吗?感谢

WWU

0 个答案:

没有答案