fork与exit、_exit的配合使用

简介:
#include "light.h"

int main(int argc, char *argv[])
{
    printf("Hello world\n");
    write(STDOUT_FILENO, "Ciao\n", 5);
    if (fork() == -1)
        errExit("fork");
    /* Both child and parent continue execution here */
    exit(EXIT_SUCCESS);
}

When we run this program with standard output directed to the terminal, we see the expected result:
$ ./fork_stdio_buf
Hello world
Ciao
However, when we redirect standard output to a file, we see the following:
$ ./fork_stdio_buf > a
$ cat a
Ciao
Hello world
Hello world


Why?
recall that the stdio buffers are maintained in a process's user-space memory.
Therefore, these buffers are duplicated in the child by fork(). When standard out-put is 
directed to a terminal, it is line-buffered by default, with the result that the newline-terminated 
string written by printf() appears immediately. However, when standard output is directed to a 
file, it is block-buffered by default. Thus, in our example, the string written by  printf() is 
still in the parent’s  stdio  buffer at the time of the fork(), and this string is duplicated in 
the child. When the parent and the child later call  exit(), they both flush their copies of the stdio 
buffers, resulting in duplicate output.

We can prevent this duplicated output from occurring in one of the following ways:
	As a specific solution to the stdio  buffering issue, we can use fflush() to flush the
stdio buffer prior to a  fork() call. Alternatively, we could use  setvbuf() or setbuf()
to disable buffering on the  stdio  stream.
	Instead of calling  exit(), the child can call  _exit(), so that it doesn’t flush stdio
buffers. This technique exemplifies a more general principle: in an application that creates 
child processes, typically on ly one of the processes (most often the parent) should terminate 
via exit(), while the other processes should terminate via _exit(). This ensures that only one 
process calls exit handlers and flushes stdio buffers, which is usually desirable.

The output of the write() in the program doesn’t appear twice, because write() transfers data 
directly to a kernel  buffer, and this buffer is not dupli-cated during a  fork(). By now, the reason 
for the second strange aspect of the program’s output when redirected to a file should be clear. 
The output of  write() appears before that from printf()  because the output of write() is immediately 
transferred to the kernel buffer cache, while the output from printf() is transferred only when 
the stdio  buffers are flushed by the call to exit().

目录
相关文章
|
前端开发 API
图片或文件Blob、File、Base64之间的相互转换
在做前端项目的时候,遇到图片的上传或者下载时,不可避免的会遇到Blob、File、Base64三种类型的转换。那么今天就总结下,三者之间的相互转换。 首先我们看看base64 ,File 对象,Blob 对象长什么样的,怎么来的。
1094 1
|
存储 人工智能 安全
欧盟GDPR升级草案曝光 跨境数据合规成本将增30% - 金海境科技
服务器数据恢复,勒索病毒解密恢复,虚拟化数据恢复,数据库修复数据恢复,VMWare数据恢复,分布式数据恢复,vSAN数据恢复,存储数据恢复,数据恢复
757 1
|
12月前
|
人工智能 缓存 算法
为什么你学了那么多算法,代码性能还是“一塌糊涂”?
本文针对开发者普遍存在的“学了算法却写不出高性能代码”的痛点,提供了一套系统化的“算法优化AI指令”。该指令旨在引导开发者建立“分析-设计-验证”的工程化思维,通过结构化的提问框架,让AI成为辅助性能优化的“私人教练”,从而将零散的算法知识转化为体系化的实战能力。
530 7
|
SQL 资源调度 OLAP
StarRocks 性能实测:在 Coffee-shop Benchmark 中快 10 倍!
在评估数据库性能时,如何同时衡量“算得快”和“算得省”一直是工程师关注的核心问题。
|
设计模式 算法 Java
设计模式觉醒系列(04)策略模式|简单工厂模式的升级版
本文介绍了简单工厂模式与策略模式的概念及其融合实践。简单工厂模式用于对象创建,通过隐藏实现细节简化代码;策略模式关注行为封装与切换,支持动态替换算法,增强灵活性。两者结合形成“策略工厂”,既简化对象创建又保持低耦合。文章通过支付案例演示了模式的应用,并强调实际开发中应根据需求选择合适的设计模式,避免生搬硬套。最后推荐了JVM调优、并发编程等技术专题,助力开发者提升技能。
|
开发框架 .NET 测试技术
了解 .NET 9 中的新 Microsoft.AspNetCore.OpenApi 包,并将其与 NSwag 和 Swashbuckle.AspNetCore 进行比较。
本文介绍了 `.NET 9` 中新推出的 `Microsoft.AspNetCore.OpenApi` 包,该包旨在为 `ASP.NET Core` 应用程序生成 `OpenAPI` 文档。文章对比了 `NSwag` 和 `Swashbuckle.AspNetCore` 两大现有库,探讨了新包的优势和不足,特别是在性能和功能方面。尽管新包在某些方面尚不及成熟库完善,但其对原生 `AoT` 编译的支持和未来的扩展潜力使其成为一个值得考虑的选择。文章还提供了详细的性能测试数据和优化建议,适合对 `OpenAPI` 文档生成感兴趣的开发者阅读。
1213 4
了解 .NET 9 中的新 Microsoft.AspNetCore.OpenApi 包,并将其与 NSwag 和 Swashbuckle.AspNetCore 进行比较。
|
JavaScript 前端开发 测试技术
精通Selenium:从基础到高级的网页自动化测试策略
【10月更文挑战第6天】随着Web应用变得越来越复杂,手动进行功能和兼容性测试变得既耗时又容易出错。自动化测试因此成为了现代软件开发不可或缺的一部分。Selenium是一个强大的工具集,它支持多种编程语言(包括Python),允许开发者编写脚本来模拟用户与Web页面的交互。本文将带领读者从Selenium的基础知识出发,逐步深入到高级的应用场景,通过丰富的代码示例来展示如何高效地进行网页自动化测试。
2602 5
|
消息中间件 Java 物联网
消息队列 MQ操作报错合集之建立连接时发生了超时错误,该如何解决
消息队列(MQ)是一种用于异步通信和解耦的应用程序间消息传递的服务,广泛应用于分布式系统中。针对不同的MQ产品,如阿里云的RocketMQ、RabbitMQ等,它们在实现上述场景时可能会有不同的特性和优势,比如RocketMQ强调高吞吐量、低延迟和高可用性,适合大规模分布式系统;而RabbitMQ则以其灵活的路由规则和丰富的协议支持受到青睐。下面是一些常见的消息队列MQ产品的使用场景合集,这些场景涵盖了多种行业和业务需求。
消息队列 MQ操作报错合集之建立连接时发生了超时错误,该如何解决
|
SQL 开发框架 .NET
聊聊 System.Linq.Dynamic,以及分享一个使用 System.Linq.Dynamic 扩展 LINQ 查询的详细例子
聊聊 System.Linq.Dynamic,以及分享一个使用 System.Linq.Dynamic 扩展 LINQ 查询的详细例子
710 0
|
Kubernetes Linux Docker
CentOS安装k8s
CentOS安装k8s
1370 0