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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T02:50:16+00:00 2026-06-09T02:50:16+00:00

I have a problem with a semaphore in my code. This is a struct

  • 0

I have a problem with a semaphore in my code.

This is a struct that I built:

struct PipeShm
{
        // doesn't matter 
        sem_t *mutex;
        int init;
        // more fields
};

Here I initialize the struct:

struct PipeShm myPipe ;
myPipe.mutex = NULL;
myPipe.init = 0;

And I use an initialization method:

int initMethod()
{
    if (!myPipe.init)
    {
        myPipe.mutex = mmap (NULL, sizeof *myPipe.mutex, PROT_READ | PROT_WRITE,MAP_SHARED | MAP_ANONYMOUS, -1, 0);
        if (!sem_init (myPipe.mutex, 1, 0))
        {
            myPipe.init = TRUE;
        }
        else
            perror ("initMethod");
    }
    return 1;   
}

This is a simple main that uses the above :

int main()

{
    int spd, pid, rb;
    char buff[4096];


    shm_pipe_init();
    fork();

        // more code goes here 

        return 0;
}

In the line fork(); (please correct me if I’m wrong) the 2 processes would
have the two different semaphores, right?

If so, I want to make the a global semaphore. Is that possible?

Due to problems of synchronization in my code, I suspect that the main reason is
a double semaphore for each created process.

  • 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-09T02:50:18+00:00Added an answer on June 9, 2026 at 2:50 am

    Your code to initialize the semaphore appears to be correct, so you will have to look elsewhere for your synchronization problems. Below is a test program that illustrates working behavior for your code:

    void do_child () {
        fprintf(stderr, "[%u] child: waiting...\n", (unsigned)time(0));
        sem_wait(myPipe.mutex);
        fprintf(stderr, "[%u] child: sleeping 1...\n", (unsigned)time(0));
        sleep(1);
        fprintf(stderr, "[%u] child: posting...\n", (unsigned)time(0));
        sem_post(myPipe.mutex);
        fprintf(stderr, "[%u] child: done...\n", (unsigned)time(0));
    }
    
    void do_parent (pid_t p) {
        fprintf(stderr, "[%u] parent: sleeping 5...\n", (unsigned)time(0));
        sleep(5);
        fprintf(stderr, "[%u] parent: posting...\n", (unsigned)time(0));
        sem_post(myPipe.mutex);
        fprintf(stderr, "[%u] parent: waiting...\n", (unsigned)time(0));
        sem_wait(myPipe.mutex);
        fprintf(stderr, "[%u] parent: waitpid...\n", (unsigned)time(0));
        waitpid(p, 0, 0);
        fprintf(stderr, "[%u] parent: done...\n", (unsigned)time(0));
    }
    
    int main () {
        pid_t p;
        myPipe.mutex = NULL;
        myPipe.init = 0;
        initMethod();
        switch ((p = fork())) {
        case 0:  do_child(); break;
        case -1: perror("fork"); break;
        default: do_parent(p); break;
        }
        return 0;
    }
    

    I think you would already know, but just in case, a semaphore is not really a mutex. You can think of a mutex as a semaphore that is initialized to a post value of 1. But, a semaphore does not prevent multiple simultaneous posts. If you have spurious posts to the semaphore, this will allow more than one thread to go into the critical section.

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

Sidebar

Related Questions

I have a problem using Semaphore in threading.. this is my situation, i want
I have a problem with semaphore and fork in Linux, here is my code:
I have problem with pointers. This is working fine - int main(void){ char *w;
I have problem with my query on C, I’m using the oci8 driver. This
#include <semaphore.h> sem_t* x; int main () { x = sem_open(x, O_CREAT, 0, 0);;
I have to code a solution to the dining philosophers problem in Java using
I have a problem with two separate transactions that are flushed to the database
I have program that uses shared memory and protects it with semaphore to communicate
I have an application that's built on top of Eventlet. I'm trying to write
I have problem with Activities navigation, searching works good. Activities hierarchy looks like that:

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.