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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T09:12:32+00:00 2026-06-04T09:12:32+00:00

I read a book that gives the next example: int value=0 int thread_func(int id)

  • 0

I read a book that gives the next example:

int value=0
int thread_func(int id) {
   int temp;
   temp=value+id;
   printf("Thread%d value: %d", id, temp);
   value=temp;
}

int main() {
   int fork_id, status, i;
   pthread_t tids[3];
   fork_id=fork();
 if (fork_id == 0) {
   for (i=1; i≤3; i++) 
        pthread_create(&tids[i-1], NULL, thread_func, i);
      for (i=0; i≤2; i++)
        pthread_join(tids+i, &status);
      printf("Second process value: %d", value);
  }
 else {
  wait(&status);
      printf("First process value: %d", value)
      }

I don’t understand two main things:
As I read, the only value that the line has in printf("First process value: %d", value) is 0.
But why? wait(&status) is wait until the child process is terminate. In out case, it will terminate only after all the joins would done. meaning, when the value is 6.

Second, in the line printf("Second process value: %d", value);, the vaule can be from 1 to 6. This is also strange, because we have the join instruction.

  • 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-06-04T09:12:34+00:00Added an answer on June 4, 2026 at 9:12 am

    The answers to your questions:

    1. The value will be 0 in the parent process because when the fork occurs the address space of the parent is duplicated (along with the variable value) in the child process. Hence, although value is changed in the child, this change is not reflected in the parent as they are different variables.

    2. Since there is no synchronization involved, there is no way to know in which order the variable value is changed by the three child threads. Specifically, each thread has a local temp variable with a different value, which is then copied in the global value variable, but there is no way to know in which order the threads will overwrite value with temp here: value = temp;. Hence, its value can vary between executions.

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

Sidebar

Related Questions

I read a book, which gives example of .h file, that equal to interface
I read a book that write the next program in C, and convet the
I read a book, which give the next example: There is a list, which
I read a book, which give the next code: void *printme(void *id) { int
I read a book about the observer pattern. It gives the following example: Class
I read a book that uses infix, infixr, and infixl in the sample programs.
I want to read a book that systematically introduces dom with live examples. Any
My buddy read an OpenGL book that said interleaving your vertex pointers and the
I have read in a Java book that says: Because a String is immutable,
I was fairly sure that I had read (in Richter's C# book) that objects

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.