Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6685045
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T05:00:41+00:00 2026-05-26T05:00:41+00:00

I do not get why Unix has fork() for creating a new process. In

  • 0

I do not get why Unix has fork() for creating a new process. In Win32 API we have CreateProcess() which creates a new process and loads an executable into its address space, then starts executing from the entry point. However Unix offers fork for creating a new process, and I don’t get why would I duplicate my process if I’d like to run another process.

So let me ask these two questions:

  1. If fork() and then exec() is more efficient, why isn’t there a function forkexec(const char *newProc) since we will call exec() after fork() almost in every case?
  2. If it is not more efficient, why does fork() exist at all?
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T05:00:42+00:00Added an answer on May 26, 2026 at 5:00 am

    The fork() call is sufficient. It is also more flexible; it allows you to things like adjust the I/O redirection in the child process, rather than complicating the system call to create the process. With SUID or SGID programs, it allows the child to lose its elevated privileges before executing the other process.

    If you want a complex way to create a process, lookup the posix_spawn() function.

    #include <spawn.h>
    
    int posix_spawn(pid_t *restrict pid, const char *restrict path,
            const posix_spawn_file_actions_t *file_actions,
            const posix_spawnattr_t *restrict attrp,
            char *const argv[restrict], char *const envp[restrict]);
    
    int posix_spawnp(pid_t *restrict pid, const char *restrict file,
              const posix_spawn_file_actions_t *file_actions,
           const posix_spawnattr_t *restrict attrp,
           char *const argv[restrict], char *const envp[restrict]);
    

    The difference is the posix_spawnp() does a search on PATH for the executable.

    There is a whole set of other functions for handling posix_spawn_file_actions_t and posix_spawnattr_t types (follow the ‘See Also’ links at the bottom of the referenced man page).

    This is quite a bit more like CreateProcess() on Windows. For the most part, though, using fork() followed shortly by exec() is simpler.


    I don’t understand what you mean. The child process code will be written by me, so what is the difference between writing if (fork() == 0) and putting this code in the beginning of child’s main()?

    Very often, the code you execute is not written by you, so you can’t modify what happens in the beginning of the child’s process. Think of a shell; if the only programs you run from the shell are those you’ve written, life is going to be very impoverished.

    Quite often, the code you execute will be called from many different places. In particular, think of a shell and a program that will sometimes be executed in a pipeline and sometimes executed without pipes. The called program cannot tell what I/O redirections and fixups it should do; the calling program knows.

    If the calling program is running with elevated privileges (SUID or SGID privileges), it is normal to want to turn those ‘off’ before running another program. Relying on the other program to know what to do is … foolish.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

On Unix, we can create a new process with fork(); execvp(argv[0], argv); (with a
I'm porting a UNIX application which uses the Qt framework (Qt, not QuickTime) to
Just could not get this one and googling did not help much either.. First
I simply can not get Visual Studio 2005 to find the System.Configuration.ConfigurationManager class. Here
I can not get to make this comparison in this simple code error ..
I can not get logs from some customers, can I use Google Urchin in
Did I not get enough sleep or what? This following code var frame=document.getElementById(viewer); frame.width=100;
It seems to not get past this step: [ 16%] Building CXX object src/CMakeFiles/hphp_runtime_static.dir/lib/system/gen/sys/dynamic_table_func.no.cpp.o
Here is some code I could not get to format properly in markdown, this
Why does the array a not get initialized by global variable size ? #include<stdio.h>

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.