Threads and forks (). How can I handle this?

Possible duplicate:
in multithreaded program

If I have an application that uses fork () and can be designed as multi-threaded, what are the rules / thumb recommendations that should be considered for safely programming such applications?

+6
source share
2 answers

Basic rules of the thumb, according to various Internet articles such as ( http://www.linuxprogrammingblog.com/threads-and-fork-think-twice-before-using-them , fork in a multi-threaded program ):

  • (Main) Process [0] Monothread β†’ fork () β†’ (Child) Process [1] Multithreading: OK!
    If Process [1] crashes or messes with memory, it will not touch the address space of Process [0] (unless you use shared R / W memory ... but that's another topic). by default, all fork () ed memory Copy On Write . Given that process [0] is thread-safe, when we call fork (), all possible mutual exclusion primitives should be generally in an unlocked state.

  • (Main) Process [0] Multithreading β†’ fork () β†’ (Child) Process [1] Mono / Multithreading: BAD!
    If fork () is a multi-threaded process, your mutexes and many other thread synchronization primitives will most likely be undefined in Process [1]. You can work with pthread_atfork (), but if you use libraries, you can also roll the die and hope that you are lucky. Because, as a rule, you don’t (want) to know the details of the implementation of libraries.

The advantages of fork () in a multi-threaded process are that you can faster / faster manipulate / read / collapse your data (in a child process) without worrying about the stability of the process that you use fork () from (Main). This is useful if your main process has a large amount of data set and you do not want to duplicate / reload it in order to safely process data in another process (Child). Thus, the original process is stable and independent of the process of aggregation / data processing (fork () ed).

Of course, this means that the initial process will usually be slower than it could be if it was developed in a multi-threaded way. But then again, this is the price you might want to pay for more stability.

If your main process is multithreaded instead, refrain from using fork (). This will be the right mess to implement in a stable way.

Greetings

+4
source

On Linux, threads are implemented in terms of processes. In other words, threads are actually just fork() with most of the shared memory, not copy to write entirely. This means that when you use fork() on a stream (main or another), you end up copying all the shared memory space of all the threads and storage with the specific thread of the stream that you are calling fork() from,

Now all this sounds good, but that does not mean that it will or will work well. If you want to make a cloned process, try making a fork before starting any other threads, and then use read-only virtual memory to update the branching process with the current memory values.

So, although this might work, I just suggest testing and try to find another way first. And get ready for a lot:

 Segmentation fault 
-1
source

Source: https://habr.com/ru/post/927613/


All Articles