0

I want to do the data exchange between two program by setting the variable value to a specific address.The data can assign into the address, but when I change the value in one of the program the other still not get the value

Program1.cpp set the value to specific address and Program2.cpp to get the data from that address, both include a header file "DataExchange.h"

Program1.cpp :

#include <iostream>
#include <windows.h>
#include "DataExchange.h"

using namespace std;

int main(int argc, char** argv) {
    double val2 = 0;
    double spd = 1; 
    double* ptr2 = (double*)speed_address; 
    cout << "Program1" << endl;


    while(1){
        DXSetvalue (Speed, spd);
        val2 = DXGetvalue (Speed);
        cout << "[speed] = " << spd++ << " [get speed] = " << val2 << " [address] = " << ptr2 << "\r" ; 
        Sleep(2000); // 2sec
    }

    return 0;
}

Program2.cpp:

#include <iostream>
#include <windows.h>
#include "DataExchange.h"

using namespace std;

int main(int argc, char** argv) {

    double t = 0;
    double val = 0;
    int* ptr = (int*)speed_address;
    cout << "Program1 Node" <<endl;
    init();

    while(1){
        t += 2;
        val = DXGetvalue (Speed);
        cout << "(" << t << "sec) " <<"[Speed] = " << val << " [blackboard address] = " << ptr << "\r";
        Sleep(2000); 
    } 
}

DataExchange.h:

#include "stdio.h"


// The macro of the value ID
#define Speed 1


// Define of the address of the value
//#define speed_address 0x4a8048
//#define speed_address 0x00CFFB3C
#define speed_address 0x7ffef8

// Declaration
void setaddressValue (double* address, double d);
double getaddressValue (int name);
double* addressID (int name);
void DXSetvalue (int name, double d);
double DXGetvalue (int name);
void init();


// Definition

void setaddressValue (double* address, double d){   
    double* p = (double*)address;
    *p = d;
    //printf("p = 0x%x *p = %lf\n", p, *p);
}

double getaddressValue (int name){
    double* pGet = addressID(name);
    double value = *pGet;
    //printf("pGet = 0x%x *pGet = %lf\n", pGet, *pGet);
    return value;
}


double* addressID (int name){
    switch (name){
        case 1:
            return (double*)speed_address;
        default:
            //cout << "not set the address" << endl;
            printf("not set the address!!!\n");
    }
}


/*program data*/
void DXSetvalue (int name, double d){
    setaddressValue(addressID(name), d);
}


double DXGetvalue (int name){
    return getaddressValue (name);
}


void init(){
    double* p = (double*)speed_address;
    *p = 0.5;
}
Morteza Jalambadani
  • 2,190
  • 6
  • 21
  • 35
Ting-Wei Chien
  • 149
  • 1
  • 2
  • 9
  • 6
    You can't access arbitrary hardware addresses. Look up interprocess communication for your operating system. – Kenny Ostrom May 18 '19 at 19:36
  • 1
    Programs have separate virtual memories, so address `0x35153260` in one program is very different from address `0x35153260` in another program. OS is responsible for that. You very, very much don't want anyone to mess with memory in your program. Imagine the possibilities for hackers if that was possible! – Yksisarvinen May 18 '19 at 19:37
  • Linux processes can share memory. see https://www.geeksforgeeks.org/ipc-shared-memory/. See also Linux functions shmget and mmap documentation. These routines map a shared memory region, but the mapping for each process will most likely be different, so you won't be using a fixed address like 'speed_address'. Instead, each process of your code will ask the os where the shared memory is mapped. see also, https://stackoverflow.com/q/5656530/2785528. Did you do any searches? – 2785528 May 19 '19 at 00:57

1 Answers1

1

Programs running atop a kernel use virtual addressing, this means that a given program cannot access the address space of another. If you would like to facilitate interprocess communication consider Unix sockets.

0x777C
  • 993
  • 7
  • 21