0

I have a background-process that is started at the beginning of my program, which generates output data every 20ms. In my program, I want to read this data, but would like to avoid constantly writing to/reading from a file as this can be very slow.

The output from every iteration is a single string (max length ~100 characters), and I only need the most recent data at any given time.

Ideally, I could use some sort of in-memory buffer to get this data, but I am unsure how to do this. I have looked into using popen (see also: c++: subprocess output to stdin), however, this seems like it is used to execute a command, and wait for a single output. My output will be more or less constantly changing. I am running this program on Raspbian.

Community
  • 1
  • 1
mwaterbu
  • 11
  • 3

2 Answers2

0

I think you wanna redirect stdout to a stringstream. it is described here.

Combined with this one.

Now you have redirected stdout and will be able to send it to another process.

Community
  • 1
  • 1
Martin Kristiansen
  • 9,875
  • 10
  • 51
  • 83
0

Use shared memory: Allocate memory for the data you want to share, and allocate a second segment for a mutex variable to handle locking.

Example code sharing a float[9]:

server

int shmid, shmid_mutex;
key_t key = 1337, key_mutex = 1338;
float *shm;
pthread_mutex_t *shm_mutex;
int SHMSZ = sizeof(float[9]), SHMSZ_mutex = sizeof(pthread_mutex_t);

//Mutex shared memory
shmid_mutex = shmget(key_mutex, SHMSZ_mutex, IPC_CREAT | 0660); //Create the segment
shm_mutex = (pthread_mutex_t *)shmat(shmid_mutex, NULL, 0); //Attach the segment to data space
shmctl(shmid_mutex, SHM_LOCK, (struct shmid_ds *) NULL); //Lock this segment from being swapped

//Setup mutex attributes
pthread_mutexattr_t psharedm;
pthread_mutexattr_init(&psharedm);
pthread_mutexattr_setpshared(&psharedm, PTHREAD_PROCESS_SHARED);
//Initialize mutex
pthread_mutex_init(shm_mutex, &psharedm);

//Float array shared memory
shmid = shmget(key, SHMSZ, IPC_CREAT | 0660); //Create the segment
shm = (float *)shmat(shmid, NULL, 0); //Attach the segment to data space
shmctl(shmid, SHM_LOCK, (struct shmid_ds *) NULL); //Lock this segment from being swapped

float x = 0.1;
while(true)
{
    pthread_mutex_lock(shm_mutex); //start critical section
    //Write to shared memory
    for (int i = 0; i < 9; i++)
        shm[i] = x * i;
    x += 0.1;
    printf("W ");
    for (int i = 0; i < 9; i++)
        printf("%f ", shm[i]);
    printf("\n");
    usleep(100000); //slow down output to make it readable
    pthread_mutex_unlock(shm_mutex);  //end critical section
    usleep(10000); //wait for 10ms to simulate other calculations
}

client

int shmid, shmid_mutex;
key_t key = 1337, key_mutex = 1338;
float *shm;
pthread_mutex_t *shm_mutex;
int SHMSZ = sizeof(float[9]), SHMSZ_mutex = sizeof(pthread_mutex_t);

//Mutex shared memory
shmid_mutex = shmget(key_mutex, SHMSZ_mutex, IPC_CREAT | 0660); //Create the segment
shm_mutex = (pthread_mutex_t *)shmat(shmid_mutex, NULL, 0); //Attach the segment to data space
shmctl(shmid_mutex, SHM_LOCK, (struct shmid_ds *) NULL); //Lock this segment from being swapped

//Float array shared memory
shmid = shmget(key, SHMSZ, 0660); //Locate the segment
shm = (float *)shmat(shmid, NULL, 0); //Attach the segment to data space
shmctl(shmid, SHM_LOCK, (struct shmid_ds *) NULL); //Lock this segment from being swapped

while(true)
{
    pthread_mutex_lock(shm_mutex); //start critical section
    //Read from shared memory
    printf("R ");
    for (int i = 0; i < 9; i++)
        printf("%f ", shm[i]);
    printf("\n");
    usleep(100000); //slow down output to make it readable
    pthread_mutex_unlock(shm_mutex); //end critical section
    usleep(10000); //wait for 10ms to simulate other calculations
}
mwaterbu
  • 11
  • 3