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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T09:21:39+00:00 2026-06-13T09:21:39+00:00

I have the following code: #include<stdio.h> #include<semaphore.h> #include<pthread.h> sem_t semr; void* func(void* i) {

  • 0

I have the following code:

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

sem_t semr;

void* func(void* i)
{
    sem_wait(&semr);
    printf("\nInstance %d running",*(int*)i);
    //sem_post(&semr);
    return NULL;
}


int main(void)
{
    sem_init(&semr,0,1);
    void* (*fp)(void*);
    int s1,s2,s3,val=0;
    pthread_t t1,t2,t3;
    fp=&func;
    val=1;
    s1=pthread_create(&t1,NULL,fp,(void*)&val);
    val=2;
    s2=pthread_create(&t2,NULL,fp,(void*)&val);
    val=3;
    s3=pthread_create(&t3,NULL,fp,(void*)&val);
    pthread_join(t1,NULL);
    pthread_join(t2,NULL);
    pthread_join(t3,NULL);  
    return 0;
}
  

This is my understanding of what happens:

The first thread(t1) executes successfully. The subsequent threads(t2 and t3) though, are blocked, since I never sem_post the semaphore. The pthread_joins will make main() wait for all 3 threads to terminate.

This is what happens:

Neither thread will output anything. Not even t1s output(see question 1 below)

However,

removing all pthread_joins has a better effect in terms of what I expect:
t1 executes successfully and the command prompt is returned.

My questions:

  1. According to the sample code on this page, main() should wait for t2 and t3 to terminate (in addition to successfully executing t1 and outputting something). Am I using pthread_join incorrectly here? What’s happening?

  2. Why happens to the blocked threads(t2 and t3)? Are the threads forced to terminate due to main() returning?

  • 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-13T09:21:40+00:00Added an answer on June 13, 2026 at 9:21 am

    You should ensure that anything you print is terminated (not followed) with a newline. stdout won’t be flushed while main is blocked waiting to join your threads. When you explicitly cancel the program, again, stdout won’t be flushed.

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

Sidebar

Related Questions

I have the following code: #include <stdio.h> #include <math.h> int main(void) { printf(%f\n, fmax(1.2,
I have the following code: #include <stdio.h> int main() { printf ("hello world\n"); return
I have the following code: #include <stdio.h> #include <pthread.h> #define THREAD_CNT 10 #define ITER
i have the following code: #include <stdio.h> int main(void) { float a[4] __attribute__((aligned(0x1000))) =
I have the following code: #include <stdlib.h> #include <stdio.h> #include <pthread.h> #define NUM_THREADS 100
I have following code: #include <stdio.h> // For printf() #include <sys/stat.h> // For struct
i have following code #include <stdlib.h> #include <stdio.h> int sorter( const void *first_arg,const void*
I have the following code: #include <stdlib.h> #include <stdio.h> #include <errno.h> void main(void) {
We have the following code: #include <stdio.h> #define LEN 10 int main(void) { int
I have the following code: #include <stdio.h> void main(){ int x=0, y=0,i=100; for (;i<1000;

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.