Skip navigation.
Home
The QNX Community Portal

View topic - Setting environment variable

Setting environment variable

For discussion of realtime and/or embedded programming.

Setting environment variable

Postby garyritu » Mon Jul 20, 2009 3:16 pm

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;
}
garyritu
Active Member
 
Posts: 58
Joined: Fri May 22, 2009 7:09 am

Re: Setting environment variable

Postby mario » Mon Jul 20, 2009 3:28 pm

garyritu wrote: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.
}


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.
mario
QNX Master
 
Posts: 4132
Joined: Sun Sep 01, 2002 1:04 am

RE: Re: Setting environment variable

Postby garyritu » Wed Jul 22, 2009 6:49 am

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.
garyritu
Active Member
 
Posts: 58
Joined: Fri May 22, 2009 7:09 am

Re: RE: Re: Setting environment variable

Postby mario » Wed Jul 22, 2009 12:30 pm

garyritu wrote:ds didn't not work for me, it is related to some server and i am working on an embedded device.


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

garyritu wrote: 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. "


That means you probably just didn`t start ds.

garyritu wrote: 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.


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.
mario
QNX Master
 
Posts: 4132
Joined: Sun Sep 01, 2002 1:04 am

Re: RE: Re: Setting environment variable

Postby Tim » Wed Jul 22, 2009 12:41 pm

Gary,

garyritu wrote:]
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.


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
Tim
Senior Member
 
Posts: 1388
Joined: Wed Mar 10, 2004 12:28 am

Postby green67 » Tue Sep 15, 2009 5:19 am

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
green67
New Member
 
Posts: 4
Joined: Tue Sep 15, 2009 5:11 am


Return to Realtime and Embedded

Who is online

Users browsing this forum: No registered users and 1 guest