Setting environment variable

Hi All,
I need to use a global variable in the my application with updated value. So i added a GLOBAL variable( with export) in the vi /etc/profile.d/qnxsdk file. To setting this global variable, i have created another application but global variable value is not reflecting change after my appllication and again run same application.
Say initial value is 7 and new value is set with 8 but after running same program 2nd time value is 7 ( but it should be 8). Please find my code,

int main( void )
{
char *path;
int value=0;

if( (path = getenv( "GLOBAL" )) != NULL ) {
            printf( "GLOBAL= %s\n", path );
        }

value = atoi(path);
printf( "GLOBAL = %d\n",value);

path = itoa(value+1,path,10);

printf( "path is = %s\n", path); 

if( setenv( "GLOBAL",path,1) == 0 )
	printf( "path is set");

if( (path = getenv( "GLOBAL" )) != NULL ) {
                printf( "GLOBAL= %s\n", path );
            }
return EXIT_SUCCESS;

}

Environment variable are not global. A common misconception. Exported variables are copied and pass downed to child processes.

There is no way to pass them up.

Look at the program “ds” for a possible solution.

ds didn’t not work for me, it is related to some server and i am working on an embedded device. I have tried to compile sample program for ds_register and ds_create but it is giving error "ENOENT
No such file or directory; the data server isn’t started. "
I am putting my problem in different way,

I have one application say named “Usb_App”, which must be started as soon as a usb device attached to device and Usb_App keep running.
But as per my problem if another usb device attached then another instance of “Usb_App” application shall be started. Which i won’t to avoid. So is there any way to check instance of “Usb_App” is already running ???
For all the usb devices only one instance of application shall be present.

I don`t see what server (which is just a program) and embedded devices have do anything???

That means you probably just didn`t start ds.

At startup get the list of running process ( see source to pidin ) and check if there is more then one instances of usb_app, if so then terminate program.

Gary,

Another option is to do a name_attach() in your Usb_App when it starts. If the call succeeds you know it’s the 1st instance of your app and you keep running. If it fails you know your app is already running and you just exit this 2nd instance. This is how we prevent 2nd instances of our app from running.

All you need to have running is gns (global name server).

Tim

Because of different reasons, very few applications succeed in completely resisting the use of global variables. If you are aware of the drawbacks of this kind of data then don’t use it and retry with different and good techniques.


70-291 questions | 70-290 questions | 70-284 questions