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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T18:13:11+00:00 2026-06-08T18:13:11+00:00

In my code I do the following initialization : struct PipeShm myPipe = {

  • 0

In my code I do the following initialization :

struct PipeShm myPipe = { .init = 0 , .flag = FALSE , .mutex = NULL , .ptr1 = NULL , .ptr2 = NULL ,
        .status1 = -10 , .status2 = -10 , .semaphoreFlag = FALSE };

int initPipe()
{
    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))  // semaphore is initialized to 0
        {
            myPipe.init = TRUE;
        }
        else
            perror ("initPipe");
    }
    return 1;   // always successful
}

I can have multiple processes that can be invoked from main() (note the fork) .

Thanks

  • 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-08T18:13:12+00:00Added an answer on June 8, 2026 at 6:13 pm

    AFAICS your error is in your control variables. Only your mutex variable is shared between the processes, not your init or flag variables. These are copy on write, so you wouldn’t see the changes in a different process.

    You’d have to pack all of your control variables inside the segment that you create. Create an appropriate struct type for all the fields that you need.

    BTW, calling a semaphore mutex is really a bad idea. A mutex has a semantic that is quite different from a semaphore. (Or if you really use it as a mutex, I didn’t check, use pthread_mutex_t with pshared in the initializer.)

    Edit after your edit: No it wouldn’t work like this. You really have to place the whole struct in the shared segment. So your struct PipeShm must contain a sem_t sem and not a sem_t* mutex. Then you’d do something like

    struct PipeShm * myPipe = 0;
    
    int initPipe()
    {
        if (!myPipe->init)
        {
            myPipe = mmap (NULL, sizeof *myPipe, PROT_READ | PROT_WRITE,MAP_SHARED | MAP_ANONYMOUS, -1, 0);
    
            if (!sem_init (myPipe->sem, 1, 0))  // semaphore is initialized to 0
            {
                myPipe->init = true;
            }
            else
                perror ("initPipe");
        }
        return 1;   // always successful
    }
    

    Other things you should be aware of:

    • The sem_t interfaces can be interrupted by any kind of IO or other signals. You always have to check
      the return of these functions and in particular restart the function
      if it received EINTR.
    • Mondern C has a Boolean. This you can easily use by including
      <stdbool.h> through names of bool, false and true.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have following code in initialization im = imread('Image02.tif'); figure(); imagesc(im); colormap(gray); [hImage hfig
I have an interesting question on initialization. I have the following code: public class
While reading a code I came across, the following definition and initialization of a
The initialization code: FB.init({ appId: '123456789012345', channelUrl: 'http://localhost/Some/Url/FacebookChannel', status: true, cookie: true, oauth: true,
I have been using the following vector initialization with values in Code::Blocks and MingW
Lets say we have the following code: #include <iostream> #include <string> struct A {
I've written the following bit of code that is producing a warning: initialization makes
Whilst reading through the DirectWrite source code I came across the following struct: ///
Why am I getting incompatible types in initialization in the following code? NSString *latString
I have the following struct in my C++ code (I am using Visual Studio

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.