linux - C++ getenv doesnt update -


i'm trying make program bash script runs. want bash script able change state of c++ program, , thing find use environment variables. thing is, seems getenv gets value @ time when program run.

bash

export blink=1 ./blink & sleep 5s unset blink 

c++

int main(int args, char **argv) {      char *blink = getenv("blink");      while(blink && blink[0] == '1')     {         std::cout << getenv("blink") << std::endl;         usleep(500000);     }      return 1; } 

so run blink program, wait 5 seconds unset environment. c++ program sees enviorment value 1 , never stops. how updated environment variable while program running? or there better way have bash script control state of c++ program.

edit should note, not want kill process either because has turn off hardware when ends.

it not possible modify program environment after started. have use method of interprocess communication. simplest 1 register handler signal app (e.g. sigusr1), , send using kill -sigusr1 <pid> command.

there other solutions available, e.g. create named pipe (using pipe shell command), , check periodically if wrote it. if yes, exit loop.

you can use sockets if want, more complicated.


Comments

Popular posts from this blog

javascript - oscilloscope of speaker input stops rendering after a few seconds -

javascript - gulp-nodemon - nodejs restart after file change - Error: listen EADDRINUSE events.js:85 -

Fatal Python error: Py_Initialize: unable to load the file system codec. ImportError: No module named 'encodings' -