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 3339504
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T00:30:00+00:00 2026-05-18T00:30:00+00:00

I am having some trouble understanding how to use Unix’s fork() . I am

  • 0

I am having some trouble understanding how to use Unix’s fork(). I am used to, when in need of parallelization, spawining threads in my application. It’s always something of the form

CreateNewThread(MyFunctionToRun());

void myFunctionToRun() { ... }

Now, when learning about Unix’s fork(), I was given examples of the form:

fork();
printf("%d\n", 123);

in which the code after the fork is “split up”. I can’t understand how fork() can be useful. Why doesn’t fork() have a similar syntax to the above CreateNewThread(), where you pass it the address of a function you want to run?

To accomplish something similar to CreateNewThread(), I’d have to be creative and do something like

//pseudo code
id = fork();

if (id == 0) { //im the child
    FunctionToRun();
} else { //im the parent
    wait();
}

Maybe the problem is that I am so used to spawning threads the .NET way that I can’t think clearly about this. What am I missing here? What are the advantages of fork() over CreateNewThread()?

PS: I know fork() will spawn a new process, while CreateNewThread() will spawn a new thread.

Thanks

  • 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-18T00:30:00+00:00Added an answer on May 18, 2026 at 12:30 am

    fork() says “copy the current process state into a new process and start it running from right here.” Because the code is then running in two processes, it in fact returns twice: once in the parent process (where it returns the child process’s process identifier) and once in the child (where it returns zero).

    There are a lot of restrictions on what it is safe to call in the child process after fork() (see below). The expectation is that the fork() call was part one of spawning a new process running a new executable with its own state. Part two of this process is a call to execve() or one of its variants, which specifies the path to an executable to be loaded into the currently running process, the arguments to be provided to that process, and the environment variables to surround that process. (There is nothing to stop you from re-executing the currently running executable and providing a flag that will make it pick up where the parent left off, if that’s what you really want.)

    The UNIX fork()-exec() dance is roughly the equivalent of the Windows CreateProcess(). A newer function is even more like it: posix_spawn().

    As a practical example of using fork(), consider a shell, such as bash. fork() is used all the time by a command shell. When you tell the shell to run a program (such as echo "hello world"), it forks itself and then execs that program. A pipeline is a collection of forked processes with stdout and stdin rigged up appropriately by the parent in between fork() and exec().

    If you want to create a new thread, you should use the Posix threads library. You create a new Posix thread (pthread) using pthread_create(). Your CreateNewThread() example would look like this:

    #include <pthread.h>
    
    /* Pthread functions are expected to accept and return void *. */ 
    void *MyFunctionToRun(void *dummy __unused);
    
    pthread_t thread;
    int error = pthread_create(&thread,
            NULL/*use default thread attributes*/,
            MyFunctionToRun,
            (void *)NULL/*argument*/);
    

    Before threads were available, fork() was the closest thing UNIX provided to multithreading. Now that threads are available, usage of fork() is almost entirely limited to spawning a new process to execute a different executable.

    below: The restrictions are because fork() predates multithreading, so only the thread that calls fork() continues to execute in the child process. Per POSIX:

    A process shall be created with a single thread. If a multi-threaded process calls fork(), the new process shall contain a replica of the calling thread and its entire address space, possibly including the states of mutexes and other resources. Consequently, to avoid errors, the child process may only execute async-signal-safe operations until such time as one of the exec functions is called. [THR] [Option Start] Fork handlers may be established by means of the pthread_atfork() function in order to maintain application invariants across fork() calls. [Option End]

    When the application calls fork() from a signal handler and any of the fork handlers registered by pthread_atfork() calls a function that is not asynch-signal-safe, the behavior is undefined.

    Because any library function you call could have spawned a thread on your behalf, the paranoid assumption is that you are always limited to executing async-signal-safe operations in the child process between calling fork() and exec().

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

Sidebar

Related Questions

I'm having some trouble understanding how to use prepared statements, when you need to
I'm having some trouble understanding how to use coroutines properly with luabind. There's a
Background: I'm having some trouble understanding exactly how to best use controllers in an
I'm having some trouble understanding this piece of code that my professor used as
I'm having some trouble understanding a bit of code. I've got 2 classes Company
I'm having some trouble understanding Outlook terms (CommandBarPopup, CommandBarButton etc) like what is what
I'm having some trouble understanding the delegate/data source methodology. I understand that they exist
Hey I'm having some trouble understanding how Recursive Algebraic Types work and how to
I am having some trouble understanding designated initializers. I am studying Objective C from
I've been reading through Programming Clojure, and I've been having some trouble understanding Stuarts

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.