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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T11:53:59+00:00 2026-05-25T11:53:59+00:00

I have a homework project that require the creation of a STATIC library to

  • 0

I have a homework project that require the creation of a STATIC library to provide mutual access to a couple of named pipes.

These pipes are used for communication between various clients using the library and a server.

Now, suppose I want to use pthread mutexes; how can I achieve that? How can the processes know which is the shared memory area in which the mutex is stored? And who should require this memory area? The server can’t because it’s required the library itself to provide mutual exclusion.

Thanks to asveikau i came up with this:

#define SHARED 1
#define MUTEX 1

int main() {

    sem_t* mutex = sem_open("mutex", O_CREAT);

    sem_init(mutex, SHARED, MUTEX);

    fork(), fork(), fork();

    sem_wait(mutex);

    int i;
    for(i = 0; i < 10; i++)
        printf("process %d %d\n", getpid(), i), fflush(stdout);

    sem_post(mutex);
}

that from the output really seem to solve my problem.

Thank you to everyone.

  • 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-25T11:54:00+00:00Added an answer on May 25, 2026 at 11:54 am

    I put this down as a comment, but it’s worth an answer I think.

    As others state, pthread mutexes are not cross-process. What you need is a “named mutex”. You can use sem_open to create a cross-process semaphore, and give it an initial count of 1. In that case sem_wait becomes “mutex lock” and sem_post becomes “mutex unlock”.

    Note that sem_open, while part of POSIX, is not universally supported. I believe it works on Linux and Mac OS X. Probably Solaris if you care about that (these days you probably don’t). I know on OpenBSD it always fails with ENOSYS. YMMV.

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

Sidebar

Related Questions

This is homework, just so that's mentioned. I have project called AdventCalendar, which is
I have a homework question that says: Problem 1: Given the array [ 22
I have a homework question please help with a little explanation so that I
I have a small homework application that writes random numbers from 5 to 77
This is currently a homework project that me and my teammate are stuck on.
In my programming class we currently have a project that requires us to take
I am working on a homework project for a Networking class where we have
I have some homework that I was flying through until I got to this
I have a homework assignment where I am buildign a GUI JPane that holds
I'm doing a C homework project and I'm incredibly lost. Essentially, I have to

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.