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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T03:52:10+00:00 2026-05-18T03:52:10+00:00

Situation: I have a multithreaded program written in C. If one of the threads

  • 0

Situation:
I have a multithreaded program written in C. If one of the threads forks, the child process is replaced by another using exec() and the parent waits for the child to exit.

Problem:
After the child process is created by fork() there are a few lines of code that compile the arguments to be used in the following exec() command.

Hypothesis
Am I correct in assuming that in the time between the child process being created by fork() and being replaced by exec(), the child process – being a copy of the parent – will have all the threads of the parent and therefore these threads will run – albeit for a very brief period?

If so, is the correct solution to call exec() immediately after fork()?

  • 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-18T03:52:11+00:00Added an answer on May 18, 2026 at 3:52 am

    Only the thread that calls fork will be running in the new process. However, there are limits to which functions you can call before exec. From fork:

    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. Fork
    handlers may be established by means
    of the pthread_atfork() function in
    order to maintain application
    invariants across fork() calls.

    I believe this means you should generally be okay, as long as any multi-threaded
    libraries use pthread_atfork properly.

    EDIT: The pthread_atfork page explains further how the library can protect itself:

    The expected usage is that the prepare
    handler acquires all mutex locks and
    the other two fork handlers release
    them.

    For example, an application can supply
    a prepare routine that acquires the
    necessary mutexes the library
    maintains and supply child and parent
    routines that release those mutexes,
    thus ensuring that the child gets a
    consistent snapshot of the state of
    the library (and that no mutexes are
    left stranded). Alternatively, some
    libraries might be able to supply just
    a child routine that reinitializes the
    mutexes in the library and all
    associated states to some known value
    (for example, what it was when the
    image was originally executed).

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

Sidebar

Related Questions

I have a situation where, in a multithreaded application, many different threads are accessing
I've run into some problems debugging a multi-threaded process using GDB. I have a
Situation:I have an ASP .NET application that will search through docs using Lucene. I
I have a multithreaded Java code in which: several threads read stateful objects from
Situation: I have many gzipped input files that I want to process in a
I have the following situation: I have two threads thread1 , which is a
I have a multithreaded program running which crashes after a day or two. Moreover
I have written an application which is using NHibernate to provide the underlying object
I have a process in a website (Asp.net 3.5 using Linq-to-Sql for data access)
i have a multithreaded library in which i call sleep(3) in different threads. i

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.