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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T01:26:45+00:00 2026-05-18T01:26:45+00:00

I wrote a simple program solving the Readers-Writers problem using semaphores. It runs perfectly

  • 0

I wrote a simple program solving the Readers-Writers problem using semaphores. It runs perfectly on Linux os, but when I run it on my Mac osX I get unexpected results and I can’t figure out why.

My Program:

#include <semaphore.h>
#include <sys/types.h>
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>

void* function1(void* val);
void* function2(void* val);

// shared values
volatile int X;
volatile int Y;

// declare semaphores
sem_t s1;
sem_t s2;

main()
{
void* status;

pthread_t thread1;
pthread_t thread2;
srand(time(NULL));

// initialize semaphores to zero
sem_init(&s1, 0, 0);
sem_init(&s2, 0, 0);

pthread_create(&thread1, NULL, function1, NULL);
pthread_create(&thread2, NULL, function2, NULL);

pthread_join(thread1, &status);
pthread_join(thread2, &status);

sem_destroy(&s1);
sem_destroy(&s2);

}

void* function1(void* val)
{
   while(1)
   {
   X = rand()%1000; // write 
   printf("After thread ID A writes to X, X = %d\n", X);
   sem_post(&s1); // signal
   sem_wait(&s2); // wait
   printf("After thread ID A reads from Y, Y = %d\n", Y); // read
   sleep(3);
   }   
}

void* function2(void* val)
{
   while(1)
   {
    sem_wait(&s1); // wait
    printf("After thread ID B reads from X, X = %d\n", X); // read
    Y = rand()%1000; // write
    printf("After thread ID B write to Y, Y = %d\n", Y);
    sem_post(&s2); // signal
    sleep(3);
   }
}

The output I receive on Linux (what it’s supposed to look like):

After thread ID A writes to X, X = 100
After thread ID B reads from X, X = 100
After thread ID B write to Y, Y = 234
After thread ID A reads from Y, Y = 234
...

The output on Mac osX (unexpected):

After thread ID A writes to X, X = 253
After thread ID A reads from Y, Y = 0
After thread ID B reads from X, X = 253
After thread ID B write to Y, Y = 728
...
  • 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-18T01:26:45+00:00Added an answer on May 18, 2026 at 1:26 am

    Check the error return on the sem_init calls; I bet you’ll find the OS X version returning a “Function not implemented” error.

    This is because unnamed POSIX semaphores are not implemented on OS X. You need to use named semaphores, or pthread mutex/condition variables.

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

Sidebar

Related Questions

I wrote a simple program that using Class::ArrayObjects but It did not work as
I wrote a simple Windows Forms program in C#. I want to be able
I wrote a simple tool to generate a DBUnit XML dataset using queries that
I wrote a simple web service in C# using SharpDevelop (which I just got
I have a simple Google App Engine app, that I wrote using ordinary strings.
I wrote a simple program that reads the characters from external device (bar code
I wrote a simple program in C# on Visual Studio 2005 on a windows
I wrote this simple program: void sig_ha(int signum) { cout<<received SIGINT\n; } int main()
I wrote a simple program to understand how objective-c works. This program is the
I am learning socket programming in c, I wrote this simple program to accept

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.