Operating System - HP-UX
1752664 Members
5587 Online
108788 Solutions
New Discussion юеВ

fork usage in a multithreading environment

 
SOLVED
Go to solution
Fedele Giuseppe
Frequent Advisor

fork usage in a multithreading environment

Hi All,

I have a multi-thread process, that in a specific thread executes a fork.

My question is: does the child process inerit all father process threads?

Thanks in advance

Giuseppe
6 REPLIES 6
Matti_Kurkela
Honored Contributor
Solution

Re: fork usage in a multithreading environment

"man fork":
(emphasis mine)

-----
DESCRIPTION

The fork() system call causes the creation of a new process. The new child process is created with _exactly one thread_ or lightweight process. The new child process contains a replica of the calling thread (if the calling process is multi-threaded) and its entire address space, possibly including the state of mutexes and other resources.
[...]
-----
i.e. the answer is "No, only the thread calling fork() is replicated."

The same is true of the vfork() system call.

MK
MK
Laurent Menase
Honored Contributor

Re: fork usage in a multithreading environment

Hi Giuseppe,

From a perf point of view it should be avoided as long as possible.

To fork() the kernel need to stop all the parent threads before duplicating its memory and fds. They are resumed after those operations.

So avoid fork() in multithread applications
use coprocess to launch forked processes and communiate with that coprocess through pipe, UNIX socket, or any other mean to ask a launch. From a perf point of view it will be much better.
if you need to share fds, you can send fds through UNIX sockets. for instance.


Dennis Handly
Acclaimed Contributor

Re: fork usage in a multithreading environment

>I have a multi-thread process, that in a specific thread executes a fork.

Note: system(3) also does a fork.

As Matti said, you only have the one thread. But libpthread would have to execute every atfork handler when you fork.

>Laurent: if you need to share fds, you can send fds through UNIX sockets

How can you do that unless you have a common grandparent?
Laurent Menase
Honored Contributor

Re: fork usage in a multithreading environment

Denis:
>How can you do that unless you have a common grandparent?

2 main ways:
one using /dev/echo and I_SENDFD/I_RECVFD ioctls

UNIX domain socket access right send with sendmsg() (man sendmsg)
RC Park
Frequent Advisor

Re: fork usage in a multithreading environment

I've been a sysadmin for over 15 years, but have never needed to understand the things you folks are discussing. It's mostly greek to me, but can you describe in general what you're doing? When does one need to know how to understand these system calls? I'm guessing you're writing a script or C program, but this is not clear. Is there an HP resource I can tap to begin to learn/understand this area of UNIX?

Thanks! Also, if you'd rather, I could create a new topic so I can assign some points too.
Dennis Handly
Acclaimed Contributor

Re: fork usage in a multithreading environment

>RC Park: I've been a sysadmin for over 15 years, but have never needed to understand the things you folks are discussing.

This is a developer question.

>I'm guessing you're writing a script or C program, but this is not clear.

This is very clear. It is a probably C or C++ program.

>I could create a new topic so I can assign some points too.

You should create your own thread for your different question.