I can't understand why fork () works that way. Here is an excerpt from the book "Modern OS" Tanenbaum

In UNIX, there is only one system call for creating a new process - fork. This call creates an exact copy of the calling process. After the fork system call is executed, the two processes, the parent and child, have a single memory image, single configuration description lines and the same open files. And nothing more. Usually, after this, the child process changes the memory image and starts a new program, executing an execve system call or similar. For example, when a user types a sort command in a shell, the shell creates a branching child process in which the sort command is executed. The purpose of this two-step process is to allow a child process to manage its file descriptors after branching, but before execve, to redirect the standard input, standard output, and standard error message output.

I do not understand why if we call the sort command, you cannot just execute the sort () program, but you need to create a copy of this program.

  • almost the same: ru.stackoverflow.com/q/816388/178576 - aleksandr barakin
  • And what is, in your view, "just run the program"? - Pavel Mayorov
  • Because it is necessary to return to the command shell, otherwise the shell will be replaced by a single call to another command. Do an experiment, instead of running sort , run exec sort . Report to us about the result. - 0andriy 10:10

2 answers 2

why if we call the sort command, you cannot just execute the sort () program, but you need to create a copy of this program

Because after working out a called program ( sort ), the current program probably still needs to do something useful.

And if she does not first fork() , then she will overwrite (with the program being called) herself by calling execve() .

  • one
    I suspect that the question is a bit different: type, why create a copy of the current process and start sort in it, if theoretically you can immediately create a new process with sort inside without any copies (as I understand it, this is exactly what happens in Windows, unlike by Unix) - andreymal
  • one
    @andreymal, yes, perhaps the author misunderstands the difference between “load the library into the process address space” and “load the program on top of the current process”. // ps and the difference in starting a new process in unix and ms / windows is well described by the link I gave in the comments questionable. - aleksandr barakin 1:58 pm
  • Well, the author did not say anything about the library (only remotely hint after the sort brackets), but also an option - andreymal
  • What are you talking about, gentlemen ?! Obviously, branching is necessary in order to be able to return to the command shell. Otherwise, the meaning of the command shell is lost. - 0andriy

At a minimum, because a buggy "child" (a program that is loaded and executed right in the process of a parent) can drop, block, write to memory, and generally interfere with the parent program. The same will be a communal apartment, where in case of ignition of the mattress of Vasya, an alcoholic will suffer all

  • And why should a child give his memory and handles? Windows also allows you to run child processes without parent cloning - Anton Shchyrov
  • And how can a program drop something if a memory area is allocated to it that does not overlap with other programs? - Yuri Kot
  • @AntonShchyrov, a copy, break the parent is no longer possible. And it is useful to fumble. - free_ze
  • @YuriKot, if the launch of the new program will occur within the same process, the memory will be shared. One of the reasons for creating a new process is its isolation. - free_ze
  • So do not give anything and do not need a copy. The fact that handles useful to fumble - I do not argue. But, usually out of several hundred parent handles, a child needs a dozen, maximum. And even 2-3. So, it is easier to share a dozen of concrete handles than to copy (even if copy-on-write) all memory - Anton Shchyrov