ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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.
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.)
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
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
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.