Linux C stdin stdout 以及重定向

本文通过分析Tinyhttpd源码,探讨Linux C中的stdin、stdout概念及重定向。通过示例代码解释输入输出重定向的工作原理,并讨论了多个server_demo程序运行时的情况,解释了exec函数族如何替换进程映像但保持PID不变。
摘要由CSDN通过智能技术生成

最近观摩了一下Tinyhttpd的源码,Tinyhttpd 是J. David Blackstone在1999年写的一个不到 500 行的超轻量型 Http Server。

官网:http://tinyhttpd.sourceforge.net/

别人的总结:https://github.com/EZLippi/Tinyhttpd

其中有一段:

管道、重定向!!

自己来写个demo来学习一下

首先是server_demo.c的代码

#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
int main()
{
	char s[1024] = "hello world";
	int input[2],output[2];
	pid_t pid;
	
	if(pipe(input) < 0)	return 0;
	if(pipe(output) < 0)	return 0;
	pid = fork();
	if(pid == 0)
	{
		dup2(input[0],STDIN_FILENO);//0
		dup2(output[1],STDOUT_FILENO);//1
		
	
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值