开发者

How to launch a new process with different program in Unix?

开发者 https://www.devze.com 2023-04-08 01:29 出处:网络
I heard that the Unix fork will copy current process. Does it mean exactly same program and state will be spawned in a new child process? I can\'t understand why it work in that way. Because it 开发者

I heard that the Unix fork will copy current process. Does it mean exactly same program and state will be spawned in a new child process? I can't understand why it work in that way. Because it 开发者_Python百科looks inefficient.

  1. What's the being copied?
  2. And why does it work in that way?
  3. Is there no other way to spawn new child process? (or separated process)


It does work that way -- sort of. It's mostly implemented using "copy on write", meaning that most memory pages are shared between the two processes until the child tries to write to a page, at which point it really is copied. Therefore if a child is forked and then immediately loads and executes a new binary image, the inefficiency you're worried about never happens: the copies of the memory pages from the parent are never actually created.

Are there other ways to make child processes? Sure, other operating systems do things differently. The UNIX way is actually a very efficient way to do things to that the child process can inherit open file descriptors, environment variables, and other system information from its parent: it actually leads to less work, rather than more!


fork is very efficient on modern operating systems. It copies only the required pages via Copy on Write techniques or similar.

Different operating systems have different ways of spawning other processes. Linux has a clone system call (and fork is based on that) that allows a lot of control on exactly what the new process inherits from its parent.

Why it works that way, I don't know. It's efficient, widespread, and reasonably easy to use and understand (once you're read through the man pages and looked at examples).


when calling a fork() think of cloning (you have an image of one but wait they are the same - which is the first one, which is the second/clone one?), there becomes a child thread under the main thread (parent/child). The forking is a system call, but the overhead is mimimal unless you follow-up with a call to exec. So, the forking/execing does cost you alot in unix.

fork: A child process takes on the attributes of the parent - so there are sort of two separate runable code sections in the same memory space (one the parent, and one the child). To separate the two follow-up with a call to 'exec', now the child code section becomes a separated runable process and has a parent-id from its parent process. If you were to use just fork() then you have to manage the separate code sections of who is the parent, who is the child.

The exec call is the cost of overhead for the kernel, not the fork.


Does it mean exactly same program and state will be spawned in a new child process?

Not exactly the same state. There is a small difference: the fork return value will differ depending on the process is the parent or the child. Of course the pids will also differ.

Also, launching a new process doesn't need to be done with fork. posix_spawn has been standardized as an alternative when fork proves too difficult to implement.

http://pubs.opengroup.org/onlinepubs/009695399/functions/posix_spawn.html


Oh, if you can explain how the init process or swapper 'init' is created in unix, then you understand the concepts of unix process creation: fork/exec. A beautiful thing!

0

精彩评论

暂无评论...
验证码 换一张
取 消