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?

  • "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 9:01
  • 1
    exec() doesn't change the PID, if that's what you're asking. – cxw Sep 24 '15 at 9:14
up vote 4 down vote accepted

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.

  • 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 9:34
  • Yes, the fork creates a new PID, and the exec inherits the new number. – ams Sep 24 '15 at 9:35
  • So our launched terminal is also the process, and fork and exec are applied to it? – Junior Sep 24 '15 at 9:37
  • I don't understand your question. Everything is a process. – ams Sep 24 '15 at 9:39
  • 2
    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 9:44

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.