pipetest.c

来自「klibc精简化的c程序库」· C语言 代码 · 共 40 行

C
40
字号
#include <stdio.h>#include <stdlib.h>#include <unistd.h>#include <fcntl.h>#include <string.h>#include <errno.h>int main(void){	/* Size of message must be <= PIPE_BUF */	const char msg[] = "Hello, World!";	char buf[512];	int rv;	int pfd[2];	if (pipe(pfd)) {		perror("pipe");		return 1;	}	if (write(pfd[1], msg, sizeof msg) != sizeof msg) {		perror("write");		return 1;	}	while ((rv = read(pfd[0], buf, sizeof buf)) < sizeof msg) {		if (rv == -1 && errno == EINTR)			continue;		perror("read");		return 1;	}	if (memcmp(msg, buf, sizeof msg)) {		fprintf(stderr, "Message miscompare!\n");		return 1;	}	return 0;}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?