5

As when we do fork on current process, our process as parent process generates child process with same characteristics but different process IDs. So after that, when we do exec() in our child process, process stops execution, and our program which was executing in our stoppped child process, now has his own process.

Isn't that the same as when we run our applications in particular after which every application has his own process and PID?

Junior
  • 343
  • "Isn't that the same as when we run our applications in particular after which every application has his own process and PID number?" ... doesn't parse. What are you trying to say? – muru Sep 24 '15 at 09:01
  • 1
    exec() doesn't change the PID, if that's what you're asking. – cxw Sep 24 '15 at 09:14

1 Answers1

10

Yes, because that's how it's done in UNIX.

There is no "run application" system call; it's always done by fork/exec pairs.

Incidentally, exec does not generate a new PID. exec replaces the contents of the process -- the memory is discarded, and a whole new executable is loaded -- but the kernel state remains the same (open files, environment variables, working directory, user, etc.), and the PID remains the same.


Further reading, if you're interested:

  • vfork is like fork except that it must always be paired with exec, and is useful when fork can't work, as in ucLinux.

  • clone is the new fork (today's fork function uses clone behind the scenes) but does a lot more, including creating new processes that share the same memory (rather than duplicate it, like fork) and we call those threads.

ams
  • 5,807
  • 1
  • 20
  • 27
  • So every new process is created at first by forking existing process and exec() child of parent of that process? So the PID in that way stays the same regardless its newly created process? – Junior Sep 24 '15 at 09:34
  • 1
    Yes, the fork creates a new PID, and the exec inherits the new number. – ams Sep 24 '15 at 09:35
  • So our launched terminal is also the process, and fork and exec are applied to it? – Junior Sep 24 '15 at 09:37
  • 1
    I don't understand your question. Everything is a process. – ams Sep 24 '15 at 09:39
  • I am trying to understand on what process is fork() and exec() applied after we logged on terminal and want to run our application. – Junior Sep 24 '15 at 09:40
  • 3
    Your shell process runs fork to create a new process, when you hit enter, and then the new forked process runs exec with the command you wrote. – ams Sep 24 '15 at 09:44
  • Further reading on fork vs. clone: http://unix.stackexchange.com/q/199686/135943 – Wildcard Mar 12 '17 at 04:00
  • 1
    @ams, is fork() efficient? Say I have a large application that runs a simple process /bin/ls for example to take its output. All parent's memory will be duplicated just to be replaced from ls? – Vassilis Feb 03 '19 at 13:38
  • 3
    @Vassilis, it's not duplicated, it's shared. The only copying is a few page tables inside the kernel. If the child process attempts to write to memory, only then does the memory get duplicated, and only for that one page (about 4k, I think). – ams Feb 04 '19 at 14:25
  • So this is how all new processes are spawned? From a combination of fork and exec? – the_prole Sep 20 '21 at 03:47
  • @the_prole, yes. – ams Sep 22 '21 at 10:26