Splice (system Call) - Example

Example

This is an example of splice in action:

/* This example is wrong. A pipe is a kernel buffer of fixed size, * so if you try to splice a big enough file directly into it, * you're gonna either be blocked indefinitely or get an error. * You should splice, in a loop, a chunk of data which does not exceed * the dimension of the pipe buffer in each iteration. */ /* Transfer from disk to a log. */ int log_blocks (struct log_handle * handle, int fd, loff_t offset, size_t size) { int filedes ; int ret; size_t to_write = size; ret = pipe (filedes); if (ret < 0) goto out; /* splice the file into the pipe (data in kernel memory). */ while (to_write > 0) { ret = splice (fd, &offset, filedes, NULL, to_write, SPLICE_F_MORE | SPLICE_F_MOVE); if (ret < 0) goto pipe; else to_write -= ret; } to_write = size; /* splice the data in the pipe (in kernel memory) into the file. */ while (to_write > 0) { ret = splice (filedes, NULL, handle->fd, &(handle->fd_offset), to_write, SPLICE_F_MORE | SPLICE_F_MOVE); if (ret < 0) goto pipe; else to_write -= ret; } pipe: close (filedes ); close (filedes ); out: if (ret < 0) return -errno; return 0; }

Read more about this topic:  Splice (system Call)

Famous quotes containing the word example:

    Our intellect is not the most subtle, the most powerful, the most appropriate, instrument for revealing the truth. It is life that, little by little, example by example, permits us to see that what is most important to our heart, or to our mind, is learned not by reasoning but through other agencies. Then it is that the intellect, observing their superiority, abdicates its control to them upon reasoned grounds and agrees to become their collaborator and lackey.
    Marcel Proust (1871–1922)