Alex Rivera | Logout

Is it safe to fork from within a thread?

Asked 2011-05-21T00:07:59.170
56

Let me explain: I have already been developing an application on Linux which forks and execs an external binary and waits for it to finish. Results are communicated by shm files that are unique to the fork + process. The entire code is encapsulated within a class.

Now I am considering threading the process in order to speed things up. Having many different instances of class functions fork and execute the binary concurrently (with different parameters) and communicate results with their own unique shm files.

Is this thread safe? If I fork within a thread, apart from being safe, is there something I have to watch for? Any advice or help is much appreciated!

Edit
Report

1 Answer

0

If you are using the unix 'fork()' system call, then you are not technically using threads- you are using processes- they will have their own memory space, and therefore cannot interfere with eachother.

As long as each process uses different files, there should not be any issue.

answered 2011-05-21T00:24:29.440

Your Answer