LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-30-2019, 10:26 AM   #1
Cristiano Urban
Member
 
Registered: Jul 2013
Location: Gonars (UD), Italy
Distribution: Slackware 14.2 x86_64
Posts: 76

Rep: Reputation: 10
fork(), shared memory and pointers in C


Hi folks,
I'm trying to share a struct like the one below among the various processes:
Code:
// Data to be shared among processes
struct Shared_data {
    int setPointCounter;
    struct Point old_point;
    pthread_mutex_t my_mutex;
} *shd;
The struct is declared as global (it is located before the main(), let's say...).

I initialize the shared memory into the main:
Code:
shd = (struct Shared_data *) (mmap(NULL, sizeof *shd, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, 0));
and then at a certain point I do a fork(). It works fine.

The problem arises when I have to share something like this:

Code:
// Data to be shared among processes
struct Shared_data {
    int setPointCounter;
    struct Point old_point;
    MyObject *obj;
    pthread_mutex_t my_mutex;
} *shd;
In the main function I call a function which returns a pointer to an object of MyObject type and I would like to share it in some way. How can I do this?
By searching on the web I've found something related to relative pointers but I'm not sure it will work.

I'm working on Linux (Slackware 64 14.2) and the language is C/C++ (mostly is C, actually).

Thank you in advance for your suggestions.

Last edited by Cristiano Urban; 10-30-2019 at 04:54 PM.
 
Old 10-30-2019, 11:18 AM   #2
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,865
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
The pointed object has to be in the shared memory, too. (Note: there is no point in storing pthread_mutex in shared memory: it works only with threads, not with processes.)

Edit: sorry, I have been wrong here regarding mutexes.

Last edited by NevemTeve; 10-30-2019 at 02:37 PM.
 
Old 10-30-2019, 12:54 PM   #3
Cristiano Urban
Member
 
Registered: Jul 2013
Location: Gonars (UD), Italy
Distribution: Slackware 14.2 x86_64
Posts: 76

Original Poster
Rep: Reputation: 10
Hi,
thank you for the answer.

Quote:
Originally Posted by NevemTeve
The pointed object has to be in the shared memory, too.
The function in the main which returns the pointer to the object is part of a third-party library. So, I think the only way to access to the object is through a pointer. :-(

Quote:
Originally Posted by NevemTeve
(Note: there is no point in storing pthread_mutex in shared memory: it works only with threads, not with processes.)
In the main I've set the following:
Code:
pthread_mutexattr_t attr;
pthread_mutexattr_init(&attr);
pthread_mutexattr_setpshared(&attr, PTHREAD_PROCESS_SHARED);
pthread_mutex_init(&shd->my_mutex, &attr);
 
1 members found this post helpful.
Old 10-30-2019, 01:16 PM   #4
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,865
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
The thing you should use is called deep copy -- if you know the complete structure, and it doesn't contain native resources (files, pipes, sockets, any sort of handles).

Note: Sorry, I was wrong regarding mutexes in post #2

Last edited by NevemTeve; 10-31-2019 at 03:23 AM.
 
1 members found this post helpful.
Old 10-31-2019, 02:50 PM   #5
Cristiano Urban
Member
 
Registered: Jul 2013
Location: Gonars (UD), Italy
Distribution: Slackware 14.2 x86_64
Posts: 76

Original Poster
Rep: Reputation: 10
Quote:
Originally Posted by NevemTeve
The thing you should use is called deep copy -- if you know the complete structure, and it doesn't contain native resources (files, pipes, sockets, any sort of handles).
The class seems to not have a copy constructor and to be honest I would not like to change the code of that library, so I think I will go through another way to solve my problem.

Quote:
Originally Posted by NevemTeve
Note: Sorry, I was wrong regarding mutexes in post #2
Hey! It's ok, don't worry!

Thank you for the hints!
 
Old 11-07-2019, 03:45 AM   #6
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
uuugh shared memory, what a hideous concept.

whatever happened to "high cohesion" and "low coupling"?

I have used OpenMP, compile it on linux, compile the same code on windows

https://www.openmp.org/wp-content/up...P3.1-CCard.pdf
 
1 members found this post helpful.
Old 11-07-2019, 05:36 AM   #7
Cristiano Urban
Member
 
Registered: Jul 2013
Location: Gonars (UD), Italy
Distribution: Slackware 14.2 x86_64
Posts: 76

Original Poster
Rep: Reputation: 10
Quote:
Originally Posted by bigearsbilly
What a wonderful cheatsheet!

Thank you for the suggestion but at the end I've solved my problem with the classical "blocking" way (no processes, no threads).
 
Old 11-07-2019, 08:47 AM   #8
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
I remember i was on a project before when someone thought sharing memory between processes was a good idea.
So I cut the brake lines on his car and he went over a cliff.
Consequently we had a much simpler design.

 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: Linux C Programming tutorial Part 21: Character pointers, array of pointers, and pointer to pointer LXer Syndicated Linux News 0 04-12-2019 06:15 PM
fork and shared memory multiplex22 Programming 29 03-11-2015 11:13 AM
[SOLVED] Could I get some pointers on pointers please? theKbStockpiler Programming 46 05-18-2010 12:30 AM
Pointers Pointers Pointers urzumph Programming 9 03-11-2004 09:49 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:39 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration