⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 piperw.c

📁 ARM Linux2.4 进程间通信 非常好
💻 C
字号:
/*********************************************************************
* File:		piperw.c
* Author:	Embest	J.Zhao	2005.2.17
* Desc:		use pipes
* History:
*********************************************************************/

#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <asm/limits.h>

#define BUF_SIZE PIPE_BUF

int main(void)
{
	int nFd[2];						/* Array for the pipe */
	int nPid, nLen;
	char szBuf[BUF_SIZE];
		
	/* Create pipe */
	if ((pipe(nFd)) < 0)
	{
		printf("create pipe error.\n");
		exit(0);
	}
	
	/* fork */
	if ((nPid = fork()) < 0)
	{
		printf("fork error.\n");
		exit(-1);
	}		
	if (nPid == 0)
	{
		/* child open the read descriptor, close the write descriptor */
		close (nFd[0]);
		write (nFd[1], "message\n", 8);
		close (nFd[1]);
		exit(0);
	}
	else
	{
		close (nFd[1]);
		while ((nLen = read(nFd[0], szBuf, nLen)) > 0)
			write (STDOUT_FILENO, szBuf, nLen);
		close (nFd[0]);
	}

	waitpid(nPid, NULL, 0);
	return 0;
}

⌨️ 快捷键说明

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