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

  • Home
  • SEARCH
  • 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 6624501
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T21:39:40+00:00 2026-05-25T21:39:40+00:00

I am using an old exam as a study guide and one of the

  • 0

I am using an old exam as a study guide and one of the questions is to use pthreads to fill in the following code:

#include <pthread.h>
#include <stdio.h>

typedef struct {
    int a;
    int b;
    } local_data;

void *foo(void *arg);

int main() {
    int a = 12;
    int b = 9;
    pthread_t tid;
    pthread_attr_t attr;

    local_data local;
    local.a = a;
    local.b = b;

    pthread_attr_init(&attr);

    /* block of code we are supposed to fill in (my attempt at filling it in)
    pthread_create(&tid, &attr, foo, &local);
    pthread_join(tid, NULL);
    */

    b = b - 5;
    printf("program exit. a = %d, b = %d\n", a, b);

    return 0;
}

void *foo(void *arg) {
     int a, b;
     local_data *local = (local_data*)arg;

    /* block of code we are supposed to fill in (my attempt at filling it in)
    a = local->a;
    b = local->b;
    a++;
    */

    printf("program exit. a = %d, b = %d\n", a, b);
    pthread_exit(0);
}

What we are supposed to do is make our pthreads mimic this code:

int main() {
    int a = 12;
    int b = 9;
    int fid = fork();

    if (fid == 0) {
        a++;
    }
    else {
        wait(NULL);
        b = b - 5;
    }

    printf("program exit. a = %d, b = %d\n", a, b);
    return 0;
}

I’ve been really lost on this section and I am sure I do not understand it as well as I should (or at all). Would appreciate any answers to help me grasp the concept.

  • 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-25T21:39:41+00:00Added an answer on May 25, 2026 at 9:39 pm

    This line is wrong:

    pthread_create(&tid, &attr, foo(local), NULL);
    

    pthread_create‘s signature is:

    int pthread_create(pthread_t *thread, const pthread_attr_t *attr,
        void *(*start_routine)(void*), void *arg);
    

    The third argument is a function and the last argument is it’s argument, so instead of calling the function (foo(local)), pass the function and the argument separately:

    pthread_create(&tid, &attr, foo, &local);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here's a simple function delcared and defined using old style syntax: #include <stdio.h> void
We are using very old version of spring 2.0.6 in our code, We were
I'm using mod_rewrite to map my old directory structure to a new one. I
I'm using an old thread to post new code which attempts to solve the
My study guide (for 70-536 exam) says this twice in the text and encoding
I'm currently writing a small script for use on one of our servers using
I'm using some old code that runs a sql query as a reference. At
I'm using old php script that depends on session_register to gives warning messages but
I periodically get this problem where all of a sudden mako is using old
We're using an old application that stores dates in C / Unix format. C

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.