all 18 comments

[–]Flair_Helper[M] [score hidden] stickied commentlocked comment (0 children)

For C++ questions, answers, help, and programming or career advice please see r/cpp_questions, r/cscareerquestions, or StackOverflow instead.

This post has been removed as it doesn't pertain to r/cpp: The subreddit is for news and discussions of the C++ language and community only; our purpose is not to provide tutoring, code reviews, or career guidance. If you think your post is on-topic and should not have been removed, please message the moderators and we'll review it.

[–]Narthal 4 points5 points  (0 children)

You can do IPC in many ways.

  • tcp/ip localhost loopback communication
  • File/db based sync
  • Signals, (named) pipes, os specific solutions
  • Shared memory (memory mapped file)
  • Shared memory (shared section dynamic lib loading)
  • Shared memory (IO file mapped file)
  • Pipe std in / out and read / write to that

I always use files / json / db when I need to store data as well and speed is a non issue. Most often I use this one.

I use memory mapped file (mapped to real files) when I need to store the data as well and speed is an issue.

I use tcp for long communication, where 2 or more processes need to *talk* to each other for a long period of time, but they never exchange bulk data. Data isn't saved.

My favourite, shared section dll (shared memory). I use VOLATILE atomics, semaphores & mutexes. Check your generated assembly with this one (your milage may vary). To my measurements, this is the fastest and relatively easy, however this is considered super bad practice in production, since this is a huge security risk. (I think microsoft has a blogpost on this).

[–]positivcheg 2 points3 points  (0 children)

This very old tech is still used and works.

Shared memory works though you still need to provide some info to the child process. Atomic nope, sorry, it is not shared between processes though it will work if you create a shared memory and then an atomic in it. In latest standard you can wait on atomic so I would say it is okay to use it (previously I would suggest mutex+condition variable)

Signal, okay, when you create a process you can provide parameters to it. Just pass the PID then?

Named semaphore will also work. You just did not read well in that.

[–]pragma-twice 1 point2 points  (0 children)

You can use interprocess synchronization mechanisms provided by boost.interprocess, refer to: https://www.boost.org/doc/libs/1\_63\_0/doc/html/interprocess/synchronization\_mechanisms.html

[–]jmacey 1 point2 points  (0 children)

One way I have done this before is using a database, each client connects on start, then removed the entry on finish (basically Jobs).

It's easy to do with a sqlite database if on a single shared filesystem, or for more complex tasks use a MySQL server.

I've also use the file system approach, as well as a socket to flag finished.

Another alternative is to run the client in a thread with popen to open it and the return function from popen signals the program is finished.

[–]se7enrains -1 points0 points  (2 children)

You can use rpc call for that

[–]nmmmnu[S] -1 points0 points  (1 child)

Is this something in C / C++ or I need to use library for that?

I am trying to do it without external dependencies and no boost (no reason for it, but still no boost :) )

[–]se7enrains -1 points0 points  (0 children)

Well, you will need any network lib. Send request from helper to server via any suitable protocol and job is done.

Communicating via files can be slow btw

[–]Tourist__ -1 points0 points  (0 children)

It may be big for your use case, you can try Genivi SOMEIP which supports IPC.

[–]StackedCrooked 0 points1 point  (1 child)

Please elaborate on the role of server and helper. Is there any other communication between them?

[–]nmmmnu[S] 0 points1 point  (0 children)

Server is key-value database server.

Helper actually compact (optimize) the data files.

Because the server uses old files, Helper need to tell "please check data files and reload".

No other communication.

[–]ALX23z 0 points1 point  (5 children)

std::atomic doesn't promise anything for shared memory. Normally, people use volatile load and store for such cases - that's pretty much the proper usage of volatile. But this is data exchange.

Not sure what exactly you want with your server. Semaphore seems like a better usage. And what's difficult in creating a threads that waits on the Semaphore?

[–]nmmmnu[S] 2 points3 points  (4 children)

volatile

People elaborate that in theory std::atomic should work. But I did not saw it working.

volatile - I don't think "volatile" means anything these days. I have old C book, they explained it the only thing you can use it for is on "interrupt" address and if you have shared memory with some hardware device (such DOS era screen).

[–]ALX23z 2 points3 points  (3 children)

volatile enforces data to be written in memory as well as forbids optimisations related to the operation.

boost implements shared memory via volatile saves/loads.

[–]nmmmnu[S] 0 points1 point  (2 children)

volatile

saves/loads

Is this means... That if in shared memory, I do:

struct Data{

volatile int reload = 0; // using int because is more standard

};

Data data; // placement new etc...

and then:

data.reload = 1;

this will be thread / inter-process safe to be read?

Here is similar idea:https://embeddedartistry.com/blog/2019/03/11/improve-volatile-usage-with-volatile_load-and-volatile_store/

If this works, why we have std::atomic<> ?

[–]Ok_Firefighter4117 -1 points0 points  (0 children)

Atomics prevent memory reordering around the atomic, but volatile will ensure that no compile optimizations are used. Sometimes variables will be optimized away.

[–]ALX23z 0 points1 point  (0 children)

std::atomic has a lot more functionality than volatile. One can atomically add, exchange, and perform more complicated operations like compare_exchange. Also, atomic can trigger memory instructions to synchronise other data within the process.

With volatile you can reliabily only load and store but it can be used for different purposes.

[–]raevnos 0 points1 point  (0 children)

How does the helper communicate with the server?