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

The Archive Base Latest Questions

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

When I do this : myProgram.h myProgram.c struct PipeShm { // all my fields

  • 0

When I do this :

myProgram.h
myProgram.c

    struct PipeShm
    {
    // all my fields
    // more 
    // ...

    };



    struct PipeShm myPipe = { /* initialization for all fields */ };
    struct PipeShm * sharedPipe = &myPipe;

void func()
{
 sharedPipe = mmap (NULL, sizeof * sharedPipe, PROT_READ | PROT_WRITE,MAP_SHARED | MAP_ANONYMOUS, -1, 0);

}

When I mmap the pointer sharedPipe, if I invoke from main() any methods from myProgram code, would all processes share the exact shared memory that I shared with myPipe struct?

Or would each new child that’s created, would have a new myPipe of his own?

Regards

EDIT:

This is after I read the comments & answers : now changes were made , and I initialize the values of the segment only after I allocate it :

#include "my_pipe.h"

struct PipeShm * sharedPipe = NULL;



int shm_pipe_init()
{
    if (!sharedPipe)
    {
        int myFd = shm_open ("/myregion", O_CREAT | O_TRUNC | O_RDWR, 0600);

        if (myFd == -1)
            error_out ("shm_open");

        // Allocate some memory in the region - We use ftruncate, write(2) would work just as well
        int retAlloc = ftruncate (myFd, sizeof * sharedPipe);
        if (retAlloc < 0)
            error_out("ftruncate");


        sharedPipe = mmap (NULL, sizeof * sharedPipe,
                PROT_READ | PROT_WRITE,MAP_SHARED | MAP_ANONYMOUS, myFd, 0);

        if (!sem_init (&sharedPipe->semaphore, 1, 0))
        {
            sharedPipe->init = TRUE;
            sharedPipe->flag = FALSE;
            sharedPipe->ptr1 = NULL;
            sharedPipe->ptr2 = NULL;
            sharedPipe->status1 = -10;
            sharedPipe->status2 = -10;
            sharedPipe->semaphoreFlag = FALSE;
            sharedPipe->currentPipeIndex = 0;

        }
        else
            perror ("shm_pipe_init");
    }
    return 1;   // always successful
}

The problem however continues ,the shared memory seems to be not so shared between the processes , since while running and forking that main :

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

    fork();
    shm_pipe_init();

        // more
        return 0;
}

I still get outputs , that simulates the behavior like only one process is running (instead of multiple outputs , I get only a single one or a couple ,depends on a race condition between the processes) .

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

    If you intend to call this program several times, the answer is “no”. If you intend to fork after creating the mapping, the answer is “yes”.

    An anonymous mapping has no underlying file. Therefore, a process creating an anonymous mapping has no way of specifying which already existing mapping in particular it wants (and this is also not the intended usage, you’re supposed to get a new, independent one). Therefore “no” to the first case.

    A shared mapping allows all processes that own the same mapping to access the same phsyical memory. This means, if you fork after creating the mapping, then fork will mostly work the usual way, marking all pages owned by the process as copy-on-write except the pages in the mapping. Both the parent and the child will retain the mapped pages and will be able to access the same physical memory through the pointer (incidentially, this means the pages will even have the same virtual addresses, too — this is normally not something you can rely on when mapping a file, but in this case the OS has no other choice but to make sure this is the case).

    The manpages remain vague about the combination of anonymous and shared mapping or about what exactly is supposed to happen, but TLPI chapter 49.7 explicitly mentions MAP_SHARED|MAP_ANONYMOUS:

    […] followed by a call to fork(), then, because the child produced by fork() inherits the mapping, both processes share the memory region.

    Therefore “yes” for the second case.

    Re: edited post
    Wrong order:

    fork();
    shm_pipe_init();
    

    This forks first and then initializes the shared memory. Which will just create a shared (read as share-able, it may be shared in the future, if the process forks again, but it does not magically back-share with the parent!) mapping for each process, separately.

    You then have two mappings, one in each process, which will be shared by their respective child and grandchild processes (if any), but which will help nothing to achieve what you want. The fact that they are “shared” does not matter, they’re different mappings, and unknown to the respective other process.

    First create the mapping, then fork. This will create one shared mapping that both processes indeed own/share and can use for the intended purpose.

    (Besides, your usage of fork is not strictly wrong, but a bit weird… you’re normally supposed to check the return value, so you know which one is the parent and which one is the child. And it’s not like fork can’t fail. Of course if that doesn’t matter at all because parent and child are always 100% identical and you don’t care about failure, that’s fine. Normally, one usually wants to know, though.)

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

Sidebar

Related Questions

On whatever reason this is not working (says 'file not found'), set in=c:\myprogram\_save cd
This is my program - (void)viewDidLoad { [super viewDidLoad]; // Uncomment the following line
I have this in my source code: struct passwd* user_info = getpwnam(root); Is there
If I have a snippit of my program like this: struct Node *node; while(...){
I have something like this in my code typedef struct ts_fem_mesh { double **vertices;
I have a struct like this: typedef struct tgPoligono { int numero_de_lados; CvPoint** cantos;
This is my program: int main(int argc, char *argv[]){ if(argc != 2){ printf(Uso: ./server
This is my program .when i submit the print button i can print the
This is my server code I have a problem because my program freeze and
This is not homework, I need this for my program :) I ask this

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.