0
#include <bits/stdc++.h>
#include <unistd.h>
using namespace std;
#define CLONE

mutex m;

int subproc(void*){
    cout << "Start" << endl;
    while(true){
        vector<int>V;
        //m.lock();
        V.reserve(10);
        //m.unlock();
        for(int i=0;i<10;i++)
            V.push_back(i);
    }
}

int main(){
    const unsigned int proc_num = 4;
    const unsigned int stack_size = 16*1024*1024;

    #if defined CLONE
        for(int i=0;i<proc_num;i++)
            clone(subproc, new char[stack_size]+stack_size, CLONE_VM, nullptr);
    #elif defined THREADS
        vector<thread>Vec;
        for(int i=0;i<proc_num;i++)
            Vec.push_back(thread(subproc, nullptr));
    #endif

    while(true)
        sleep(1);
    return 0;
}

When I compile this code with different #defines (CLONE or THREADS), program behaves differently. With CLONE, child processes immediately crash (address sanitizer gives heap-use-after-free error). When I use THREADS, everything works as expected.
If I remove comments from mutexes, both versions work well, so the problem is about allocating memory for vector.

Why is that so? And is there a method to make CLONE version work (condition is that all processes should share memory)?

bebidek
  • 592
  • 3
  • 8
  • 1
    Unrelated: Avoid `#include ` (rational: https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h) and `using namespace std;` (rational: https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice). Definitely avoid using them together as they amplify one another's worst aspects. – user4581301 Apr 27 '18 at 19:35
  • Unfortunately, I will have to use both of them in my final application. Never mind why. – bebidek Apr 27 '18 at 19:45
  • Getting back to this (now that I have a handy dandy Linux box at my disposal) and I can't duplicate. Will take another look at it from home. – user4581301 Apr 27 '18 at 22:11

0 Answers0