`
iunknown
  • 浏览: 409346 次
社区版块
存档分类
最新评论

fork with thread

阅读更多
http://gceclub.sun.com.cn/solaris/819-7051-10.pdf

Fork-One 安全问题和解决方案
除了通常关注的问题(如锁定共享数据)以外,当只有fork() 线程处于运行状态时,还应
根据fork 子进程的操作来处理库。问题在于子进程中的唯一线程可能会尝试获取由未复制
到子进程中的线程持有的锁定。
大多数程序不可能遇到此问题。从fork() 返回后,大多数程序都会调用子进程中的
exec()。但是,如果程序在调用exec() 之前必须在子进程中执行操作,或永远不会调用
exec(),则子进程可能会遇到死锁。每个库编写者都应提供安全的解决方案,尽管提供一
个非fork 安全的库不是一个很大的问题。
例如,假设当T2 fork 新进程时,T1 在进行打印,且对printf() 持有锁定。在子进程中,
如果唯一的线程(T2) 调用printf(),则T2 将快速死锁。
POSIX fork() 或Solaris fork1() 函数仅复制用于调用fork() 或fork1() 的线程。如果调用
Solaris forkall() 来复制所有线程,则此问题不是要关注的问题。
但是,forkall() 可能会导致其他问题,使用时应小心。例如,如果一个线程调用
forkall(),则将在子进程中复制对文件执行I/O 的父线程。线程的两个副本都将继续对同
一个文件执行I/O,一个副本在父进程中,一个副本在子进程中,这将导致出现异常或文件
损坏。
要防止在调用fork1() 时出现死锁,请确保在执行fork 时任何锁定都未被持有。防止死锁的
最有效的方式就是让fork 线程获取可能由子进程使用的所有锁定。由于无法获取对
printf() 的所有锁定(由于printf() 由libc 所有),因此必须确保在使用fork() 时没有使
用printf()。
分享到:
评论
2 楼 iunknown 2007-09-16  
Category


Description

Safe


This code can be called from a multithreaded application

Safe with exceptions


See the NOTES sections of these pages for a description of the exceptions.

Unsafe


This interface is not safe to use with multithreaded applications unless the application arranges for only one thread at a time to execute within the library. 

MT-Safe


This interface is fully prepared for multithreaded access. The interface is both safe and supports some concurrency.

MT-Safe with exceptions


See the NOTES sections of these pages in the man pages section 3: Basic Library Functions for a list of the exceptions.

Async-Signal-Safe


This routine can safely be called from a signal handler. A thread that is executing an Async-Signal-Safe routine does not deadlock with itself when interrupted by a signal. 

Fork1–Safe


This interface releases locks it has held whenever Solaris fork1(2) or POSIX fork(2) is called.
1 楼 iunknown 2007-09-16  
引用
From: IEEE Std 1003.1, 2003 Edition
A process shall be created with a single thread. If a
multi-threaded process calls fork(), the new process shall
contain a replica of the calling thread and its entire address
space, possibly including the states of mutexes and other
resources. Consequently, to avoid errors, the child process may
only execute async-signal-safe operations until such time as one
of the exec functions is called. [THR]   Fork handlers may be
established by means of the pthread_atfork() function in order
to maintain application invariants across fork() calls.

相关推荐

    grails不能运行fork模式解决方法

    描述提到的异常信息是:“Exception in thread "main" Error | Forked Grails VM exited with error java.lang.instrument ASSERTION FAILED ***: "!errorOutstanding" with message transform method call failed”...

    Java 7 Concurrency Cookbook

    Chapter 1, Thread Management will teach the readers how to make basic operations with threads. Creation, execution, and status management of the threads are explained through basic examples. Chapter 2...

    Java.Threads.and.the.Concurrency.Utilities.1484216997

    The primary audience is Java beginners and the secondary audience is more advanced Java developers who have worked with the Thread APIs and the Concurrency Utilities. Table of Contents Part I: Thread...

    Java 9 Concurrency Cookbook.2e

    * Thread creation and management delegation with executors * Fork/Join framework to enhance the performance of your application * Parallel streams to process big sets of data in a parallel way, ...

    Linux内核设计与实现.pdf

    /* * offsets of these are hardcoded elsewhere - touch with care */ volatile long state; /* -1 unrunnable, 0 runnable, >0 stopped */ unsigned long flags; /* per process flags, defined below */ int ...

    Java 9 Concurrency Cookbook - Second Edition

    Solve problems using a parallelized version of the divide and conquer paradigm with the Fork / Join framework Process massive data sets in an optimized way using streams and reactive streams See which...

    C事件驱动循环的网络IO编程框架的源码.rar

    - fast intra-thread communication between multiple event loops (with optional fast linux eventfd backend). - extremely easy to embed. - very small codebase, no bloated library. - fully extensible by ...

    Android代码-konf

    Konf A type-safe cascading configuration library for Kotlin/Java, ... Config can fork from another config by adding a new layer on it. Each layer of config can be updated independently. This feat

    debugging with gdb

    - **调试多进程程序**:对于多进程程序,GDB也提供了解决方案,可以通过`fork`或`exec`命令进行调试。 #### 停止和继续 - **断点**:使用`break`命令设置断点;使用`delete`命令删除断点。 - **条件断点**:可以在...

    Debugging with GDB 10th edition

    - **调试fork操作**:当程序调用`fork`函数创建子进程时,gdb能自动跟踪新创建的子进程。 #### 知识点六:高级调试技巧 - **设置书签**:gdb支持设置书签(checkpoints),以便快速返回到调试过程中的某个特定位置...

    Debugging with gdb

    最后,gdb还支持对程序中的“fork”(分叉)操作进行调试,这在Unix/Linux系统中常见,特别是在系统编程和守护进程的开发中尤为重要。 在gdb的用户手册中还提到了一些高级调试技巧,比如设置书签,以便在调试过程中...

    Java Language Features 2nd Edition

    The chapter on threads follows this up and discusses everything from the very basic concepts of a thread to the most advanced topics such as synchronizers, the fork/join framework, and atomic ...

    Program-With-OpenMP

    基于线程的并行编程模型是OpenMP的核心,采用Fork-Join模型,主线程创建一组线程,这些线程在并行域内执行任务,最后所有线程重新加入主线程,结束并行执行。 在Fortran中,OpenMP程序结构通常使用`!$OMP PARALLEL`...

    debugging with GDB/GDB使用指南

    如果需要同时调试多个进程,可以使用`fork`命令来创建新进程。 ```bash (gdb) fork ``` 使用`info processes`命令可以查看当前所有的进程信息。 ```bash (gdb) info processes ``` #### 结论 GDB作为一款功能...

    Debugging with GDB --2007年

    5.4 Stopping and starting multi-thread programs . . . . . . . . . . . . . 6 Examining the Stack . . . . . . . . . . . . . . . . . . . . . . 51 6.1 6.2 6.3 6.4 6.5 6.6 7 Stack frames . . . . . . . . . ...

Global site tag (gtag.js) - Google Analytics