0

I know how to synchronize between process and how to synchronize between threads. But I don't know how to synchronize between process with multiple threads.

Consider a scenario

I am creating 10 threads in a process. Each thread is printing a number.

I am running 10 processes simultaneously.

Now I need to create synchronization between process and threads such that at last, it should print 100 unique numbers in the console.Kindly note i don't expect the numbers printed in console to be in sequence , but all 100 numbers printed should be unique.

Kindly note that this idea should not only be limited to the above task. It may happen that I want to only one thread of one of the processes to execute certain code.

How to write this program in CPP for redhat.

Kindly note the version: gcc version 4.4.7.

skratchi.at
  • 1,151
  • 7
  • 22
Abhishek Garg
  • 308
  • 2
  • 11
  • 1
    cpp does not provide any specific inter-process communication features. you can use any of hundreds of available IPC methods directly from libc or from other libraries. e.g. pipes, shared memory semaphores, fifos, ... – Serge Feb 20 '19 at 14:09
  • @Serge I don't have any idea how to do it using shared memory semaphores. It will be kind of u if u could help me. – Abhishek Garg Feb 20 '19 at 14:13
  • It will depend on your tools, but my general expectation would be that process-shared synchronization objects (mutexes, semaphores, *etc*.) will serve your inter-thread synchronization requirements, too. That is, you ought to be able to synchronize the activity of multiple threads across multiple processes with one set of synchronization objects. – John Bollinger Feb 20 '19 at 14:13
  • Synchronization is usually about resources. Your statement seems to be more one of synchronizing output than of processes. So it seems that one way to do it is to have one overarching process have a semaphore for the console, where each thread has to acquire exclusive control of the console via that semaphore. What you use as a semaphore really depends on the operating environment – infixed Feb 20 '19 at 14:18
  • 2
    You might want to look at [boost::interprocess](https://www.boost.org/doc/libs/1_69_0/doc/html/interprocess.html). Also note that gcc 4.4.7 is old and may even be missing certain `c++11` features. – G.M. Feb 20 '19 at 14:19
  • @JohnBollinger with synchronization , it should be able to generate unique numbers as explained above – Abhishek Garg Feb 20 '19 at 14:19
  • Add an extra process that takes the output (with `select(2)`, for example) from the 10 other processes and interleaves them. – Botje Feb 20 '19 at 14:20
  • @infixed please read the edit done in question – Abhishek Garg Feb 20 '19 at 14:22
  • You may want to look at https://stackoverflow.com/questions/13161153/c11-interprocess-atomics-and-mutexes But if you want to deal with interprocess, perhaps you should start looking at `man sem_overview` too. Which is more C than C++ – infixed Feb 20 '19 at 14:27
  • Yes, @AbhishekGarg, and what? I took you to be saying that you already know how to use synchronization to solve the problem, and were merely uncertain about how to synchronize threads belonging to different processes. If that is not actually the case then this question is much too broad for SO. – John Bollinger Feb 20 '19 at 14:31
  • @JohnBollinger i did it using lockf() , it did not work as expected – Abhishek Garg Feb 20 '19 at 14:39
  • Try mpi, it's pretty good at synchronizing processes – Guillaume Racicot Feb 20 '19 at 14:41
  • Then actually, no, you do *not* know (very well) how to synchronize between processes. I suppose you could use your `flock()` lock to synchronize between the processes, and when each process runs, let each of its threads run with appropriate inter-thread synchronization within the process. The more elegant way, however, would be to use a genuine IPC mechanism, such as a [POSIX semaphore](http://man7.org/linux/man-pages/man7/sem_overview.7.html), to synchronize the threads directly. – John Bollinger Feb 20 '19 at 14:46
  • so @JohnBollinger by using named semaphore as mentioned [here] , it will synchronize between process with multiple threads in cpp. – Abhishek Garg Feb 20 '19 at 15:28
  • you need to figure out which method is the best for you. google it. try this https://stackoverflow.com/questions/372198/best-way-for-interprocess-communication-in-c – Serge Feb 20 '19 at 18:37
  • We can use Boost.Interprocess .Thanks guys for your comments – Abhishek Garg Feb 24 '19 at 00:28

0 Answers0