pipe:splice/teeサンプル


Rev.1を表示中。最新版はこちら

[root@localhost north]# cat pipe.c
#define _GNU_SOURCE

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/syscall.h>

void	read_pipe_print(int pfd, char* pfdtype);
void	write_pipe1();
void	pipe_splice(int cnt);
void	pipe_tee(int cnt);
void	pipe_read_write(int cnt);

int   pfd1[2], pfd2[2];
char 	*pdata = "1234567890";

void main(int argc, char *argv[])
{ 
    pipe2(pfd1, O_NONBLOCK);
    pipe2(pfd2, O_NONBLOCK);

    int cnt = atoi(argv[2]);
    write_pipe1();
    if(!strcmp(argv[1], "READ_WRITE")) {
        pipe_read_write(cnt);
     }
    if(!strcmp(argv[1], "SPLICE")) {
        pipe_splice(cnt);
     }
    if(!strcmp(argv[1], "TEE")) {
        pipe_tee(cnt);
     }

    read_pipe_print(pfd1[0], "pfd1");
    read_pipe_print(pfd2[0], "pfd2");
    printf("\n");
}

void	pipe_read_write(int cnt)
{
    char	buff[64];

    cnt = read(pfd1[0], buff, cnt);
    write(pfd2[1], buff, cnt);
}

void	pipe_splice(int cnt)
{
    splice(pfd1[0], NULL, pfd2[1], NULL, cnt, 0);
}

void	pipe_tee(int cnt)
{
    syscall(SYS_tee, pfd1[0], pfd2[1], cnt, 0);
}

void	write_pipe1()
{
    write(pfd1[1], pdata, strlen(pdata));
}

void	read_pipe_print(int pfd, char* pname)
{
    char	buff[64];

    int cnt = read(pfd, buff, 64);
    buff[cnt] = 0;
    printf("%s -> %s\n", pname, buff);
}

[root@localhost north]# ./pipe.out READ_WRITE 1
pfd1 -> 234567890
pfd2 -> 1
[root@localhost north]# ./pipe.out READ_WRITE 2
pfd1 -> 34567890
pfd2 -> 12

[root@localhost north]# ./pipe.out SPLICE 1
pfd1 -> 234567890
pfd2 -> 1
[root@localhost north]# ./pipe.out SPLICE 2
pfd1 -> 34567890
pfd2 -> 12

[root@localhost north]# ./pipe.out TEE 1
pfd1 -> 1234567890
pfd2 -> 1
[root@localhost north]# ./pipe.out TEE 2
pfd1 -> 1234567890
pfd2 -> 12










north@localhost ]





[root@localhost mnt1]# cat babakaka.c
#define _GNU_SOURCE

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/syscall.h>

void read_pipe(int pfd, char* pfdtype);
void write_pipe1();
void pipe_splice();
void pipe_tee();
void pipe_file();

int pfd1[2], pfd2[2];
char *pdata = "1234567890abcdefg";

void main(int argc, char *argv[])
{
pipe2(pfd1, O_NONBLOCK);
pipe2(pfd2, O_NONBLOCK);

write_pipe1();
if(!strcmp(argv[1], "READ")) {
pipe_read();
}
if(!strcmp(argv[1], "SPLICE")) {
pipe_splice();
}
if(!strcmp(argv[1], "TEE")) {
pipe_tee();
}
read_pipe(pfd1[0], "pfd1");
read_pipe(pfd2[0], "pfd2");
}

void pipe_read()
{
char buff[64];

int cnt = read(pfd1[0], buff, strlen(pdata));
write(pfd2[1], buff, cnt);
}

void pipe_splice()
{
splice(pfd1[0], NULL, pfd2[1], NULL, strlen(pdata), 0);
}

void pipe_tee()
{
syscall(SYS_tee, pfd1[0], pfd2[1], strlen(pdata), 0);
}
void write_pipe1()
{
if (write(pfd1[1], pdata, strlen(pdata)) < 0) {
printf("write pipe err\n");
}
}

void read_pipe(int pfd, char* pname)
{
char buff[64];
int cnt;

printf("%s -> ", pname);
cnt = read(pfd, buff, 64);
if (cnt >= 0) {
buff[cnt] = 0;
printf("%s\n", buff);
}
else {
printf("no data in pipe buffer\n");
}

}

[root@localhost mnt1]# ./babakaka.out READ
pfd1 -> no data in pipe buffer
pfd2 -> 1234567890abcdefg

[root@localhost mnt1]# ./babakaka.out SPLICE
pfd1 -> no data in pipe buffer
pfd2 -> 1234567890abcdefg

[root@localhost mnt1]# ./babakaka.out TEE
pfd1 -> 1234567890abcdefg
pfd2 -> 1234567890abcdefg

最終更新 2017/12/18 12:16:59 - north
(2017/12/18 12:16:59 作成)


検索

アクセス数
3588639
最近のコメント
コアダンプファイル - sakaia
list_head構造体 - yocto_no_yomikata
勧告ロックと強制ロック - wataash
LKMからのファイル出力 - 重松 宏昌
kprobe - ななし
ksetの実装 - スーパーコピー
カーネルスレッドとは - ノース
カーネルスレッドとは - nbyst
asmlinkageってなに? - ノース
asmlinkageってなに? - よろしく
Adsense
広告情報が設定されていません。